0% found this document useful (0 votes)
49 views10 pages

Linked List

The document provides an overview of linked lists, detailing their types including singly linked lists, doubly linked lists, circular linked lists, and doubly circular linked lists, along with their operations such as insertion, deletion, searching, and displaying elements. It also discusses the applications of linked lists in dynamic data storage, efficient insertion/deletion, and various data structures. Additionally, the document covers stacks and queues, explaining their operations and applications in computer science.

Uploaded by

vikasjaat90675
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)
49 views10 pages

Linked List

The document provides an overview of linked lists, detailing their types including singly linked lists, doubly linked lists, circular linked lists, and doubly circular linked lists, along with their operations such as insertion, deletion, searching, and displaying elements. It also discusses the applications of linked lists in dynamic data storage, efficient insertion/deletion, and various data structures. Additionally, the document covers stacks and queues, explaining their operations and applications in computer science.

Uploaded by

vikasjaat90675
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

Linked List

A linked list is a kind of linear dynamic data structure which we use to store data elements.
Arrays are also a type of linear data structure where the data items are stored in continuous
memory blocks.

Unlike arrays, the linked list does not need to store data elements in contiguous memory regions
or blocks.

Types of Linked Lists

Following are the types of linked lists in C++:

1. Singly Linked List

The singly linked list is the simplest form of linked list in which the node contain two members data
and a next pointer that stores the address of the next node. Each node is a singly linked list is
connected through the next pointer and the next pointer of the last node points to NULL denoting
the end of the linked list. The following diagram describes the structure of a singly linked list:

2. Doubly Linked List

The doubly linked list is the modified version of the singly linked list where each node of the doubly
linked consists of three data members data ,next and prev. The prev is a pointer that stores the
address of the previous node in the linked list sequence. Each node in a doubly linked list except
the first and the last node is connected with each other through the prev and next pointer. The
prev pointer of the first node and the next pointer of the last node points to NULL in the doubly
linked list. The following diagram describes the structure of a doubly linked list:
3. Circular Linked List

The circular linked list is almost same as the singly linked list but with a small change. In a circular
linked list the next pointer of the last node points to the first node of the linked list rather than
pointing to NULL, this makes this data structure circular in nature which is used in various
applications like media players. The following diagram describes the structure of a circular linked
list:

4. Doubly Circular Linked List

The doubly circular linked list is a combination of a doubly linked list and a circular linked list. In a
doubly circular linked list the prev pointer of the first node points to the last node and the next
pointer of the last node points to the first node. The main advantage of a doubly circular linked list
is that we can access the last node of the linked list in constant time through the first node. The
following diagram describes the structure of a doubly circular linked list:
1. Linked List

Insert at Beginning

1. Step 1: Create a new node with the given data.


2. Step 2: Check if the head of the list is null (empty list).
3. Step 3: If the list is empty, set the head to the new node.
4. Step 4: If the list is not empty, point the new node's next to the current head.
5. Step 5: Update the head pointer to the new node.
6. Step 6: The new node is now at the beginning of the list.
7. Step 7: If necessary, update the tail (if you are maintaining a tail pointer).
8. Step 8: Exit the function.

Insert at End

1. Step 1: Create a new node with the given data.


2. Step 2: Check if the head is null (empty list).
3. Step 3: If the list is empty, make the new node the head of the list.
4. Step 4: If the list is not empty, traverse the list until the last node (node where next is
null).
5. Step 5: Set the next pointer of the last node to the new node.
6. Step 6: Set the new node's next pointer to null.
7. Step 7: If maintaining a tail pointer, update it to point to the new node.
8. Step 8: Exit the function.

Delete from Beginning

1. Step 1: Check if the list is empty (head is null).


2. Step 2: If the list is empty, return an error message ("List is empty").
3. Step 3: If the list is not empty, store the current head node in a temporary pointer.
4. Step 4: Set the head pointer to the next node of the current head.
5. Step 5: If the head becomes null after deletion, the list is now empty.
6. Step 6: Delete the temporary node that was the old head.
7. Step 7: If necessary, update the tail pointer to null if the list is now empty.
8. Step 8: Exit the function.
Delete from End

1. Step 1: Check if the list is empty (head is null).


2. Step 2: If the list is empty, return an error message ("List is empty").
3. Step 3: If the list has one element, set the head to null and delete the node.
4. Step 4: If the list has more than one node, traverse the list to find the second-to-last
node.
5. Step 5: Set the next pointer of the second-to-last node to null.
6. Step 6: Delete the last node.
7. Step 7: If necessary, update the tail pointer to the second-to-last node.
8. Step 8: Exit the function.

Search

1. Step 1: Check if the list is empty (head is null).


2. Step 2: If the list is empty, return false (item not found).
3. Step 3: Initialize a temporary pointer to the head of the list.
4. Step 4: Traverse the list and compare the data of each node with the target value.
5. Step 5: If a match is found, return true (item found).
6. Step 6: If no match is found by the end of the list, return false (item not found).
7. Step 7: If necessary, return the node containing the value.
8. Step 8: Exit the function.

Display

1. Step 1: Check if the list is empty (head is null).


2. Step 2: If the list is empty, display a message ("List is empty").
3. Step 3: Initialize a temporary pointer to the head of the list.
4. Step 4: Traverse the list, printing each node's data followed by an arrow (->).
5. Step 5: If the list is empty, ensure it prints an appropriate message.
6. Step 6: When the last node is reached, print null to indicate the end of the list.
7. Step 7: Format the output neatly (e.g., space-separated).
8. Step 8: Exit the function.

APPLICTAION OF LINKED LIST:

1. Dynamic Data Storage

Linked lists are ideal for scenarios where the amount of data is unknown at compile time and
can grow or shrink during program execution. Unlike arrays, they don’t require a fixed size.

2. Efficient Insertion/Deletion

Linked lists allow efficient insertion and deletion of elements at any position (especially at the
beginning or middle), without shifting elements like in arrays.
3. Implementation of Abstract Data Types

They are used to implement abstract data structures like stacks, queues, deques, and graphs,
due to their flexible node-based structure.

4. Polynomial Arithmetic

Each term of a polynomial expression (e.g., 4x3+3x2+2x+14x3+3x2+2x+1) can be represented as


a node in a linked list, making operations like addition and multiplication efficient.

5. Navigation Systems

Doubly linked lists are used in navigation systems like browsers and document viewers, where
forward and backward movement between states is needed.

6. Memory Management (Free Lists)

Operating systems and memory managers use linked lists to track free memory blocks or
allocate/deallocate memory chunks dynamically.

7. Real-Time Task Scheduling

Linked lists are used in real-time systems (like embedded systems) to manage scheduled
tasks or interrupts in a time-sensitive queue.

8. Symbol Table in Compilers

Compilers use linked lists to manage symbol tables and scope management during parsing
and semantic analysis.

9. File System Directories

Many file systems use linked lists to manage directory structures, where each file or folder
entry points to the next.

10. Sparse Matrix Representation

In matrices where most elements are zero, only non-zero elements are stored in a linked list,
reducing memory usage and computation time.

11. Music/Video Playlist

Each song or video can be a node, and the playlist allows moving forward or backward using
next and previous pointers.

12. Undo/Redo Functionality


Using a doubly linked list, actions can be undone or redone by moving back and forth in the
list.

Stack is the fundamental data structures used in the computer science to the store collections of
the objects. It can operates on the Last In, First Out (LIFO) principle where the most recently added
the object is the first one to be removed. It can makes the stacks highly useful in the situations
where you to the reverse a series of the operations or repeatedly undo operations.

Stack Data Structure in C++

A stack can be visualized as the vertical stack of the elements, similar to the stack of the plates.
We can only add or remove the top plate. Similarly, in the stack data structures, elements can
added to and removed from the top of the stack.

Stacks can be implemented using the arrays or linked lists:

1. Array-based implementation: It can uses the simple array to the store the elements of the
stack. The pointer is used to the keep of the top of the stack.
2. Linked List based implementation: Each element in the stack is the node in a linked list. The
top of the stack is simply the head of the linked list.

Types of Stack:

Fixed Size Stack : As the name suggests, a fixed size stack has a fixed size and cannot grow or
shrink dynamically. If the stack is full and an attempt is made to add an element to it, an
overflow error occurs. If the stack is empty and an attempt is made to remove an element
from it, an underflow error occurs.
Dynamic Size Stack : A dynamic size stack can grow or shrink dynamically. When the stack is
full, it automatically increases its size to accommodate the new element, and when the stack
is empty, it decreases its size. This type of stack is implemented using a linked list, as it allows
for easy resizing of the stack.

Basic Operations on Stack:

In order to make manipulations in a stack, there are certain operations provided to us.

push() to insert an element into the stack


pop() to remove an element from the stack
top() Returns the top element of the stack.
isEmpty() returns true if stack is empty else false.
isFull() returns true if the stack is full else false.

Push

1. Step 1: Check if the stack is full (if using a fixed-size stack).


2. Step 2: If the stack is full, return an error ("Stack Overflow").
3. Step 3: Create a new element with the given data.
4. Step 4: Insert the new element at the top of the stack.
5. Step 5: Update the stack's top pointer to the new element.
6. Step 6: Increase the stack size (if dynamic).
7. Step 7: Ensure that the stack operates within the allocated memory size.
8. Step 8: Exit the function.

Pop

1. Step 1: Check if the stack is empty.


2. Step 2: If the stack is empty, return an error ("Stack Underflow").
3. Step 3: Store the current top element in a temporary variable.
4. Step 4: Update the top pointer to point to the next element in the stack.
5. Step 5: Remove the temporary variable.
6. Step 6: If the stack becomes empty, reset the top pointer to null.
7. Step 7: Return the popped element's data.
8. Step 8: Exit the function.

Peek

1. Step 1: Check if the stack is empty.


2. Step 2: If the stack is empty, return an error ("Stack is empty").
3. Step 3: Return the data of the top element without removing it.
4. Step 4: Do not modify the stack's top pointer.
5. Step 5: If needed, check for overflow or underflow conditions.
6. Step 6: Display the top element.
7. Step 7: Ensure no changes to the stack state.
8. Step 8: Exit the function.

IsEmpty

1. Step 1: Check if the stack is empty.


2. Step 2: If the stack is empty, return true.
3. Step 3: If the stack is not empty, return false.
4. Step 4: Ensure the stack size is tracked properly.
5. Step 5: If the stack is dynamic, check if any elements are left.
6. Step 6: Handle edge cases where the top pointer is corrupted.
7. Step 7: Return the result.
8. Step 8: Exit the function.

Display

1. Step 1: Check if the stack is empty.


2. Step 2: If the stack is empty, display "Stack is empty".
3. Step 3: Start at the top of the stack.
4. Step 4: Traverse the stack, printing each element's data.
5. Step 5: Print the elements in order from top to bottom.
6. Step 6: Format the output appropriately.
7. Step 7: If needed, separate elements by commas or spaces.
8. Step 8: Exit the function.

APPLICATIONS OF STACK

1. Function Call Management:


In programming languages, stacks are used to manage function calls using a call stack. It
stores return addresses, parameters, and local variables.
2. Expression Evaluation:
Stacks help evaluate arithmetic expressions, especially postfix (Reverse Polish Notation)
and prefix expressions.
3. Expression Conversion:
Converting infix expressions to postfix or prefix requires stacks.
4. Undo Mechanism in Text Editors:
Each action is pushed onto a stack, and undo operations pop the last action to revert it.
5. Backtracking Algorithms:
Used in puzzles like Sudoku, maze solving, and N-Queens, where decisions are pushed
and popped as paths are explored and backtracked.
6. Syntax Parsing:
Compilers use stacks for parsing syntax and checking for balanced parentheses or tags.
7. Browser History:
Web browsers use stacks to manage the history of visited pages (back button
functionality).
8. Memory Management:
Stacks are used to allocate memory for variables and function calls dynamically in the
runtime environment.
9. Reversing Data:
A stack can reverse strings, arrays, or linked lists by pushing and then popping elements.
10. Depth-First Search (DFS):
DFS algorithm in graphs and trees uses a stack (either explicit or system call stack) to
keep track of nodes to visit.

✅ Algorithm to Convert Infix to Postfix


Input: Infix expression (e.g., A + B * C)
Output: Postfix expression (e.g., A B C * +)

Step-by-step Algorithm:

1. Initialize:
An empty stack for operators.
An empty output list for the postfix expression.
2. Scan the infix expression left to right, one symbol at a time:
3. For each symbol:
Operand (A–Z or 0–9):
➤ Add it directly to the output.
Left Parenthesis (:
➤ Push it onto the stack.
Right Parenthesis ):
➤ Pop and output from the stack until a left parenthesis ( is encountered.
➤ Pop and discard the (.
Operator (+, -, *, /, ^):
➤ While the top of the stack has the same or higher precedence and is not (,
pop from the stack to the output.
➤ Then push the current operator onto the stack.
4. After scanning the entire infix expression:
➤ Pop all remaining operators from the stack to the output.

Introduction to Queue Data Structure

Queue is a linear data structure that follows FIFO (First In First Out) Principle, so the first element inserted is
the first to be popped out.

FIFO Principle in Queue:

FIFO Principle states that the first element added to the Queue will be the first one to be removed or
processed. So, Queue is like a line of people waiting to purchase tickets, where the first person in line
is the first person served. (i.e. First Come First Serve).

3. Queue

Enqueue

1. Step 1: Check if the queue is full (if using a fixed-size queue).


2. Step 2: If the queue is full, return an error ("Queue Overflow").
3. Step 3: Create a new element with the given data.
4. Step 4: Add the new element to the rear of the queue.
5. Step 5: Update the rear pointer to point to the newly added element.
6. Step 6: If the queue is dynamic, ensure the memory can accommodate the new element.
7. Step 7: If necessary, update the front pointer (if circular).
8. Step 8: Exit the function.

Dequeue

1. Step 1: Check if the queue is empty.


2. Step 2: If the queue is empty, return an error ("Queue Underflow").
3. Step 3: Store the current front element in a temporary variable.
4. Step 4: Move the front pointer to the next element in the queue.
5. Step 5: Delete the temporary element.
6. Step 6: If the queue is now empty, reset the rear pointer to null.
7. Step 7: Return the dequeued element's data.
8. Step 8: Exit the function.

Front

1. Step 1: Check if the queue is empty.


2. Step 2: If the queue is empty, return an error ("Queue is empty").
3. Step 3: Return the data of the front element without removing it.
4. Step 4: Do not modify the queue’s front pointer.
5. Step 5: Display the front element's data.
6. Step 6: Ensure no changes to the queue's structure.
7. Step 7: Handle edge cases where the queue has one element.
8. Step 8: Exit the function.

IsEmpty

1. Step 1: Check if the queue is empty.


2. Step 2: If the queue is empty, return true.
3. Step 3: If the queue has elements, return false.
4. Step 4: Ensure the queue size is correctly tracked.
5. Step 5: If the queue is dynamic, check the memory for any remaining elements.
6. Step 6: Return the result based on the queue state.
7. Step 7: If needed, handle circular queues and their conditions.
8. Step 8: Exit the function.

Display

1. Step 1: Check if the queue is empty.


2. Step 2: If the queue is empty, display "Queue is empty".
3. Step 3: Start at the front of the queue.
4. Step 4: Traverse the queue, printing each element's data.
5. Step 5: Format the output properly.
6. Step 6: Print elements from front to rear.
7. Step 7: If needed, separate the elements by commas or spaces.
8. Step 8: Exit the function.

You might also like