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

Program of STACK AND QUEUE Using Linked List

This document contains C code to implement a stack and queue using linked lists. For the stack, it defines functions to push, pop and display elements. It uses a top pointer to track the head of the list. For the queue, it defines functions to insert, delete and display elements. It uses front and rear pointers to track the head and tail of the queue. The main function provides a menu to call these functions and test the data structures.

Uploaded by

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

Program of STACK AND QUEUE Using Linked List

This document contains C code to implement a stack and queue using linked lists. For the stack, it defines functions to push, pop and display elements. It uses a top pointer to track the head of the list. For the queue, it defines functions to insert, delete and display elements. It uses front and rear pointers to track the head and tail of the queue. The main function provides a menu to call these functions and test the data structures.

Uploaded by

pd03487
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

/* Program of stack using linked list*/

# include<stdio.h>
# include<malloc.h>
# include<stdlib.h>
# include<conio.h>
void push();
void pop();
void display();

typedef struct node_type


{
int data;
struct node_type *next;
}node;
typedef node *list;
list top=NULL;
int main()
{
int choice;
while(1)
{ printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ") ;
scanf("%d", &choice);

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()*/

/* Program of queue using linked list*/


# include<stdio.h>
# include<malloc.h>
#include<stdlib.h>
void insert();
void del();
void display();

typedef struct node_type


{
int data;
struct node_type *next;
}node;

typedef node *list;


list rear=NULL;
list front=NULL;

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()*/

You might also like