0% found this document useful (0 votes)
33 views

Nickel: Linked Stack

This document contains C++ code to implement linked data structures for a stack and queue. For the stack, it defines push and pop functions to add and remove elements. For the queue, it defines insert and delete functions to add elements to the rear and remove them from the front. The main function provides a menu to test the stack and queue functions and display the contents.

Uploaded by

Abram Becker
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Nickel: Linked Stack

This document contains C++ code to implement linked data structures for a stack and queue. For the stack, it defines push and pop functions to add and remove elements. For the queue, it defines insert and delete functions to add elements to the rear and remove them from the front. The main function provides a menu to test the stack and queue functions and display the contents.

Uploaded by

Abram Becker
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

https://www.scribd.

com/doc/40156935/project-onnickel
LINKED STACK
#include<iostream.h>
#include<conio.h>
struct node
{int data;
node *next;};
node *push(node *top,int val)
{node *x=new node;
x->data=val;
x->next=top;
top=x;
return(top);
}
node *pop(node *top,int &val)
{node *x;
if(top==NULL)
{cout<<"Stack empty";
val=-9999;}
else
{x=top;
top=top->next;
val=x->data;
delete x;}
return(top);
}
void showstack(node *top)
{node *ptr=top;
if(ptr==NULL)
cout<<"Stack empty";
else
while(ptr!=NULL)
{cout<<ptr->data<<" ";
ptr=ptr->next;}
}

void main()
{clrscr();
node *top=NULL;
int val,choice;
char ch;
cout<<"MENU";
cout<<"\n1.push";
cout<<"\n2.pop";
cout<<"\n3.display";
do
{cout<<"\nEnter your choice";
cin>>choice;
switch(choice)
{case 1:cout<<"Enter the value to be pushed ";
cin>>val;
top=push(top,val);
break;
case 2:top=pop(top,val);
if(val!=-9999)
cout<<"Popped value is "<<val;
break;
case 3:showstack(top);
}
cout<<"\nDo you wish to continue(y/n)";
cin>>ch;
}while(ch=='y'||ch=='Y');
}

LINKED QUEUE
#include<iostream.h>
#include<conio.h>
struct node
{int data;
node *next;};
node *add(node *rear,int val)
{node *x=new node;
x->data=val;
x->next=NULL;
if(rear!=NULL)
rear->next=x;
rear=x;
return(rear);
}
node *delet(node *front,int &val)
{node *x;
if(front==NULL)
{cout<<"Q empty";
val=-9999;}
else
{x=front;
front=front->next;
val=x->data;
delete x;}
return(front);
}
void showQ(node *front)
{node *ptr=front;
if(ptr==NULL)
cout<<"Q empty";
else
while(ptr!=NULL)
{cout<<ptr->data<<" ";
ptr=ptr->next;}

}
void main()
{clrscr();
node *front=NULL,*rear=NULL;
int val,choice;
char ch;
cout<<"MENU";
cout<<"\n1.insert";
cout<<"\n2.delete";
cout<<"\n3.display";
do
{cout<<"\nEnter your choice";
cin>>choice;
switch(choice)
{case 1:cout<<"Enter the value to be inserted ";
cin>>val;
rear=add(rear,val);
if(front==NULL)
front=rear;
break;
case 2:front=delet(front,val);
if(val!=-9999)
cout<<"Deleted value is "<<val;
if(front==NULL)
rear=front;
break;
case 3:showQ(front);
}
cout<<"\nDo you wish to continue(y/n)";
cin>>ch;
}while(ch=='y'||ch=='Y');
}

You might also like