Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

Module 2

Linked list
 A linked list is a linear data structure that stores a collection of data elements
dynamically.
 Linked list consist of nodes. Each node consists of two fields, the information stored in a
linked list and a pointer that stores the address of its next node.
 The last node contains null in its second field because it will point to no node.
 A linked list can grow and shrink its size, as per the requirement.
 It does not waste memory space.

Representation of linked list

Node Structure: A node in a linked list typically consists of two components:


Data: It holds the actual value or data associated with the node.
Next Pointer: It stores the memory address (reference) of the next node in the sequence.
Head and Tail: The linked list is accessed through the head node, which points to the first
node in the list. The last node in the list points to NULL or nullptr, indicating the end of the
list. This node is known as the tail node.
Advantages
 Dynamic Data structure: The size of memory can be allocated or de-allocated at
run time based on the operation insertion or deletion.
 Ease of Insertion/Deletion: The insertion and deletion of elements are simpler than
arrays since no elements need to be shifted after insertion and deletion, Just the
address needed to be updated.
 Efficient Memory Utilization: Linked List is a dynamic data structure the size
increases or decreases as per the requirement so this avoids the wastage of memory.
 Implementation: Various advanced data structures can be implemented using a
linked list like a stack, queue, graph, hash maps, etc.

Array V/S Linked List


Array: Arrays store elements in contiguous memory locations, resulting in easily calculable
addresses for the elements stored and this allows faster access to an element at a specific
index.

Data storage scheme of an array


Linked List: Linked lists are less rigid in their storage structure and elements are usually not
stored in contiguous locations, hence they need to be stored with additional tags giving a
reference to the next element.

Linked-List representation

Applications of linked list in computer science:


 Implementation of stacks and queues
 Implementation of graphs: Adjacency list representation of graphs is the most popular
which uses a linked list to store adjacent vertices.
 Dynamic memory allocation: We use a linked list of free blocks.
 Maintaining a directory of names
 Performing arithmetic operations on long integers
 Manipulation of polynomials by storing constants in the node of the linked list
 Representing sparse matrices
Applications of linked list in the real world:
 Image viewer  Robotics
 Previous and next page in a web  Task Scheduling
browser  Image Processing
 Music Player  Undo/Redo Functionality
 GPS navigation systems  Speech Recognition

Representation of linked list in memory


Node Creation

// structure node declaration


struct node
{
int data;
struct node *next;
};

// Creation and initialization of nodes


struct node *head, *ptr;

//Memory allocation of nodes


ptr = (struct node *)malloc(sizeof(struct node *));

Types of linked lists:


There are mainly three types of linked lists:
1. Single-linked list
2. Double linked list
3. Circular linked list
1. Single-linked list:
In a singly linked list, each node contains a reference to the next node in the sequence.
Traversing a singly linked list is done in a forward direction.

Single-linked list

2. Double-linked list:
In a doubly linked list, each node contains references to both the next and previous nodes.
This allows for traversal in both forward and backward directions, but it requires additional
memory for the backward reference.

Double-linked list
3. Circular linked list :
In a circular linked list, the last node points back to the head node, creating a circular
structure. It can be either singly or doubly linked.

Circular linked list

Disadvantages of Linked Lists


 Random Access: Unlike arrays, linked lists do not allow direct access to
elements by index. Traversal is required to reach a specific node.
 Extra Memory: Linked lists require additional memory for storing the pointers,
compared to arrays.

Linked List Operations


The basic linked list operations are:

 Traversal – Access the nodes of the list.


 Insertion – Adds a new node to an existing linked list.
 Deletion – Removes a node from an existing linked list.
 Search – Finds a particular element in the linked list.

Traverse a Linked List


Accessing the nodes of a linked list in order to process it is called traversing a linked list.
Normally we use the traverse operation to display the contents or to search for an element
in the linked list. The algorithm for traversing a linked list is given below.
Algorithm to traverse linked list
STEP 1: SET PTR = HEAD
STEP 2: IF PTR = NULL
WRITE "EMPTY LIST"
GOTO STEP 7
STEP 4: REPEAT STEP 5 AND 6 UNTIL PTR != NULL
STEP 5: PRINT PTR→ DATA
STEP 6: SET PTR = PTR → NEXT
STEP 7: EXIT
We first initialize PTR with the address of HEAD. Now the PTR points to the first node of the
linked list. A while loop is executed, and the operation is continued until PTR reaches the last
node (PTR = NULL). Apply the process(display) to the current node. Move to the next node
by making the value of PTR to the address of next node

Traversing linked list code

struct node *ptr= head;


printf("Elements in the linked list are : ");
while (ptr!= NULL)
{
printf("%d ", temp->data);
ptr= ptr->next;
}
Inserting Elements to a Linked List
A new node can be added to an existing linked list in the following cases.
1. The new node is inserted at the beginning.
2. The new node is inserted at the end.
3. The new node is inserted after a given node.
Insert a Node at the beginning of a Linked list
Consider the linked list shown in the figure. Suppose we want to create a new node with
data 24 and add it as the first node of the list. The linked list will be modified as follow:

 Allocate memory for new node and initialize its DATA part to 24.
 Add the new node as the first node of the list by pointing the NEXT part of the new
node to HEAD.
 Make HEAD to point to the first node of the list.

Algorithm
Step 1: IF PTR = NULL
Write OVERFLOW
Go to Step 7
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR → NEXT
Step 4: SET NEW_NODE → DATA = VAL
Step 5: SET NEW_NODE → NEXT = HEAD
Step 6: SET HEAD = NEW_NODE
Step 7: EXIT
Note that the first step of the algorithm checks if there is enough memory available to create
a new node. The second, and third steps allocate memory for the new node.
C code
struct node *new_node;
new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = 24;
new_node->next = head;
head = new_node;

Insert a Node at the end of a Linked list


Suppose we want to add a new node with data 24 as the last node of the list. Then the
linked list will be modified as follows.
 Allocate memory for new node and initialize its DATA part to 24.
 Traverse to last node.
 Point the NEXT part of the last node to the newly created node.
 Make the value of next part of last node to NULL.

Algorithm
Step 1: IF PTR = NULL Write OVERFLOW
Go to Step 1
Step 2: SET NEW_NODE = PTR
Step 3: SET PTR = PTR - > NEXT
Step 4: SET NEW_NODE - > DATA = VAL
Step 5: SET NEW_NODE - > NEXT = NULL
Step 6: SET PTR = HEAD
Step 7: Repeat Step 8 while PTR - > NEXT != NULL
Step 8: SET PTR = PTR - > NEXT
[END OF LOOP]
Step 9: SET PTR - > NEXT = NEW_NODE
Step 10: EXIT
C code
struct node *new_node;
new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = 24;
new_node->next = NULL;
struct node *temp = head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = new_node;

Insert a Node after a given Node in a Linked list


The last case is when we want to add a new node after a given node. Suppose we want to
add a new node with value 24 after the node having data 9. These changes will be done in
the linked list

 Allocate memory for new node and initialize its DATA part to 24.
 Traverse the list until the specified node is reached.
 Change NEXT pointers accordingly
Algorithm

1) Create the node which is to be inserted, say newnode.


2) If the list is empty, the head will point to the newnode, and we will return.
3) Else, If the list is not empty:

 Make newnode → next = head. This step ensures that the new node is being added
at the beginning of the list.

 Then after that, make head = newnode, as the new node is now the first node of the
list, hence the new head.

C code
Struct node* Newnode;
Newnode=malloc(sizeof(struct node));
Newnode-> data=24;
Struct node *temp=head;
For(i=2;i<pos-1;i++)
{
If(temp->next!=NULL)
Temp=temp->next;
}
Newnode->next=temp->next;
Temp->next=Newnode;

Deleting Elements from a Linked List


Let’s discuss how a node can be deleted from a linked listed in the following cases.
1. The first node is deleted.
2. The last node is deleted.
3. The node after a given node is deleted.
Delete a Node from the beginning of a Linked list
Suppose we want to delete a node from the beginning of the linked list. The list has to be
modified as follows:
 Check if the linked list is empty or not.
 Exit if the list is empty.
 Make HEAD points to the second node. Free the first node from memory.

Algorithm

C code
if(head == NULL)
printf("Underflow");
else
{
ptr = head;
head = head -> next;
free(ptr);
}
Delete last Node from a Linked list
Suppose we want to delete the last node from the linked list. The linked list has to be
modified as follows
 Traverse to the end of the list.
 Change value of next pointer of second last node to NULL.
 Free last node from memory.
Algorithm

Here we use two pointers PTR and PREPTR to access the last node and the second last node.
This can be implemented in C as follows,
if(head == NULL)
printf("Underflow");
else
{
struct node* ptr = head;
struct node* preptr = NULL;
while(ptr->next!=NULL)
{
preptr = ptr;
ptr = ptr->next;
}
preptr->next = NULL;
free(ptr);
}
Delete the Node after a given Node in a Linked list
Suppose we want to delete the that comes after the node which contains data 9
 Traverse the list upto the specified node.
 Change value of next pointer of previous node(9) to next pointer of current node(10)
Algorithm

C code
if(head == NULL)
{
printf("Underflow");
} else
{
struct node* ptr = head;
struct node* preptr = ptr;
while(ptr->data!=num)
{
preptr = ptr; ptr = ptr->next;
}
struct node* temp = ptr;
preptr -> next = ptr -> next;
free(temp);
}
Search
Finding an element is similar to a traversal operation. Instead of displaying data, we have to
check whether the data matches with the item to find.
 Initialize PTR with the address of HEAD.
 Now the PTR points to the first node of the linked list.
 A while loop is executed which will compare data of every node with item. If item has
been found then control goes to last step
Algorithm
Step 1: SET PTR = HEAD
Step 2: Repeat Steps 3 and 4 while PTR != NULL
Step 3: If ITEM = PTR -> DATA
SET POS = PTR
Go To Step 5
ELSE
SET PTR = PTR -> NEXT
Step 4: SET POS = NULL
Step 5: EXIT
C code
struct node* ptr = head;
struct node* pos = NULL;
while (ptr != NULL)
{
if (ptr->data == item)
pos = ptr
printf("Element Found");
break;
else ptr = ptr -> next;
}
STACK
 A Stack is a linear data structure that follows the LIFO (Last-In-First-
Out) principle. Stack has one end.
 It contains only one pointer TOP pointing to the topmost element of the stack.
Whenever an element is added in the stack, it is added on the top of the stack, and the
element can be deleted only from top of the stack.
 a stack can be defined as a container in which insertion and deletion can be done
from the one end known as the top of the stack.
 It is called as stack because it behaves like a real-world stack, piles of books, etc.

Working of Stack

Stack works on the LIFO pattern. As we can observe in the below figure there are five
memory blocks in the stack; therefore, the size of the stack is 5.

Suppose we want to store the elements in a stack and let's assume that stack is empty. We
have taken the stack of size 5 as shown below in which we are pushing the elements one by
one until the stack becomes full.

When we perform the delete operation on the stack, there is only one way for entry and exit
as the other end is closed. It follows the LIFO pattern, which means that the value entered
first will be removed last. In the above case, the value 1 is entered first, so it will be removed
only after the deletion of all the other elements.

Standard Stack Operations


The following are some common operations implemented on the stack:
o push(): When we insert an element in a stack then the operation is known as a push.
If the stack is full then the overflow condition occurs.
o pop(): When we delete an element from the stack, the operation is known as a pop. If
the stack is empty means that no element exists in the stack, this state is known as an
underflow state.
o isEmpty(): It determines whether the stack is empty or not.
o isFull(): It determines whether the stack is full or not.'
o peek(): It returns the element at the given position.
o count(): It returns the total number of elements available in a stack.
o change(): It changes the element at the given position.
o display(): It prints all the elements available in the stack.

PUSH operation
The steps involved in the PUSH operation is given below:
o Before inserting an element in a stack, we check whether the stack is full.
o If we try to insert the element in a stack, and the stack is full, then
the overflow condition occurs.
o When we initialize a stack, we set the value of top as -1 to check that the stack is
empty.
o When the new element is pushed in a stack, first, the value of the top gets
incremented, i.e., top=top+1, and the element will be placed at the new position of
the top.
o The elements will be inserted until we reach the max size of the stack.

Algorithm:
Step-1: If TOP = Max-1
Print “Overflow”
Goto Step 4
Step-2: Set TOP= TOP + 1
Step-3: Set Stack[TOP]= ELEMENT
Step-4: END

POP operation
The steps involved in the POP operation is given below:
o Before deleting the element from the stack, we check whether the stack is empty.
o If we try to delete the element from the empty stack, then the underflow condition
occurs.
o If the stack is not empty, we first access the element which is pointed by the top
o Once the pop operation is performed, the top is decremented by 1, i.e., top=top-1.

Algorithm:
Step-1: If TOP= NULL
Print “Underflow”
Goto Step 4
Step-2: Set VAL= Stack[TOP]
Step-3: Set TOP= TOP-1
Step-4: END

Applications of the Stack

1. A Stack can be used for evaluating expressions consisting of operands and operators.
2. Stacks can be used for Backtracking, i.e., to check parenthesis matching in an
expression.
3. Infix to Postfix /Prefix conversion
4. Redo-undo features at many places like editors, photoshop.
5. Forward and backward features in web browsers
6. Used in many algorithms like Tower of Hanoi, tree traversals, stock span problems ,
and histogram problems.
7. It can be used for systematic Memory Management.
8. Parsing: Stacks can be used to parse input strings, such as HTML or XML code.
9. Function calls: When a function is called, the arguments to the function are pushed
onto a stack. When the function returns, the arguments are popped off the
stack. This allows the function to access its arguments in the order in which they
were passed.

Implementation of stack using array


In array implementation, the stack is formed by using the array. All the operations
regarding the stack are performed using arrays.

#include <stdio.h>
int stack[100],i,j,choice=0,n,top=-1;
void push();
void pop();
void show();
void main ()
{
printf("Enter the number of elements in the stack ");
scanf("%d",&n);
printf("*********Stack operations using array*********");
printf("\n----------------------------------------------\n");
while(choice != 4)
{
printf("Chose one from the below options...\n");
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
show();
break;
case 4:
printf("Exiting....");
break;
default:
printf("Please Enter valid choice ");

};
}
}
void push ()
{
int val;
if (top == n )
printf("\n Overflow");
else
{
printf("Enter the value?");
scanf("%d",&val);
top = top +1;
stack[top] = val;
}
}
void pop ()
{
if(top == -1)
printf("Underflow");
else
top = top -1;
}
void show()
{
for (i=top;i>=0;i--)
printf("%d\n",stack[i]);
if(top == -1)
{
printf("Stack is empty");
}
}
Implementation of stack using Linked list
Instead of using array, we can also use linked list to implement stack. Linked list allocates the
memory dynamically. However, time complexity in both the scenario is same for all the
operations i.e. push, pop and peek.

In linked list implementation of stack, the nodes are maintained non-contiguously in the
memory. Each node contains a pointer to its immediate successor node in the stack. Stack is
said to be overflown if the space left in the memory heap is not enough to create a node.

The top most node in the stack always contains null in its address field.

void push ()
{
int val;
struct node *ptr =(struct node*)malloc(sizeof(struct node));
if(ptr == NULL)
printf("not able to push the element");
else
{
printf("Enter the value");
scanf("%d",&val);
if(head==NULL)
{
ptr->val = val;
ptr -> next = NULL;
head=ptr;
}
else
{
ptr->val = val;
ptr->next = head;
head=ptr;
}
printf("Item pushed");
}
}
void pop()
{
int item;
struct node *ptr;
if (head == NULL)
printf("Underflow");
else
{
item = head->val;
ptr = head;
head = head->next;
free(ptr);
printf("Item popped");
}
}
void display()
{
int i;
struct node *ptr;
ptr=head;
if(ptr == NULL)
printf("Stack is empty\n");
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->val);
ptr = ptr->next;
}
}
}
Expression Parsing in Data Structure
An expression is any word or group of words or symbols that generates a value on evaluation.
Parsing expression means analysing the expression for its words or symbols depending on a
particular criterion.

The way to write arithmetic expression is known as a notation. An arithmetic expression can
be written in three different but equivalent notations, i.e., without changing the essence or
output of an expression. These notations are −

 Infix Notation
 Prefix (Polish) Notation
 Postfix (Reverse-Polish) Notation

Infix Notation

We write expression in infix notation, e.g. a - b + c, where operators are used in-between
operands. It is easy for us humans to read, write, and speak in infix notation but the same
does not go well with computing devices. An algorithm to process infix notation could be
difficult and costly in terms of time and space consumption.

Prefix Notation

In this notation, operator is prefixed to operands, i.e. operator is written ahead of operands.
For example, +ab. This is equivalent to its infix notation a + b. Prefix notation is also known
as Polish Notation.

Postfix Notation

This notation style is known as Reversed Polish Notation. In this notation style, the operator
is postfixed to the operands i.e., the operator is written after the operands. For example, ab+.
This is equivalent to its infix notation a + b.

The following table briefly tries to show the difference in all three notations −

Infix Notation Prefix Notation Postfix Notation

a+b +ab ab+

(a + b) ∗ c ∗+abc ab+c∗

a ∗ (b + c) ∗a+bc abc+∗

a/b+c/d +/ab/cd ab/cd/+

(a + b) ∗ (c + d) ∗+ab+cd ab+cd+∗

((a + b) ∗ c) – d -∗+abcd ab+c∗d-

Conversion of an Infix Expression to Postfix Expression


To convert each expression from infix to postfix, we assign priority to each of the operators
present in the expression. Each operator has its priority for an expression. For example, if we
take some operators, i.e., +, -, *, /, then these will be arranged in priority.
Higher Priority Operators : *, /, %.
Lower Priority Operators : +, -.
Order of Operators : +, −, ∗, /, ^.

Conversion of an Infix Expression into a Postfix Expression


The algorithm below converts an infix expression to a postfix expression.

Push "(" on to the stack, and add ")" to the end of infix expression.
1.
2. Repeat until each character in the infix notation is scanned
 IF a "(" is encountered, push it on the stack.
 IF an operand is encountered, add it to the postfix expression.
 IF a ")" is encountered, then
1. Repeatedly pop from stack and add it to the postfix
expression until a "(" is encountered.
2. Discard the "(" . That is, remove the "(" from stack and do
not add it to the postfix expression.
 IF an operator O is encountered, then
1. Repeatedly pop from stack and add each operator (popped
from the stack) to the postfix expression which has the
same precedence or a higher precedence than O.
2. Push the operator O to the stack.
3. Repeatedly pop from the stack and add it to the postfix expression until
the stack is empty.
Example

Convert the infix expression A + ( B * C ) into postfix expression.

Infix Character Scanned Stack Postfix Expression

A ( A

+ (+ A

( (+( A

B (+( AB

* (+(* AB

C (+(* ABC

) ABC*+
Postfix Evaluation Algorithm
Step 1. Scan the expression from left to right
Step 2. If it is an operand push it to stack
Step 3. If it is an operator pull operand from stack and perform operation
Step 4. Store the output of step 3, back to stack
Step 5. Scan the expression until all operands are consumed
Step 6. Pop the stack and perform operation

Example: Consider the following postfix notation 8 2 3 * 8 + 2 / –. The equivalent infix


expression is
8 – ((2 * 3) + 8) / 2
The following table shows the procedure for evaluating the expression by simulating the
above algorithm.

Symbol Scanned STACK


8 8
2 8, 2
3 8, 2, 3
* 8, 6
8 8, 6, 8
+ 8, 14
2 8, 14, 2
/ 8, 7
– 1

Recursion Using Stack with Example


 A function that calls itself is called a recursive function and this technique is called
recursion.
 A recursive function will call itself until a final call that does not require a call to itself
is made.
 It takes advantage of the system stack to temporarily store the calling function’s
return address and local variables.
 There are two major cases in any recursive solution. They are:
Base case: in which the problem is simple enough to be solved directly without the
need for any more calls to the same function
Recursive case
o The problem at hand is initially subdivided into simpler sub-parts.
o The function calls itself again, but this time with sub-parts of the problem
obtained in the first step.
o The final result is obtained by combining the solutions of simpler sub-parts.

A recursive function is said to be well-defined if it has the following two properties:


o There must be a base criteria in which the function doesn’t call itself.
o Every time the function is called itself, it must be closer to the base
criteria.

Factorial Function
To illustrate recursive functions, consider calculating the factorial of an integer. The product
of the positive integers from 1 to n is called n factorial denoted by n!. To find n!, multiply
the number by the factorial of the number that is one less than the number.
That is, n! = n * (n - 1)!
Assume we need to find the value of 5!.Then,
5! = 5 * 4!, where
4! = 4 * 3!
Therefore,
5! = 5 * 4 * 3!
Expanding further, we get
5! = 5 * 4 * 3 * 2 * 1!
We can now write a recursive function that computes the factorial of a number. Here the base
case is when n = 1
because the result will be 1 as
1! = 1
The recursive case of the factorial function will call itself, but with a smaller value of n, as
factorial(n) = n factorial (n–1).

#include <stdio.h>
int Fact(int);
int main()
{
int num, val;

//read a number from the user


printf("Enter the number: ");
scanf("%d", &num);

//call fact function


val = Fact(num);

//print result
printf("Factorial of %d = %d", num, val);
return 0;
}
//function to calculate factorial
int Fact(int n)
{
if (n == 1)
return 1;
else
return (n * Fact(n-1));
}
OUTPUT
Enter the number: 5
Factorial of 5 = 120

You might also like