DOWNLOAD C++ CODE FOR STACK IMPLEMENTATION USING LINK LIST

STACK IMPLEMENTATION USING LINK LIST SOURCE CODE :  

For those who are studying data structures with respect to C++  this is a source code for stack implemented by the use of linklist.Download it by clicking the download button below or you can copy paste the code.

HAPPY CODING!!!

This is header file.

#ifndef LIST_H_INCLUDED
#define LIST_H_INCLUDED

class data
{//start of class
    private:
    int element;
    data *Next;

    public:
    data();//constructor
    void add_data(data *&ptr);
    void print_data(data *ptr);
    int calculate_sum(data *ptr);
};//end of class




class list
{//start of struct
   private:
   int id;
   int sum;
   data *root;
   list *previous;

   public:
   list();//constructor
   //~list();//destructor
   void push(list *&head);
   void pop(list *&head);
   void print_node(list *head);
   void print_by_id(list *head);
};//end of class



#endif // LIST_H_INCLUDED



This is .cpp file

#include<iostream>
#include<windows.h>
#include"list.h"

using namespace std;


data::data()
{//start of constructor
    Next=NUnbsp;      system("cls");
                obj1.pop(head);
                system("pause");
                break;
            }//end of case 2

            case 3:
            {//start of case 3
                system("cls");
                cout<<"stack position is:"<<endl<<endl;
                obj1.print_node(head);
                system("pause");
 &nb;   cout<<"Enter the data:";
                cin>>node->element;

                 if (ptr==NULL)
                {//start if
                   ptr=node;
                   ptr->Next=NULL;
                }//end if
                else
                {//star else
                    data *current=ptr;
                    while(current->Next!=NULL)
                    {//start of while
                       current=current->Next;
                    }//end of while
                    current->Next=node;
                    node->Next=NULL;
                }//end else
                break;

            }//end of case1

            case 2:
            {//start of case 2
                flag=false;
                break;
            }//end of case 2


        }//end of switch
    }//end of while
}//end of function

void data::print_data(data *ptr)
{//start of function
    data *temp=ptr;
    int i=1;
    while(temp!=NULL)
    {//start while
        cout<<"data element "<<i<<" is:"<<temp->element<<endl;
        temp=temp->Next;
        i++;

    }//end while
}//end of function

int data::calculate_sum(data *ptr)
{//start of function
    int total_sum=0;
        data *temp=ptr;
    while(temp!=NULL)
    {//start while
        total_sum=total_sum+(temp->element);
        temp=temp->Next;

    }//end while

    return total_sum;
}//end of function



list::list()
{//start of constructor
   root=NULL;
   previous=NULL;
}//end of constructor


void list::push(list *&head)
{//start of function
    list *node=new list;

    cout<<"Enter the id:";
    cin>>node->id;

    data obj2;
    obj2.add_data(node->root);

    node->sum=obj2.calculate_sum(node->root);

    if (head==NULL)
    {//start if
       head=node;
       head->previous=NULL;
    }//end if
    else
    {//star else
        list *current=head;
        head=node;
        node->previous=current;

    }//end else

}//end of function

void list::pop(list *&head)
{//start of function
     data obj2;

    cout<<"Top of the stack node is poped:"<<endl;
    cout<<"id is:"<<head->id<<endl;
    cout<<"sum is:"<<head->sum<<endl;
    obj2.print_data(head->root);


    list *temp=head;
    head=head->previous;
    delete temp;





}//end of function

void list::print_node(list *head)
{//start of function
    list *temp=head;
    data obj2;
    while(temp!=NULL)
    {//start while
        cout<<"id is:"<<temp->id<<endl;
        cout<<"sum is:"<<temp->sum<<endl;
        obj2.print_data(temp->root);
        cout<<endl;
        temp=temp->previous;

    }//end while

}//end of function

void list::print_by_id(list *head)
{//start of function
    list *temp=head;
    data obj2;

    int enter_id;
    cout<<"Enter the id:";
    cin>>enter_id;

    while(temp!=NULL)
    {//start while
        if (temp->id==enter_id)
        {//start if
            cout<<"id is:"<<temp->id<<endl;
            cout<<"sum is:"<<temp->sum<<endl;
            obj2.print_data(temp->root);
        }//end if
            cout<<endl;
            temp=temp->previous;
    }//end while
}//end of function





This is main.cpp file.

//link list implementation using stacks

#include<iostream>
#include<windows.h>
#include"list.h"

using namespace std;


int main()
{//start of main

    list *head=NULL;
    list obj1;
    bool condition=true;

    while(condition==true)
    {//start of while
        system("cls");
        cout<<"Enter 1 to push node"<<endl;
        cout<<"Enter 2 to pop node"<<endl;
        cout<<"Enter 3 to print all"<<endl;
        cout<<"Enter 4 to print by id"<<endl;
        cout<<"Enter 5 to exit"<<endl;
        int a;
        cin>>a;

        switch (a)
        {//start of switch
            case 1:
            {//start of case 1
                obj1.push(head);
                break;
            }//end of case 1

            case 2:
            {//start of case 2
                system("cls");
                obj1.pop(head);
                system("pause");
                break;
            }//end of case 2

            case 3:
            {//start of case 3
                system("cls");
                cout<<"stack position is:"<<endl<<endl;
                obj1.print_node(head);
                system("pause");
                break;
            }//end of case 3

            case 4:
            {//start of case 4
                  system("cls");
                obj1.print_by_id(head);
                system("pause");
                break;
            }//end of case 4

            case 5:
            {//start of case 5
                condition=false;
                break;
            }//end of case 5

        }//end of switch
    }//end of while

return 0;
}//end of main



  DOWNLOAD CODE WITH EXECUTABLE FILE:


No comments:

Post a Comment