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

Stack and Queuelinked List

The document describes how to implement a stack and queue as linked lists in C. For a stack, it shows algorithms to push and pop elements onto and from the stack by adding and removing nodes from the front of a linked list. For a queue, it shows algorithms to enqueue and dequeue elements by adding to the rear and removing from the front of the linked list. Both data structures use pointers to traverse and connect the nodes in the linked list.

Uploaded by

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

Stack and Queuelinked List

The document describes how to implement a stack and queue as linked lists in C. For a stack, it shows algorithms to push and pop elements onto and from the stack by adding and removing nodes from the front of a linked list. For a queue, it shows algorithms to enqueue and dequeue elements by adding to the rear and removing from the front of the linked list. Both data structures use pointers to traverse and connect the nodes in the linked list.

Uploaded by

Bishal Shahi
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Stack as Linked List

struct stack
{
Int data;
struct stack *next;
};
struct stack *top;
top=NULL;
Algorithm: PUSH
Let *top be the pointer to the first node and *newnode be another
pointer of structure type
1. Start
2. Create a new node using malloc() function.
i.e newnode=(struct stack*)malloc(size of(struct stack));
3. Read the number say num
4. Assign num to data field
i.e newnode->data=num;
5. Check the top pointer. If it points to NULL then
Set: top pointer to newnode
i.e top=newnode;
6. otherwise:
Set: pointer field of newnode to top
i.e newnode->next=top
Set: top pointer to newnode
i.e top=newnode;
7. stop
Algorithm: POP
Let *top be the pointer to the first node and *temp be the
pointer variable of structure type.
1. Start
2. Check the top pointer and if it is NULL then display the
message: list does not exist and exit.
i.e if (top==NULL)
print: stack does not exist and exit
3. Store address of first node to temp
i.e temp=top
4. Set top to ptr field of top
i.e top=top->next
5. display: poped item is temp->data
6. Free the memory reserved by temp
7. stop
Display the Content
1. Start
2. If top== NULL then
Display: stack is empty and exit.
3. set: temp=top
4. While temp!=NULL
Display: data field of temp
Set: temp=temp->next
5. stop
Queue as Linked List
struct queue
{
int data;
struct queue *next;
};
struct queue *front, *rear;
front=NULL;
rear=NULL;
Algorithm: ENQUEUE
1. Start
2. Create a newnode using malloc()
i.e. newnode=(struct queue*)malloc(sizeof(struct queue));
3. Read a number say num
4. set: newnode->data=num
5. set: newnode->next=NULL
5. If rear==NULL then,
Set: front=rear=newnode
Else:
Set: rear->next=newnode
rear=newnode
6. stop
Algorithm: DEQUEUE
1. Start
2. If front==NULL then
Display: queue is empty and exit.
Else
Set: temp=front
Display: deleted item is: temp->data
Set: front=front->next
free temp
3. stop
Display the Content
1. Start
2. If front== NULL then
Display: queue is empty and exit.
3. set: temp=front
4. While temp!=NULL
Display: data field of temp
Set: temp=temp->next
5. stop

You might also like