Thursday, July 7, 2016

Stack and Queue as Link List


Both stack and queue can be implemented as a link list.

Program of stack as a link list


#include<iostream>
using namespace std;
class stackaslist
{
    struct node
    {
        int data;
        struct node *link;
    };
    struct node *start,*temp;
    public:
        stackaslist()
        {
            start=NULL;
        }
        void push(int q)
        {
            struct node *tmp;
            tmp=(struct node*)new(struct node);
            tmp->data=q;
            tmp->link=start;
            start=tmp;
            cout<<q<<" Pushed.\n";
        }
        void pop()
        {
            if(start==NULL)
            {
                cout<<"\nStack is empty.\n";
            }
            else
            {
                temp=start;
                cout<<start->data<<" Popped.\n";
                start=start->link;
                temp->link=NULL;
            }
        }
};

int main()
{
    stackaslist s1;
    s1.push(1);
    s1.push(2);
    s1.push(3);
    s1.pop();
    s1.pop();
    s1.pop();
    s1.pop();
    return 0;
}

Program of queue as a link list


#include<iostream>
using namespace std;
class queueaslist
{
    struct node
    {
        int data;
        struct node *link;
    };
    struct node *front,*rear;
    public:
    queueaslist()
    {
        front=NULL;
    }
    void enqueue(int q)
    {
            struct node *tmp;
            tmp=(struct node*)new(struct node);
            tmp->data=q;
            tmp->link=NULL;
            if(rear!=NULL)
                rear->link=tmp;
            rear=tmp;
            cout<<q<<" Enqueued.\n";
            if (front==NULL)
            front=rear;
    }
    void dequeue()
    {
        if (front==NULL)
            cout<<"\nQueue is empty.\n";
        else
            {
                cout<<front->data<<" Dequeued.\n";
                if(front!=rear)
                    front=front->link;
                else
                    front=NULL;
            }
    }
};

int main()
{
    queueaslist q;
    q.enqueue(1);
    q.enqueue(2);
    q.enqueue(3);
    q.enqueue(4);
    q.dequeue();
    q.dequeue();
    q.dequeue();
    q.dequeue();
    q.dequeue();
    return 0;
}


Program for single link list


#include<iostream>
#include<windows.h>
#include<conio.h>
using namespace std;
class lnklist
{
 //   private:
        struct node
        {
            int data;
            struct node *link;
        };
        struct node *start,*templ,*hold;
    public:
        lnklist()
        {
            start=NULL;
        }
        void addatbegin(int q)
        {
            struct node *tmp;
            tmp=(struct node*)new(struct node);
            tmp->data=q;
            cout<<q<<" added to the beginning of the list.\n";
            if(start==NULL)
                tmp->link=NULL;
            else
                tmp->link=start;
            start=tmp;

        }
        void addatend(int q)
        {
            struct node *tmp;
            tmp=(struct node*)new(struct node);
            tmp->data=q;
            tmp->link=NULL;
            if(start==NULL)
                start=tmp;
            else
                {
                    templ=start;
                    while(templ->link=NULL)
                    {
                        templ=templ->link;
                    }
                    templ->link=tmp;
                    cout<<tmp->data<<" added to the end of the list.\n";
                }
        }
        void addafterdata(int q, int pos)
        {
            templ=start;
            int j=0;
            while(j<pos)
            {
                templ=templ->link;
                if(templ==NULL)
                {
                    cout<<"\nNode in the list is less than the position supplied\n";
                    exit(0);
                }
                j=j+1;
            }
            struct node *tmp;
            tmp=(struct node*)new(struct node);
            tmp->data=q;
            tmp->link=templ->link;
            templ->link=tmp;
            cout<<q<<" added to the "<<pos<<" in the list.\n";
        }
        void deletenode(int q)
        {
            hold->data=0;
            if(start->data==q)
            {
                templ=start;
                start=start->link;
                templ->link=NULL;
                return ;
            }
            hold=start;
            while((hold->link)->data!=0)
            {
                if((hold->link)->data==q)
                {
                    templ=hold->link;
                    hold->link=templ->link;
                    templ->link=NULL;
                    return ;
                }
                hold=hold->link;
            }
            if((hold->link)->data==q)
            {
                templ=hold->link;
                templ->link=NULL;
                hold->link=NULL;
                return ;
            }
            cout<<q<<" Data not found\n";
        }
        void display()
        {
            struct node *tmp;
            cout<<endl;
            if(start == NULL)
                {
                    cout<<"\n\nList is empty";
                    return;
                }
            tmp=start;
            cout<<"\n\nList is : ";
            while(tmp!=NULL)
                {
                    cout<<tmp->data<<"\t"<<tmp->link<<endl;
                    tmp=tmp->link;
                }
            cout<<"\n";

        }
};

int main()
{
    lnklist l1;
    l1.addatbegin(1);
    l1.addatend(2);
    l1.addatbegin(3);
    l1.addafterdata(4,0);
    l1.display();
    l1.deletenode(2);
    l1.display();
    return 0;

}

No comments:

Post a Comment