Program of STACK AND QUEUE Using Linked List
Program of STACK AND QUEUE Using Linked List
# include<stdio.h>
# include<malloc.h>
# include<stdlib.h>
# include<conio.h>
void push();
void pop();
void display();
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
getch();
return 0;
}/*End of main() */
void push()
{
list tmp;
int pushed_item;
tmp = (list)malloc(sizeof(node));
printf("Input the new value to be pushed on the stack : ");
scanf("%d",&pushed_item);
tmp->data=pushed_item;
tmp->next=top;
top=tmp;
}/*End of push()*/
void pop()
{
list tmp;
int n;
if(top == NULL)
printf("Stack is empty\n");
else
{ tmp=top;
n=tmp->data;
printf("Popped item is %d\n",n);
top=top->next;
free(tmp);
}
}/*End of pop()*/
void display()
{ list ptr;
ptr=top;
if(top==NULL)
printf("Stack is empty\n");
else
{
printf("Stack elements :\n");
while(ptr!= NULL)
{
printf("%d\n",ptr->data);
ptr = ptr->next;
}/*End of while */
}/*End of else*/
}/*End of display()*/
int main()
{
int choice;
while(1)
{ printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default :
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
void insert()
{
list tmp;
int added_item;
tmp = (list)malloc(sizeof(node));
printf("Input the element for adding in queue : ");
scanf("%d",&added_item);
tmp->data = added_item;
tmp->next=NULL;
if(front==NULL) /*If Queue is empty*/
front=tmp;
else
rear->next=tmp;
rear=tmp;
}/*End of insert()*/
void del()
{
list tmp;
if(front == NULL)
printf("Queue Underflow\n");
else
{
tmp=front;
printf("Deleted element is %d\n",tmp->data);
front=front->next;
free(tmp);
}
}/*End of del()*/
void display()
{
list ptr;
ptr = front;
if(front == NULL)
printf("Queue is empty\n");
else
{
printf("Queue elements :\n");
while(ptr != NULL)
{
printf("%d ",ptr->data);
ptr = ptr->next;
}
printf("\n");
}/*End of else*/
}/*End of display()*/