0% found this document useful (0 votes)
29 views3 pages

Stack Using Linked List C++

The document contains a C++ implementation of a stack data structure with various operations such as push, pop, display, count, peek, isempty, and search. It includes a main function that provides a menu for user interaction to perform these operations. However, there are several syntax errors and issues in the code that need to be addressed for it to function correctly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Stack Using Linked List C++

The document contains a C++ implementation of a stack data structure with various operations such as push, pop, display, count, peek, isempty, and search. It includes a main function that provides a menu for user interaction to perform these operations. However, there are several syntax errors and issues in the code that need to be addressed for it to function correctly.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<iostream>

using namespace std

struct stack
{
int data;
struct stack *next;
};
typedef struct stack stack;
stack *top = 0, *newnode, *temp;
int
main ()
{
int x, ch;
while(1);
{
cout<<"1.push\t2.pop\t3.display\t any key to exit";
cout<<"\n enter the choice::";
cin>>"%d", &ch;
switch (ch)
{
case 1:
cout<< "\n enter the number::";
cin>>"%d", &x;
push (x);
break;
case 2:
pop ();
break;
case 3:
display ();
break;

default:
exit(0);

}
}
void push(int x)
{
newnode=(stack *)malloc(sizeof(stack));
if(newnode==NULL)
{
cout<<"\n memory is not allocate!!";
}
else
{
newnode->data=x;
newnode->next=top;
top=newnode;
}
}
void pop()
{
if(top==0)
{
cout<<"\n stack is empty";
}
else
{
temp=top;
cout<<"\n %d is deleted"<<top->data;
top=top->next;
free(temp);
}
}
void display()
{

if(top==0)
{
printf("\n stack is empty");
}
else
{
temp=top;
while(temp->next!=0)
{
printf("%d---->",temp->data);
temp=temp->next;
}
printf("%d",temp->data);
}
}
void count()
{
int c=0;
if(top==0)
{
printf("\n stack is empty");
}
else
{
temp=top;
while(temp->next!=0)
{
c++;
temp=temp->next;
}
c++;
printf("\ncount=%d",c);
}
}
void peek()

if(top==0)
{
printf("\n stack is empty");
}
else
{
printf("\n%d is the peek element",top->data);
}
}
void isempty()
{

if(top==0)
{
printf("\n stack is empty");
}
else
{
printf("\n stack is not empty");
}
}
void search()
{
int key,i=0,flag=0;
if(top==0)
{
printf("\n stack is empty");
}
else
{
temp=top;
printf("\n enter the number:");
scanf("%d",&key);
while(temp->next!=0)
{
i=i+1;
if(temp->data==key)
{
flag=1;
printf("\n element is found at %d place",i);
}
temp=temp->next;
}
if(temp->data==key)
{
i=i+1;
flag=1;
printf("\n element is found at %d place",i);
}
if(flag==0)
{
printf("\n elemeent is not found");
}
}
}

You might also like