C++ programs to implement the Stack ADT using a singly linked list

/* Write C++ programs to implement the Stack ADT using a singly linkedlist*/

#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
class node
{
public:
class node *next;
int data;
};
 
class stack : public node
{
node *head;
int tos;
public:
stack()
{
tos=-1;
}
void push(int x)
{        
if (tos < 0 )
{
head =new node;
head->next=NULL;
head->data=x;
tos ++;
}
else
{
node *temp,*temp1;
temp=head;
if(tos >= 4)
{
cout <<"stack over flow";
return;
}
tos++;
while(temp->next != NULL)
temp=temp->next;
temp1=new node;
temp->next=temp1;
temp1->next=NULL;
temp1->data=x;
}
}
void display()
{
node *temp;
temp=head;
if (tos < 0)
{
cout <<" stack under flow";
return;
}
while(temp != NULL)
{
cout <<temp->data<< " ";
temp=temp->next;
}
}
void pop()
{
node *temp;
temp=head;
if( tos < 0 )
{
cout <<"stack under flow";
return;
}                  
tos--;
while(temp->next->next!=NULL)
{
temp=temp->next;
}
temp->next=NULL;
}
};
main()
{
stack s1;
int ch;
while(1)
{
cout <<"\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n enter ur choice:";
cin >> ch;
switch(ch)
{
case 1:   cout <<"\n enter a element";
cin >> ch;
s1.push(ch);
break;
case 2:   s1.pop();break;
 
 
 
 
case 3:   s1.display();
break;
case 4:   exit(0);
}
}
return (0);
}

OUTPUT
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element23

1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:1
enter a element67

1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:3
23 67
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2

1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:3
23
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2

1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:2
stack under flow
1.PUSH 2.POP 3.DISPLAY 4.EXIT
enter ru choice:4

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

20 thoughts on “C++ programs to implement the Stack ADT using a singly linked list

  1. You just made a queue… A stack is First in Last out. A queue is First in First out. If I run this, and push 1, 2, and 3 in that order, when I display the data, I should get 3, 2, 1. That is how a stack works. Yours gives me 1,2,3. While the order of data is irrelevant to me, I’m using my program for my portfolio and I’d look pretty dumb if I submitted a stack and it worked like a queue…

  2. The display function displays the list in FIFO manner….. 🙁 I guess changing the insert function by inserting each element at the beginning will solve the issue…

  3. /****** Program to Implement Stack using Linked List ******/
    /*
    * Student database by using Single linked list
    */
    #include
    #include
    #include
    #include
    using namespace std;

    struct node
    {
    int id, marks;
    char grade, name[20], course[15];
    struct node *next;
    } *headnode = NULL, *tempnode, *startnode;
    class student
    {
    public:
    void add();
    void pop();
    void display();
    };
    void student::add()
    {
    system(“cls”);
    int id, marks, totalmarks, m1, m2, m3, m4, m5, m6;
    tempnode = new node;
    if (headnode == NULL)
    {
    cout <> tempnode->id;
    cout <> tempnode->name;
    cout <> tempnode->course;
    tempnode->next = NULL;
    headnode = tempnode;
    }
    else
    {
    cout <> tempnode->id;
    cout <> tempnode->name;
    cout <> tempnode->course;
    tempnode->next = headnode;
    headnode = tempnode;
    }
    }

    void student::display()
    {
    system(“cls”);
    int item;
    struct node *ptr = headnode;
    int i = 1;
    if (ptr == NULL)
    cout << "\n list is empty.\n";
    else
    {
    cout << "\n id\tname\tcourse\tGrade \n\n";
    while (ptr != NULL)
    {
    cout << " " <id << " " <name << " ";
    cout <course << " " <grade <next;
    i++;
    }
    }
    //system(“pause”);
    }

    void student::pop()
    {
    int item;
    struct node *ptr;
    if (headnode == NULL)
    cout <id;
    headnode = headnode->next;
    free(ptr);

    cout << "\n\n Item deleted:";
    }
    }
    int main()
    {
    student sd;
    system("cls");
    int ch = 0;
    //char ni[5];
    while (ch != 7)
    {
    cout << "\n \t\t************STUDENT DATABASE************";
    cout << "\n \t\t—————————————-";
    cout << "\n\n \t\t\t 1.Push the Student Record \n\n \t\t\t 2.Pop Record";
    cout << "\n\n \t\t\t 3.Display Records \n\n \t\t\t 0.Exit This Menu";
    cout <> ch;
    switch (ch)
    {
    case 1:
    sd.add();
    break;
    case 2:
    sd.pop();
    break;
    case 3:
    sd.display();
    break;
    case 0:
    exit(0);
    }
    }
    getch();
    return 0;
    }

  4. Hai Friends..The above program don’t have any errors..use dev c++ ide application not in turbo c++…its good program

  5. “C++ programs to implement the Stack ADT using a singly linked list” This program not contains any error..use dev c++ ide…if u use turbo c++ do some modifications…

  6. there is a small mistake in the pop function… plz check it out…. once you push two elements into the stack.. pop one and the display.. it displays both the elements…. i think the while loop is unnecessary and “temp->p=NULL” shoul be included in the else statement its self

    void pop()
    {
    node *temp;
    temp=s;
    if( tos == -1 )
    {
    cout <p=NULL;
    }
    }

  7. theres a problem with the code… unable to copy it… plz ignore the above code and try checking the code of pop function once again

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in