Algorithm and Flowchart for Implementing a Stack using Linked List

[41720 views]




Stack

Stack is a linear data structure which follows LIFO(Last In First Out) or FILO(First In Last Out) order to perform its functions. It can be implemented either by using arrays or linked lists. Algorithm for Stack Using Linked List


Basic operations are performed in the stack are:
  1. Push: It adds an item in the stack. If the stack is full, then the stack is said to be in Overflow condition.
  2. Pop: It deletes an item from the stack. The items are popped out in the reverse order in which they were pushed in. If the stack is empty, then it is said to be in Underflow condition i.e no more items can be deleted.
  3. Peek or Top: It returns the top element of the stack.
  4. isEmpty: It returns true if stack is empty, otherwise false.

Linked List

A linked list is a linear data structure, but unlike arrays the elements are not stored at contiguous memory locations, the elements in a linked list are linked to each other using pointers. It consists of nodes where, each node contains a data field and a link to the next node.

Flowchart for Implementing a Stack using Linked List:

1. Push Operation:

Flowchart for Push Operation for a Stack using Linked List
Remove WaterMark from Above Flowchart

2. Pop Operation:

Flowchart for Pop Operation for a Stack using Linked List
Remove WaterMark from Above Flowchart

3. Peek Operation:

Flowchart for Peek Operation for a Stack using Linked List
Remove WaterMark from Above Flowchart

Algorithm for Implementing a Stack using Linked List:

1. PUSH() Operation:

Step 1: Start Step 2: Create a node new and declare variable top Step 3: Set new data part to be Null // The first node is created, having null value and top pointing to it Step 4: Read the node to be inserted. Step 5: Check if the node is Null, then print "Insufficient Memory" Step 6: If node is not Null, assign the item to data part of new and assign top to link part of new and also point stack head to new.

2. POP() Operation:

Step 1: Start Step 2: Check if the top is Null, then print "Stack Underflow." Step 3: If top is not Null, assign the top's link part to ptr and assign ptr to stack_head's link part. Step 4: Stop

3. PEEK() Operation:

Step 1: Start Step 2: Print or store the node pointed by top variable Step 3: Stop

In the above algorithm,

  1. We first create a node with value Null.
  2. Then when we have to push an element new we check if new is not null otherwise insertion cannot be done. We then feed Item into data part of new and assign top to its link and top is updated to the new value. And thus, an element is inserted.
  3. For popping we check first if the item is not null, otherwise stack is empty i.e underflow condition. The pointer is then made to point the next element and st_head link is assigned to the pointer's value. This eliminates the element from the link list. Thus, popping the element
  4. Peek value is stored or printed by returning node at which top is pointing
  5. Stop
                 



Want to Learn How to write own Algorithm and Flowcharts



Want to test your logical skills in Algorithms?




Comments

2 comment
  • Pravallika

    Queue using Linked list ?

  • Sakshi Bawane

    Well!










Search Anything:

Sponsored Deals ends in





Technical Quizzes Specially For You:

Search Tags

    Pseudocode for Implementing a Stack using Linked List

    Flowchart for Implementing a Stack using Linked List