16-Module-2 Linear Data Structures-10-Mar-2021Material I 10-Mar-2021 Stack Using LL
16-Module-2 Linear Data Structures-10-Mar-2021Material I 10-Mar-2021 Stack Using LL
# 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()*/
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;
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()*/
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;
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() */