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

16-Module-2 Linear Data Structures-10-Mar-2021Material I 10-Mar-2021 Stack Using LL

This document contains code for implementing different data structures using linked lists in C programming language. It includes code for implementing a stack, queue and priority queue using linked lists. For the stack, it contains functions to push, pop and display elements. For the queue, functions to insert, delete and display elements are included. The priority queue code allows inserting elements based on priority and deleting the highest priority element.

Uploaded by

mir zain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

16-Module-2 Linear Data Structures-10-Mar-2021Material I 10-Mar-2021 Stack Using LL

This document contains code for implementing different data structures using linked lists in C programming language. It includes code for implementing a stack, queue and priority queue using linked lists. For the stack, it contains functions to push, pop and display elements. For the queue, functions to insert, delete and display elements are included. The priority queue code allows inserting elements based on priority and deleting the highest priority element.

Uploaded by

mir zain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

/* Stack using linked list*/

# include<stdio.h>
# include<conio.h>

struct node
{
int info;
struct node *link;
}*top=NULL;

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();
}/*End of main() */

push()
{
struct node *tmp;
int item;
tmp = (struct node *)malloc(sizeof(struct node));
printf("Input the new value to be pushed on the stack : ");

scanf("%d",&item);
tmp->info=item;
tmp->link=top;
top=tmp;
}/*End of push()*/
pop()
{
struct node *tmp;

if(top == NULL)
printf("Stack is empty\n");
else
{
tmp=top;
printf("Popped item is %d\n",tmp->info);

top=top->link;
free(tmp);
}

}/*End of pop()*/

display()
{
struct node *ptr;
ptr=top;
if(top==NULL)
printf("Stack is empty\n");

else
{
printf("Stack elements :\n");
while(ptr!= NULL)
{
printf("%d\n",ptr->info);
ptr = ptr->link;
}/*End of while */
}/*End of else*/

}/*End of display()*/

Queue Using Linked List

/* Program of queue using linked list*/


# include<stdio.h>
# include<conio.h>

struct node
{
int info;
struct node *link;
}*front=NULL,*rear=NULL;

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()*/
insert()
{
struct node *tmp;
int item;

tmp = (struct node *)malloc(sizeof(struct node));


printf("Input the element for adding in queue : ");
scanf("%d",&item);
tmp->info = item;
tmp->link=NULL;
if(front==NULL) /*If Queue is empty*/

front=tmp;
else
rear->link=tmp;
rear=tmp;
}/*End of insert()*/

del()
{
struct node *tmp;
if(front == NULL)
printf("Queue Underflow\n");
else

{
tmp=front;
printf("Deleted element is %d\n",tmp->info);
front=front->link;
free(tmp);
}
}/*End of del()*/

display()

{
struct node *ptr;
ptr = front;
if(front == NULL)
printf("Queue is empty\n");
else

{
printf("Queue elements :\n");
while(ptr != NULL)
{
printf("%d ",ptr->info);
ptr = ptr->link;

}
printf("\n");
}/*End of else*/
}/*End of display()*/

Priority Queue Using Linked List


# include<stdio.h>
# include<conio.h>

struct node
{
int priority;
int info;
struct node *link;

}*front = NULL;

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

insert()
{
struct node *tmp,*q;
int added_item,item_priority;
tmp = (struct node *)malloc(sizeof(struct node));
printf("Input the item value to be added in the queue : ");

scanf("%d",&added_item);
printf("Enter its priority : ");
scanf("%d",&item_priority);
tmp->info = added_item;
tmp->priority = item_priority;
/*Queue is empty or item to be added has priority more than first

item*/
if( front == NULL || item_priority < front->priority )
{
tmp->link = front;
front = tmp;
}
else
{
q = front;

while( q->link != NULL && q->link->priority <= item_priority )


q=q->link;
tmp->link = q->link;
q->link = tmp;
}/*End of else*/
}/*End of insert()*/

del()
{
struct node *tmp;
if(front == NULL)
printf("Queue Underflow\n");

else
{
tmp = front;
printf("Deleted item is %d\n",tmp->info);
front = front->link;
free(tmp);

}
}/*End of del()*/

display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
printf("Queue is empty\n");

else
{
printf("Queue is :\n");
printf("Priority Item\n");
while(ptr != NULL)
{

printf("%5d %5d\n",ptr->priority,ptr->info);
ptr = ptr->link;
}
}/*End of else */
}/*End of display() */

You might also like