0% found this document useful (0 votes)
10 views12 pages

Expt 4 Ds

Uploaded by

konuriyash
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)
10 views12 pages

Expt 4 Ds

Uploaded by

konuriyash
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/ 12

AGNEL INSTITUTE OF TEC HNOLOGY & DESIGN

PROGRAMS : LINKED LIST IMPLEMENTATION OF STACK

#include<stdio.h>

#include<stdlib.h>

struct node

int info;

struct node *link;

}*top=NULL;

void push(int item);

int pop();

int peek();

int isEmpty();

void display();

main()

int choice,item;

while(1)

printf("\n1.Push\n");

printf("2.Pop\n");

printf("3.Display the top element\n");

printf("4.Display all the stack elements\n");

printf("5.Quit\n");

printf("\nEnter your choice:");

scanf("%d",&choice);

switch(choice)

case 1:

printf("Enter the item to be pushed:");


Data Structures & Algorithms , Department of Computer Engineering RollNo. 23CO76
scanf("%d",&item);

push(item);
AGNEL INSTITUTE OF TEC HNOLOGY & DESIGN

printf("Enter the item to be pushed:");

scanf("%d",&item);

push(item);

break;

case 2:

item=pop();

printf("Popped item is:%d\n",item);

break;

case 3:

printf("Item at the top is:%d\n",peek());

break;

case 4:

display();

break;

case 5:

exit(1);

default:

printf("Wrong choice\n");

void push(int item)

struct node *tmp;

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

if(tmp==NULL) RollNo. 23CO 76


Data Structures & Algorithms , Department of Computer Engineering
{

printf("Stack Overflow\n");
AGNEL INSTITUTE OF TEC HNOLOGY & DESIGN

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

if(tmp==NULL)

printf("Stack Overflow\n");

return;

tmp->info=item;

tmp->link=top;

top=tmp;

int pop()

struct node *tmp;

int item;

if(isEmpty())

printf("Stack Underflow\n");

exit(1);

tmp=top;

item=tmp->info;

top=top->link;

free(tmp);

return item;

int peek()

if(isEmpty())

printf("Stack Underflow\n");
Data Structures & Algorithms , Department of Computer Engineering RollNo. 23CO 76
exit(1);

}
AGNEL INSTITUTE OF TEC HNOLOGY & DESIGN

printf("Stack Underflow\n");

exit(1);

return top->info;

int isEmpty()

if(top==NULL)

return 1;

else

return 0;

void display()

struct node *ptr;

ptr=top;

if(isEmpty())

printf("Stack is empty\n");

return;

printf("Stack elements :\n");

while(ptr!=NULL)

printf("%d\n",ptr->info);

ptr=ptr->link;

printf("\n");

Data Structures & Algorithms , Department of Computer Engineering RollNo. 23CO 76


AGNEL INSTITUTE OF TECHNOLOGY & DESIGN

Input and Output:

Data Structures & Algorithms, Department of Computer Engineering RollNo. 23CO76


AGNEL INSTITUTE OF TECHNOLOGY & DESIGN

Data Structures & Algorithms, Department of Computer Engineering RollNo. 23CO 76


AGNEL INSTITUTE OF TEC HNOLOGY & DESIGN

PROGRAM: LINKED LIST IMPLEMENTATION OF QUEUE

#include<stdio.h>

#include<stdlib.h>

struct node

int info;

struct node *link;

}*front=NULL,*rear=NULL;

void insert(int item);

int del();

int peek();

int isEmpty();

void display();

main()

int choice,item;

while(1)

printf("\n1.Insert\n");

printf("2.Delete\n");

printf("3.Display element at the front\n");

printf("4.Display all elements of the queue\n");

printf("5.Quit\n");

printf("\nEnter your choice:");

scanf("%d",&choice);

switch(choice)

case 1:

printf("Input the element for adding in queue:");


Data Structures & Algorithms , Department of Computer Engineering RollNo. 23CO 76
scanf("%d",&item);

insert(item);
AGNEL INSTITUTE OF TEC HNOLOGY & DESIGN

printf("Input the element for adding in queue:");

scanf("%d",&item);

insert(item);

break;

case 2:

item=del();

printf("Deleted element is:%d\n",item);

break;

case 3:

printf("Element at the front is:%d\n",peek());

break;

case 4:

display();

break;

case 5:

exit(1);

default:

printf("Wrong choice\n");

void insert(int item)

struct node *tmp;

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

return item;
Data Structures & Algorithms , Department of Computer Engineering RollNo. 23CO 76
}
AGNEL INSTITUTE OF TEC HNOLOGY & DESIGN

if(tmp==NULL)

printf("Memory not available\n");

return;

tmp->info=item;

tmp->link=NULL;

if(front==NULL)

front=tmp;

else

rear->link=tmp;

rear=tmp;

int del()

struct node *tmp;

int item;

if(isEmpty())

printf("Queue Underflow\n");

exit(1);

tmp=front;

item=tmp->info;

front=front->link;

free(tmp);

return item;

int peek()

if(isEmpty())
Data Structures & Algorithms , Department of Computer Engineering RollNo. 23CO 76
{

printf("Queue Underflow\n");
AGNEL INSTITUTE OF TEC HNOLOGY & DESIGN
if(isEmpty())

printf("Queue Underflow\n");

exit(1);

return front->info;

int isEmpty()

if(front==NULL)

return 1;

else

return 0;

void display()

struct node *ptr;

ptr=front;

if(isEmpty())

printf("Queue is empty\n");

return;

printf("Queue elements :\n\n");

while(ptr!=NULL)

printf("%d ",ptr->info);

ptr=ptr->link;

printf("\n\n");

Data Structures & Algorithms , Department of Computer Engineering RollNo. 23CO 76


AGNEL INSTITUTE OF TEC HNOLOGY & DESIGN

Input and Output:

Data Structures & Algorithms , Department of Computer Engineering RollNo. 23CO 76


AGNEL INSTITUTE OF TEC HNOLOGY & DESIGN

Data Structures & Algorithms , Department of Computer Engineering RollNo. 23CO 76

You might also like