2081 2

CSC211 ยท 2081 ยท 10 marks

Define list. How can you use linked list to implement stack? Explain circular linked list.

Define list. How can you use linked list to implement stack? Explain circular linked list.[10]

Data Structures and AlgorithmsChapter 5: ListsStack and Queue as Linked List

Quick answer

A list is a linear data structure that stores a collection of elements in a specific order. Each element in the list has a definite position (first, second, ..., last).

List, Stack Using Linked List, and Circular Linked List


1. Definition of List

There are two main ways to implement a list:

  • Contiguous List (Array-based): A large enough array is defined to hold all items. The first item is placed at position 0, and successive items occupy successive positions. Insertion and deletion can be done anywhere within the list.
  • Linked List: Nodes are connected using pointers. Each node contains a data field (info) and a pointer field (next) that points to the next node. This is called a self-referential structure. The NULL value of the next field of the last node indicates the end of the list.

2. Implementing Stack Using Linked List

A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. The two primary operations are:

  • PUSH - Insert an element at the top
  • POP - Remove an element from the top

In a linked list implementation, the head (start) pointer acts as the TOP of the stack.

Node Structure

struct node {
    int info;
    struct node *next;
};
typedef struct node NodeType;
NodeType *top = NULL;

PUSH Operation (Insert at top)

Algorithm PUSH(item):
1. Create a new node:
       newnode = (NodeType*) malloc(sizeof(NodeType))
2. If newnode == NULL then
       Print "Stack Overflow"
       Return
3. Set newnode->info = item
4. Set newnode->next = top
5. Set top = newnode
6. End.

Diagram:

Before PUSH(30):    top -> [20] -> [10] -> NULL
After  PUSH(30):    top -> [30] -> [20] -> [10] -> NULL

POP Operation (Delete from top)

Algorithm POP():
1. If top == NULL then
       Print "Stack Underflow"
       Return
2. Set temp = top
3. Set item = temp->info
4. Set top = top->next
5. Free(temp)
6. Return item
7. End.

Diagram:

Before POP():    top -> [30] -> [20] -> [10] -> NULL
After  POP():    top -> [20] -> [10] -> NULL   (30 is returned)

Advantages of Linked List over Array for Stack

FeatureArray-based StackLinked List Stack
SizeFixed (static)Dynamic (grows/shrinks)
MemoryMay waste memoryUses only needed memory
OverflowPossibleOnly on memory exhaustion

3. Circular Linked List

Definition

A circular linked list is a list where the link field (next) of the last node points back to the very first node of the list, instead of pointing to NULL.

Circular linked lists can be used to help traverse the same list again and again if needed. A circular list is very similar to the linear list where the pointer of the last node points not to NULL but to the first node.

Diagram:

start
  |
  v
[10] -> [20] -> [30] -> [40]
  ^_______________________________|

C Representation

struct node {
    int info;
    struct node *next;
};
typedef struct node NodeType;
NodeType *start = NULL;
NodeType *last  = NULL;

Identifying the First Node

In a circular linked list, there are two methods to know if a node is the first node:

  1. An external pointer start (or list) points to the first node.
  2. A header node is placed as the first node of the circular list. The header node can be separated from others by:
    • Having a sentinel (dummy) value as the info part, OR
    • Having a dedicated flag variable to specify if the node is a header node.

Inserting a Node at the Beginning

Algorithm INSERT_BEGINNING(item):
1. newnode = (NodeType*) malloc(sizeof(NodeType))
2. If start == NULL then
       newnode->info = item
       newnode->next = newnode      // points to itself
       start = newnode
       last  = newnode
   End if
3. Else
       newnode->info  = item
       newnode->next  = start       // new node points to old first node
       start          = newnode     // update start to new node
       last->next     = newnode     // last node points to new first node
   End else
4. End.

Inserting a Node at the End

Algorithm INSERT_END(item):
1. newnode = (NodeType*) malloc(sizeof(NodeType))
2. If start == NULL then
       newnode->info = item
       newnode->next = newnode      // points to itself
       start = newnode
       last  = newnode
   End if
3. Else
       newnode->info  = item
       last->next     = newnode     // old last node points to new node
       last           = newnode     // update last pointer
       last->next     = start       // new last node points to start
   End else
4. End.

Advantages of Circular Linked List

  • Any node can be a starting point; the entire list can be traversed from any node.
  • Useful in round-robin scheduling, circular queues, and multiplayer games.
  • No need to check for NULL while traversing (stop when you reach the start again).

Summary Table:

FeatureLinear Linked ListCircular Linked List
Last node points toNULLFirst node
TraversalOne direction, stops at NULLCan loop continuously
Use caseGeneral lists, stacksRound-robin, circular queues
End detectionnext == NULLnext == start
Deleting the first nodeOnly start is updatedstart and last->next must both be updated
Reaching a node from any other nodeNot always possibleAlways possible

4. Conclusion

A list is an ordered collection of elements that can be held either contiguously in an array or as a chain of nodes joined by pointers. The linked representation is the natural one for a stack, because both PUSH and POP touch only the head of the chain and therefore run in O(1) time, while the stack grows and shrinks with the actual number of items instead of being limited by a fixed array size. A circular linked list changes only one pointer, the next of the last node, so that it addresses the first node, and that single change makes endless traversal from any starting node possible, which is why it is the structure behind round robin CPU scheduling and circular queues.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.