Data Structures Unit - I Notes
Data Structures Unit - I Notes
data next
10 pointer
Node
Each “Node" contains two fields: Data field and Next field.
Data field is used to store actual value of the node
Next field is used to store the address of next node in the list.
TYPES OF LINKED LISTS
There are three common types of Linked List.
1. Single Linked List
2. Double Linked List
3. Circular Linked List
SINGLE LINKED LIST
Single linked list is a sequence of elements in which every element has link to its next
element in the sequence.
In any single linked list, the individual element is called as "Node". Every "Node" contains
two fields, data field, and the next field.
The data field is used to store actual value of the node and next field is used to store the
address of next node in the sequence.
Step 2:If head=NULL,make newnod as head and head as temp(head=ptr and temp=head)
data next
10 NULL
head temp 1000
Step 2:Make head as temp(temp=head) and traverse to the last node of the list(temp=temp->next)
data next data next data next data next
10 2000 20 3000 30 NULL 40 NULL
head 1000 2000 temp 3000 ptr 4000
Example
Program
int insert_pos()
{
struct node *ptr;
int pos,i=1,num;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
ptr->data=num;
printf("Enter position to insert:");
scanf("%d",&pos);
if(head==NULL)
{
ptr->next=NULL;
head=ptr;
}
else
{
struct node *temp;
temp=head;
while(i<pos-1)
{
temp=temp->next;
i++;
}
ptr->next=temp->next;
temp->next=ptr;
}
return 0;
}
3. Deletion
In a single linked list, the deletion operation can be performed in three ways. They are as
follows.
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
Deleting from Beginning of the list
Algorithm
Step 1 : Check whether list is Empty (head == NULL)
Step 2 : If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
Step 3 : If it is Not Empty then, define a Node pointer 'temp' and initialize with head.
Step 4 : Check whether list is having only one node (temp → next == NULL)
Step 5 : If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions)
Step 6 : If it is FALSE then set head = temp → next, and delete temp.
Step 6 : If it is FALSE then set head = temp → next, and delete temp.
Example
else
{
struct node *temp,*temp1;
temp=head;
if(temp->next==NULL)
{
head=NULL;
printf("Deleted element is %d",temp->data);
free(temp);
}
else
{
while(temp->next!=NULL)
{
temp1=temp;
temp=temp->next;
}
temp1->next=NULL;
printf("Deleted element is %d",temp->data);
free(temp);
}
}
}
Deleting a Specific Node from the list
Algorithm
Step 1 : Check whether list is Empty (head == NULL)
Step 2 : If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
Step 3 : If it is Not Empty then, define two Node pointers 'temp' and 't' and initialize 'temp' with
head.
Here, 'link1' field is used to store the address of the previous node in the sequence, 'link2' field is
used to store the address of the next node in the sequence and 'data' field is used to store the
actual value of that node.
Example
Important points to be Remembered
In double linked list, the first node must be always pointed by head.
Always the previous field of the first node must be NULL.
Always the next field of the last node must be NULL.
Basic structure of a Double Linked List
Each node of a singly linked list follows a common basic structure.
struct node
{
int data;
struct node *previous;
struct node *next;
};
OPERATIONS ON DOUBLE LINKED LIST
In a double linked list, we perform the following operations...
1. Create
2. Insertion
3. Deletion
4. Display
5. Searching
1. Creation
Algorithm
Step 1 - Create a newNode with given value, Say "ptr".
Step 2 - Check whether list is Empty (head == NULL).
Step 3 - If it is Empty then, set head = ptr and define a Node pointer 'temp' and initialize with
'head'.
Step 4 - If it is Not Empty then, set temp->next=ptr, ptr->previous=temp and temp =ptr.
Program
void create()
{
struct node *ptr,*temp;
int i,n,val;
printf("Enter Number of Elements :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter the data of node %d: ", i);
scanf("%d",&val);
ptr->data=val;
ptr->previous=NULL;
ptr->next=NULL;
if(head==NULL)
{
head=ptr;
temp=head;
}
else
{
temp->next=ptr;
ptr->previous=temp;
temp=ptr;
}
}
}
2. Insertion
In a double linked list, the insertion operation can be performed in three ways as follows...
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
In single linked list, every node points to its next node in the sequence and the last node points
NULL. But in circular linked list, every node points to its next node in the sequence but the last
node points to the first node in the list.
OPERATIONS OF CIRCULAR LINKED LIST
1. Creation
2. Insertion
3. Deletion
5. Searching
1. Creation
Algorithm
Step 1 - Create a newNode with given value, Say "ptr".
Step 2- Check whether list is Empty (head == NULL).
Step 3- If it is Empty then, set head = ptr and define a Node pointer 'temp' and initialize with
'head'.
Step 4- If it is Not Empty then, set temp->next=ptr, temp=ptr and temp->next=head.
Program
void create()
{
struct node *ptr,*temp;
int i,n,val;
printf("Enter Number of Elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter the data of node %d: ", i);
scanf("%d",&val);
ptr->data=val;
ptr->next=NULL;
if(head==NULL)
{
head=ptr;
temp=head;
}
else
{
temp->next=ptr;
temp=ptr;
temp->next=head;
}
}
}
2. Insertion
In a circular linked list, the insertion operation can be performed in three ways. They are as
follows.
1. Inserting At Beginning of the list
2. Inserting At End of the list
3. Inserting At Specific location in the list
Inserting At Beginning of the list
Algorithm
Step 1 - Create a newNode with given value , Say " ptr ".
Step 4 - If it is Not Empty then, define a Node pointer 'temp' and initialize with 'head'.
Step 5 - Keep moving the 'temp' to its next node until it reaches to the last node
Step 6 - Set 'ptr → next =head', 'head = ptr' and 'temp → next = head'.
Program
void insert_beg()
{
struct node *ptr,*temp;
int num;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
ptr->data=num;
if(head==NULL)
{
head=ptr;
ptr->next=head;
}
else
{
temp=head;
if(temp->next==head)
{
temp->next=ptr;
ptr->next=temp;
}
else
{
while(temp->next!=head)
{
temp=temp->next;
}
ptr->next=head;
head=ptr;
temp->next=head;
}
}
}
Inserting At End of the list
Algorithm
Step 1 - Create a newNode with given value.
Step 2 - Check whether list is Empty (head == NULL).
Step 3 - If it is Empty then, set head = ptr and ptr → next = head.
Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list
(until temp → next == head).
Step 6 - Set temp → next = ptr and ptr → next = head.
Program
void insert_end()
{
struct node *ptr,*temp;
int num;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
ptr->data=num;
if(head==NULL)
{
head=ptr;
ptr->next=head;
}
else
{
temp=head;
if(temp->next==head)
{
temp->next=ptr;
ptr->next=temp;
}
else
{
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=ptr;
ptr->next=head;
}
}
}
Inserting At Specific location in the list (After a Node)
Algorithm
Step 1 - Create a newNode with given value,Say ' ptr '.
Step 2 - Check whether list is Empty (head == NULL)
Step 3 - If it is Empty then, set head = ptr and ptr->next=head.
Step 4 - If it is Not Empty then, define a node pointer temp and initialize with head.
( temp=head )
Step 5 - Keep moving the temp to its next node until it reaches to the node after which we want
to insert the newNode
Step 6 - Finally, Set 'ptr → next = temp→ next' and 'temp → next = ptr'
Program
int insert_pos()
{
struct node *ptr;
int pos,i=1,num;
ptr=(struct node*)malloc(sizeof(struct node));
printf("Enter data:");
scanf("%d",&num);
ptr->data=num;
printf("Enter position to insert:");
scanf("%d",&pos);
if(head==NULL)
{
head=ptr;
ptr->next=head;
}
else
{
struct node *temp;
temp=head;
while(i<pos-1)
{
temp=temp->next;
i++;
}
ptr->next=temp->next;
temp->next=ptr;
}
return 0;
}
3. Deletion
In a circular linked list, the deletion operation can be performed in three ways those are as
follows...
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
Deleting from Beginning of the list
Algorithm
Step 1 - Check whether list is Empty (head == NULL)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the
function.
Step 3 - If it is Not Empty then, define two Node pointers 'temp' and 'temp1' and initialize both
'temp' and 'temp1' with head.
Step 4 - Check whether list is having only one node (temp → next == head)
Step 5 - If it is TRUE then set head = NULL and delete temp (Setting Empty list conditions)
Step 6 - If it is FALSE move the temp until it reaches to the last node.
(until temp →next == head )
Step 7 - Then set head = temp1 → next, temp → next = head and delete temp1.
Program
void delete_beg()
{
if(head==NULL)
{
printf("List is Empty!!! Deletion is not possible\n");
}
else
{
struct node *temp,*temp1;
temp=head;
temp1=head;
if(temp->next==head)
{
head=NULL;
printf("Deleted element is %d",temp->data);
free(temp);
}
else
{
while(temp->next!=head)
{
temp=temp->next;
}
head=temp1->next;
temp->next=head;
printf("Deleted element is %d",temp1->data);
free(temp1);
}
}
}
Stack Representation
1
STACK TERMINOLOGY
Stacksize : Stack size represents the maximum size of stack.
Top : Top represents Top of Stack ( TOP ). The stack top is used to check stack overflow or underflow
conditions. Initially top value must be -1. [ TOP= -1 ]
Stack Overflow : When a stack is completely full, it is said to be Stack Overflow.
Stack Underflow (Or ) Empty : When a stack is completely empty, it is said to be Stack Underflow
(or ) Stack Empty.
Example
If we want to create a stack by inserting 10,45,12,16,35 and 50. Then 10 becomes the bottom-most
element and 50 is the topmost element. The last inserted element 50 is at Top of the stack as shown in
the image below...
2
STACK IMPLEMENTATIONS
Stack data structure can be implemented in two ways. They are as follows...
1. Stack Using Array
2. Stack Using Linked List
STACK USING ARRAY
A stack data structure can be implemented using a one-dimensional array. But stack implemented
using array stores only a fixed number of data values.
Just define a one dimensional array of specific size and insert or delete the values into that array
by using LIFO principle with the help of a variable called 'top'. Initially, the top is set to -1.
Whenever we want to insert a value into the stack, increment the top value by one and then insert.
Whenever we want to delete a value from the stack, then delete the top value and decrement the
top value by one.
Stack Operations Using Array
1. Push
2. Pop
3. Display
1. Push
In a stack, push() is a function used to insert an element into the stack.
Whenever we want to insert a value into the stack, increment the top value by one and then
insert.
In a stack, the new element is always inserted at top position.
Algorithm
Step 1 : Check whether stack is FULL. (top == SIZE-1)
Step 2 : If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and
Step 3 : If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value
(stack[top] = value).
3
Program
void push( )
{
int value;
if(top==stacksize–1 )
{
printf("\nStack is Full!!! Insertion is not possible!!!");
}
else
{
printf(“Enter the element to be inserted”);
scanf(“%d”,&value);
top=top+1;
stack[top]=value;
}
}
Example
4
2. Pop
In a stack, pop() is a function used to delete an element from the stack.
Whenever we want to delete a value from the stack, then delete the top value and decrement the
top value by one.
In a stack, the element is always deleted from top position.
Algorithm
Step 1 : Check whether stack is EMPTY. (top == -1)
Step 2 : If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not possible!!!" and
Terminate the function.
Step 3 : If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--).
Program
void pop( )
{
if(top==–1)
{
printf(“ Stack is Empty!!! Deletion is not possible!!!”);
}
else
{
printf("\nDeleted : %d", stack[top]);
top--;
}
}
Example
5
3. Display
In a stack , display() is a function to Displays the elements of a Stack
Algorithm
Step 1 : Check whether stack is EMPTY. (top == -1)
Step 2 : If it is EMPTY, then display "Stack is EMPTY!!!" and terminate the function.
Step 3 : If it is NOT EMPTY, then define a variable 'i' and initialize with top. Display stack[i] value and
Program
void display()
{
int i;
if(top==-1)
printf("\nStack is Empty!!!");
else
{
printf("\nStack elements are:\n");
for(i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
}
}
6
WRITE A C PROGRAM TO IMPLEMENT STACK USING ARRAY
#include<stdio.h>
#include<process.h>
#define SIZE 10
void push();
void pop();
void display();
int stack[SIZE], top = -1;
int main()
{
int choice;
while(1)
{
printf("\n\n***** MENU *****\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
return 0;
}
7
void push( )
{
int value;
if(top==SIZE-1)
printf("\nStack is Full!!! Insertion is not possible!!!");
else
{
printf("Enter the value to be insert: ");
scanf("%d",&value);
top++;
stack[top] = value;
printf("\nInsertion success!!!");
}
}
void pop()
{
if(top == -1)
printf("\nStack is Empty!!! Deletion is not possible!!!");
else
{
printf("\nDeleted : %d", stack[top]);
top--;
}
}
void display()
{
int i;
if(top == -1)
printf("\nStack is Empty!!!");
else
{
printf("\nStack elements are:\n");
for(i=top; i>=0; i--)
{
printf("%d\n",stack[i]);
}
}
}
8
STACK USING LINKED LIST
The stack implemented using linked list can work for an unlimited number of values.
That means, stack implemented using linked list works for the variable size of data. So, there is no
need to fix the size at the beginning of the implementation.
The Stack implemented using linked list can organize as many data values as we want.
In linked list implementation of a stack, every new element is inserted as 'top' element.
That means every newly inserted element is pointed by 'top'.
Whenever we want to remove an element from the stack, simply remove the node which is
pointed by 'top' by moving 'top' to its previous node in the list.
The next field of the first element must be always NULL.
Example
In the above example, the last inserted node is 90 and the first inserted node is 20. The order of
elements inserted is 20, 40,60 and 90.
9
Stack Operations Using Linked List
1. Push
2. Pop
3. Display
1. Push
Algorithm
Step 1 : Create a newNode( ptr ) with given value.
Step 2 : Check whether stack is Empty (top == NULL)
Step 3 : If it is Empty, then set ptr → next = NULL.
Step 4 : If it is Not Empty, then set ptr → next = top.
Step 5 : Finally, set top = ptr.
Program
void push()
{
struct node *ptr;
int value;
ptr = (struct node*)malloc(sizeof(struct node));
printf("Enter the value to be insert: ");
scanf("%d", &value);
ptr->data = value;
if(top == NULL)
{
ptr->next = NULL;
}
else
{
ptr->next = top;
}
top = ptr;
printf("\nInsertion is Success!!!\n");
}
10
Example
2.Pop
Algorithm
Step 1 - Check whether stack is Empty (top == NULL).
Step 2 - If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and
Step 3 - If it is Not Empty, then define a node pointer 'temp' and set it to 'top'.(temp=top)
11
Program
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!Deletion is not possible!\n");
else
{
struct node *temp;
temp=top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
Example
12
3.Display
Algorithm
Step 1 : Check whether stack is Empty (top == NULL).
Step 2 : If it is Empty, then display 'Stack is Empty!!!' and terminate the function.
Step 3 : If it is Not Empty, then define a node pointer 'temp' and initialize with top.
Step 4 : Display 'temp → data --->' and move it to the next node. Repeat the same until temp
reaches to the first node in the stack. (temp → next != NULL).
Step 5 : Finally! Display 'temp → data ---> NULL'.
Program
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else
{
struct node *temp;
temp=top;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
13
WRITE A C PROGRAM TO IMPLEMENT STACK USING LINKED LIST
#include<stdio.h>
#include<stdlib.h>
#include<process.h>
struct node
{
int data;
struct node *next;
};
struct node *top = NULL;
void push();
void pop();
void display();
int main()
{
int choice, value;
printf("Stack using Linked List\n");
while(1)
{
printf("\n\n***** MENU *****\n");
printf("1. Push\n");
printf("2. Pop\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
return 0;
}
14
void push()
{
struct node *ptr;
int value;
ptr = (struct node*)malloc(sizeof(struct node));
printf("Enter the value to be insert: ");
scanf("%d", &value);
ptr->data = value;
if(top == NULL)
{
ptr->next = NULL;
}
else
{
ptr->next = top;
}
top = ptr;
printf("\nInsertion is Success!!!\n");
}
void pop()
{
if(top == NULL)
printf("\nStack is Empty!!!Deletion is not possible!\n");
else
{
struct node *temp;
temp=top;
printf("\nDeleted element: %d", temp->data);
top = temp->next;
free(temp);
}
}
void display()
{
if(top == NULL)
printf("\nStack is Empty!!!\n");
else
{
struct node *temp;
temp=top;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
15
QUEUE ADT
WHAT IS A QUEUE?
Queue is a linear data structure in which the insertion and deletion operations are performed at
two different ends.
In a queue data structure, adding and removing elements are performed at two different
positions.
The insertion is performed at one end and deletion is performed at another end.
In a queue data structure, the insertion operation is performed at a position which is known as
'rear'.
In a queue data structure, the deletion operation is performed at a position which is known as
'front'.
In queue data structure, the insertion and deletion operations are performed based on FIFO
(First In First Out) principle.
In a queue data structure, the insertion operation is performed using a function called
"enQueue()".
In a queue data structure, the deletion operation is performed using a function called
"deQueue()".
Real Life Examples Of Queue
A queue of people at ticket-window: The person who comes first gets the ticket first. The
person who is coming last is getting the tickets in last. Therefore, it follows first-in-first-out
(FIFO) strategy of queue.
Vehicles on toll-tax bridge: The vehicle that comes first to the toll tax booth leaves the booth
first. The vehicle that comes last leaves last. Therefore, it follows first-in-first-out (FIFO)
strategy of queue.
16
QUEUE IMPLEMENTATIONS
The queue implemented using array stores only fixed number of data values.
Just define a one dimensional array of specific size and insert or delete the values into that array
by using FIFO (First In First Out) principle with the help of variables 'front' and 'rear'.
Whenever, we want to insert a new value into the queue, increment 'rear' value by one and then
Whenever we want to delete a value from the queue, then delete the element which is at 'front'
Example
17
Queue Operations Using Array
1. Enqueue
2. Dequeue
3. Display
1. Enqueue
In a queue data structure, enQueue() is a function used to insert a new element into the queue.
In a queue, the new element is always inserted at rear position.
The enQueue() function Whenever we want to insert a value into the queue, increment the rear
value by one and then insert.
Algorithm
Step 1 : Check whether queue is FULL. (rear == SIZE-1)
Step 2 : If it is FULL, then display "Queue is FULL!!! Insertion is not possible!!!" and terminate the
function.
Step 3 : If it is NOT FULL, then increment rear value by one (rear++) and set queue[rear] = value.
Program
void enQueue()
{
int value;
if(rear == SIZE-1)
{
printf("\nQueue is Full!!! Insertion is not possible!!!");
}
else
{
if(front == -1)
{
front = 0;
}
printf("Enter the value to be insert: ");
scanf("%d",&value);
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}
}
18
Example
2. Dequeue
In a queue data structure, deQueue() is a function used to delete an element from the queue.
In a queue, the element is always deleted from front position.
Algorithm
Step 1 : Check whether queue is EMPTY. (rear==-1 || front > rear )
Step 2 : If it is EMPTY, then display "Queue is EMPTY!!! Deletion is not possible!!!" and
terminate the function.
Step 3 : If it is NOT EMPTY, then increment the front value by one (front ++). Then display
queue[front] as deleted element.
19
Program
void deQueue()
{
if(rear== -1 || front > rear)
{
printf("\nQueue is Empty!!! Deletion is not possible!!!");
}
else
{
printf("\nDeleted : %d", queue[front]);
front++;
}
}
Example
20
3. Display
Algorithm
Step 1 : Check whether queue is EMPTY. (rear==-1 || front > rear )
Step 2 : If it is EMPTY, then display "Queue is EMPTY!!!" and terminate the function.
Step 3 : If it is NOT EMPTY, then define an integer variable 'i' and set 'i = front'.
Step 4 : Display 'queue[i]' value and increment 'i' value by one (i++).
Repeat the same until 'i' value reaches to rear (i <= rear)
Program
void display()
{
if(rear== -1 || front > rear)
{
printf("\nQueue is Empty!!!");
}
else
{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
{
printf("%d\t",queue[i]);
}
}
}
21
WRITE A C PROGRAM TO IMPLEMENT QUEUE USING ARRAY
#include<stdio.h>
#include<process.h>
#define SIZE 10
void enQueue( );
void deQueue();
void display();
int queue[SIZE],front=-1,rear=-1;
int main()
{
int choice;
while(1)
{
printf("\n\n***** QUEUE USING ARRAY MENU *****\n");
printf("1.enQueue\n");
printf("2.deQueue\n");
printf("3.Display\n");
printf("4.Exit\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: enQueue();
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
return 0;
22
}
void enQueue()
{
int value;
if(rear==SIZE-1)
{
printf("\nQueue is Full!!! Insertion is not possible!!!");
}
else
{
if(front==-1)
{
front=0;
}
printf("Enter the value to be insert: ");
scanf("%d",&value);
rear++;
queue[rear]=value;
printf("\nInsertion success!!!");
}
}
void deQueue()
{
if(rear==-1||front>rear)
{
printf("\nQueue is Empty!!! Deletion is not possible!!!");
}
else
{
printf("\nDeleted : %d",queue[front]);
front++;
}
}
23
void display()
{
if(rear==-1||front>rear)
{
printf("\nQueue is Empty!!!");
}
else
{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
{
printf("%d\t",queue[i]);
}
}
}
QUEUE USING LINKED LIST
The queue which is implemented using a linked list can work for an unlimited number of values.
That means, queue using linked list can work for the variable size of data (No need to fix the size at
the beginning of the implementation).
The Queue implemented using linked list can organize as many data values as we want.
In linked list implementation of a queue, the last inserted node is always pointed by 'rear' and the
first node is always pointed by 'front'.
Example
In above example, the last inserted node is 50 and it is pointed by 'rear' and the first inserted node is
10 and it is pointed by 'front'. The order of elements inserted is 10, 15, 22 and 50.
24
Queue Operations Using Linked List
1. Enqueue
2. Dequeue
3. Display
1. Enqueue
Algorithm
Step 1 : Create a newNode(ptr) with given value and set 'ptr → next' to NULL.
Step 2 : Check whether queue is Empty (rear == NULL)
Step 3 : If it is Empty then, set front = ptr and rear = ptr.
Step 4 : If it is Not Empty then, set rear → next = ptr and rear = ptr.
Program
void enQueue()
{
struct node *ptr;
int value;
ptr = (struct node*)malloc(sizeof(struct node));
printf("Enter the value to be insert: ");
scanf("%d", &value);
ptr->data = value;
ptr -> next = NULL;
if(rear == NULL)
{
front=ptr;
rear=ptr;
}
else
{
rear -> next = ptr;
rear = ptr;
}
printf("\nInsertion is Success!!!\n");
}
25
Example
2. Dequeue
Algorithm
Step 2 : If it is Empty, then display "Queue is Empty!!! Deletion is not possible!!!" and
Step 3 : If it is Not Empty then, define a Node pointer 'temp' and set it to 'front'.
Step 4 : Then set 'front = front → next' and delete 'temp' (free(temp)).
26
Program
void deQueue()
{
if(front == NULL)
printf("\nQueue is Empty!!!Deletion is not possible!\n");
else
{
struct node *temp;
temp=front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}
}
Example
27
3. Display
Algorithm
Step 2 : If it is Empty then, display 'Queue is Empty!!!' and terminate the function.
Step 3 : If it is Not Empty then, define a Node pointer 'temp' and initialize with front.
Step 4 : Display 'temp → data --->' and move it to the next node. Repeat the same until
Program
void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct node *temp;
temp=front;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);
}
}
28
WRITE A C PROGRAM TO IMPLEMENT QUEUE USING LINKED LIST
#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front = NULL,*rear = NULL;
void enQueue();
void deQueue();
void display();
int main()
{
int choice;
while(1)
{
printf("\n\n***** QUEUE USING LINKED LIST MENU *****\n");
printf("1. enQueue\n");
printf("2. deQueue\n");
printf("3. Display\n");
printf("4. Exit\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: enQueue();
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
return 0;
}
29
void enQueue()
{
struct node *ptr;
int value;
ptr = (struct node*)malloc(sizeof(struct node));
printf("Enter the value to be insert: ");
scanf("%d", &value);
ptr->data = value;
ptr -> next = NULL;
if(front == NULL)
{
front=ptr;
rear=ptr;
}
else
{
rear -> next = ptr;
rear = ptr;
}
printf("\nInsertion is Success!!!\n");
}
void deQueue()
{
if(front == NULL)
printf("\nQueue is Empty!!!Deletion is not possible!\n");
else
{
struct node *temp;
temp=front;
front = front -> next;
printf("\nDeleted element: %d\n", temp->data);
free(temp);
}
}
void display()
{
if(front == NULL)
printf("\nQueue is Empty!!!\n");
else
{
struct node *temp;
temp=front;
while(temp->next != NULL)
{
printf("%d--->",temp->data);
temp = temp -> next;
}
printf("%d--->NULL",temp->data);} }
30
WHAT IS AN EXPRESSION?
An expression is a collection of operators and operands that represents a specific value.
EXPRESSION REPRESENTATIONS
An expression can be represented in various forms such as
1. Infix Notation
2. Prefix (Polish) Notation.
3. Postfix (Reverese-Polish) Notation.
1. Infix Notation: if the operator is used in between the operands are called Infix Notation.
Syntax : <operand> <operator> <operand>
Example : a + b
2. Prefix (Polish) Notation: If the operator is used before operands are called Prefix notation.
Syntax : <operator> <operand> <operand>
Example : + ab
3. Postfix (Reverse-Polish) Notation: If the operator is used after operands are called Postfix
notation.
Syntax : <operand> <operand><operator>
Example : ab+
ORDER OF PRECEDENCE
Highest precedence : Exponential ( ^ or ↑ )
STACK APPLICATIONS
CONVERSION OF EXPRESSION
1. Conversion of Infix expression to Postfix expression.
4. Balancing of Symbols
1
1. CONVERSION OF INFIX EXPRESSION TO POSTFIX EXPRESSION
Steps required for conversion of Infix to Postfix expression
1. Read an Expression from left to right.
a)If the stack is Empty, then push operator into the stack.
b)If the stack is not Empty, check the priority of the operator
i ) If scan operator is higher priority than the top of stack operator then scanned
ii) If scan operator is same or lower priority than the top of stack operator then pop the
operator from the stack and add to postfix expression and scanned operator will
5. If the character is RIGHT PARANTHESIS, then pop all the operators from the stack until it reaches
6. After reading all characters, if stack is not empty then pop and ADD to postfix expression.
2 + Push '+' + A
2
Example 2 : Convert A + ( B * C ) to postfix expression.
2 + Push '+' + A
3 ( Push '(' +( A
B Add to postfix 'B' +( AB
4
3
Example 4 : Convert ( A + B ) * C - ( D - E ) * ( F + G ) to postfix expression.
4
2. CONVERSION OF INFIX EXPRESSION TO PREFIX EXPRESSION
Steps required for conversion of Infix to Prefix expression
1. Read an Expression from left to right.
2. Reverse the input string.
3. If the character is OPERAND, ADD to the prefix expression.
4. If the character is OPERATOR, check whether stack is empty or not.
a)If the stack is Empty, then push operator into the stack.
b)If the stack is not Empty, check the priority of the operator
i ) If scan operator is same or higher priority than the top of stack , scanned
ii) If scan operator is lower priority than the top of stack , pop the operator from the
stack and add to prefix expression and scanned operator will push on stack,
Repeat step 4.
5. If the character is CLOSING PARANTHESIS, then push operator into the stack.
6. If the character is OPEN PARANTHESIS, then pop all the operators from the stack until it reaches
CLOSING PARANTHESIS and ADD to prefix expression.pop and discard the closing parenthesis.
7. After reading all characters, if stack is not empty then pop and ADD to prefix expression
8. Reverse the output string.
Example 1 : Convert A + B to prefix expression.
Solution : Reverse the input string : A + B = B + A
5
Example 2 : Convert A + ( B * C ) to prefix expression.
Solution : Reverse the input string : A + ( B * C ) = ) C * B ( + A
S.No Scanned Character Operation Stack Prefix Expression
6
Example 4 : Convert ( A + B ) * C - ( D - E ) * ( F + G ) to Prefix expression.
7
3. POSTFIX EXPRESSION EVALUATION
Postfix Expression : If the operator is used after operands are called Postfix expression.
To evaluate a postfix expression using Stack data structure we can use the following steps.
3. If the character is OPERATOR, POP top two operands from the stack perform calculation and PUSH
the result back into stack.
4. After reading the characters from the postfix expression stack will be having only the value which is
result.
Example : Consider the postfix expression
5 3 + 8 2 - *
S.No Scanned Character / Operations Evaluated Part of
Symbol Stack Expression
Nothing
2 5 Push ( 5 )
5
Nothing
3 3 Push ( 3 ) 3
5
8
value1 =5
value2 =3
(5 + 3) = 8
4 + pop two operands
8
(5 + 3) = 8
5 8 Push ( 8 )
8
8
(5 + 3) = 8
2
6 2 Push ( 2 ) 8
8
value1=2
value2=8
(8-2)=6
7 - pop two operands 6
8
value1=6
value2=8
8 * pop two operands (8 * 6)=48
48
9
BALANCING OF SYMBOLS
The objective of this application is to check the Symbols such as parenthesis ( and ), Square
brackets [ and ] and Curly braces { and } are matched or not.
The algorithm is very much useful in compilers.
We know in a valid expression the parenthesis, Square brackets , Curly braces must occur in
pairs.
That is when there is an opening parenthesis, Square brackets , Curly braces there should be
corresponding closing parenthesis. Otherwise , the expression is not a valid.
Examples
EXAMPLE Valid? Description
(A+B) + (C-D) Yes The expression is having balanced symbol
((A+B) + [C-D]] No The last brace does not correspond with the first
opening brace.
3. If the character is an opening Symbol like ‘(‘ , ‘{‘ or ‘[‘ then it is PUSHED in to the stack.
4. If the character is an closing Symbol like ' ) ' , ' } ' , ' ] ' , then
a ) If the stack is empty then report as unbalanced expression otherwise pop from the stack .
b ) if the symbol popped is not the corresponding open symbol then report as unbalanced
expression
5. After reading all the characters are processed, if the stack is not empty report as unbalanced
10
Example 1 : Check whether the expression is balanced symbol or not ( ) ( ( ) [ ( ) ] )
Example 2 : Check whether the expression is balanced symbol or not ((A+B) + (C-D)
The expression is not having balanced symbol, because One closing brace is missing.
11
DATA STRUCTURES
ASSIGNMENT - 1
C)2*3/(2-1)+5*(4-1)
C)2*3/(2-1)+5*(4-1)
12