Stack and Queue Implementation of Linked List in C++

Please refer to this link for more detail explanation.

(A) Stack Implementation of Linked list
Algorithm

1. Declare and initialize necessary variables such as struct node *top, *p, top = NULL
2. For push operation,
– check for memory full
if((p=(nodetype*) malloc (sizeof(nodetype))) == NULL)
print “Memory Exhausted”
Else
-take data to be inserted say x
-Create an empty node p and assign data x to its info field
i.e. p->info = x;
p->next = NULL
if(top!=NULL)
p->next = top
3. For next push operation, goto step 2.
4. For pop operation,
if top = NULL
print “stack is empty”
else
-temp = top
-top = top->next
-display popped item as top->info
-delete temp
5. For next pop operation goto step 4

Source Code

(B) Queue Implementation of Linked List

Algorithm

1. Declare and initialize necessary variables such as struct node *front, *rear etc
2. For enqueue operation,
-take input data to be inserted
-create an empty node and assign data to its into field
i.e. p->info=data
p->next = NULL
if front = NULL
front = p
else
rear->next = p
-rear = p
3. For next enqueue operation, goto step 2
4. For dequeue operation,
if front = NULL
print “Queue is empty”
else
-create a node pointer temp
-temp = front
-front = front->next
-display dequeued item as temp->data
-delete temp
5. For dequeue of next data item, goto step 4

Source Code

SHARE Stack and Queue Implementation of Linked List in C++

You may also like...

3 Responses

  1. Anonymous says:

    This comment has been removed by a blog administrator.

  2. joinwitraj says:

    Easily understandable one. Thanks for the Code

  3. Anonymous says:

    This comment has been removed by a blog administrator.

Leave a Reply

Your email address will not be published. Required fields are marked *

Share