Queue
#include<iostream.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int data;
struct node *link;
};
int main()
{
clrscr();
struct node *newnode,*ptr,*pptr,*start=NULL;
int ch,count=0,pos;
start=NULL;
while(1)
{
//view of queue
clrscr();
count=0;
ptr=start;
while(ptr!=NULL)
{
cout<<"\n["<<ptr->data<<"]";
ptr=ptr->link;
count++;
}
cout<<"\n\nTotal "<<count<<" nodes\n\n";
cout<<"\n MENU\n______\n\n";
cout<<"1. Push\n2. Pop\n0. EXIT\n\nchoice: ";
cin>>ch;
switch(ch)
{
case 1:
newnode=(node*)malloc(sizeof(node));
if(newnode==NULL)
{
cout<<"\nout of memory\n\npress any key to continue..";
getch();
break;
}
cout<<"\n\nEnter the data:\n";
cin>>newnode->data;
if(start==NULL)
{
start=newnode;
start->link=NULL;
break;
}
newnode->link=start;
start=newnode;
break;
case 2:
if(start==NULL)
{
cout<<"\nUnderflow\n\nPress any key to continue..";
getch();
break;
}
pptr=NULL;
ptr=start;
while(ptr->link!=NULL)
{
pptr=ptr;
ptr=ptr->link;
}
if(pptr==NULL)
{
start=NULL;
}
pptr->link=NULL;
free(ptr);
break;
case 0://exit
//freeing all the memory before exit
while(start!=NULL)
{
ptr=start;
start=start->link;
free(ptr);
}
return 0;
}//end of switch
}//end of while
}//end of main
Comments
Post a Comment