Data Structures: Unit-Ii
Data Structures: Unit-Ii
Data Structures: Unit-Ii
UNIT-II
STACKS:
FEATURES:
3. push() function is used to insert new elements into the Stack and pop() is used to delete an element from
the stack. Both insertion and deletion are allowed at only one end of Stack called Top.
4. Stack is said to be in Overflow state when it is completely full and is said to be in Underflow state if it is
completely empty.
REPRESENTATION:
Insertion and deletion can be done only one side in the concept of stack with the help of variable called top.
Step 1: Include all the header files which are used in the program and define a constant 'SIZE' with
specific value.
Step 3: Create a one dimensional array with fixed size (int stack[SIZE])
Step 4: Define a integer variable 'top' and initialize with '-1'. (int top = -1)
Step 5: In main method display menu with list of operations and make suitable function calls to perform
In a stack, push() is a function used to insert an element into the stack. In a stack, the new element is always inserted
at top position. Push function takes one integer value as parameter and inserts that value into the stack. We can use
the following steps to push an element on to the stack...
Step 2: If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and terminate the
function.
Step 3: If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value
(stack[top] = value).
In a stack, pop() is a function used to delete an element from the stack. In a stack, the element is always deleted
from top position. Pop function does not take any value as parameter. We can use the following steps to pop an
element from the stack...
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--).
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
#include<stdio.h>
#include<conio.h>
#define SIZE 10
void push(int);
void pop();
void display();
void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
push(value);
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
RRAY
S
S: STACKS USING LINKED LIST:
The major problem with the stack implemented using array is, it works only for fixed number of data
values.
That means the amount of data must be specified at the beginning of the implementation itself.
Stack implemented using array is not suitable, when we don't know the size of data which we are going to
use.
A stack data structure can be implemented by using linked list data structure. The stack implemented using
linked list can work for unlimited number of values.
That means stack implemented using linked list works for 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 next node in the list.
The next field of the first element must be always NULL.
Example
In above example, the last inserted node is 99 and the first inserted node is 25. The order of elements inserted is 25,
32,50 and 99.
Operations
To implement stack using linked list, we need to set the following things before implementing actual operations.
Step 1: Include all the header files which are used in the program. And declare all the user defined
functions.
Step 2: Define a 'Node' structure with two members data and next.
Step 4: Implement the main method by displaying Menu with list of operations and make suitable function
We can use the following steps to insert a new node into the stack...
We can use the following steps to delete a node from the stack...
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 define a Node pointer 'temp' and set it to 'top'.
We can use the following steps to display the elements (nodes) of a stack...
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
#include<stdio.h>
#include<conio.h>
struct Node
{
int data;
struct Node *next;
}*top = NULL;
void push(int);
void pop();
void display();
void main()
{
int choice, value;
clrscr();
printf("\n:: Stack using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Push\n2. Pop\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
push(value);
break;
case 2: pop(); break;
case 3: display(); break;
case 4: exit(0);
default: printf("\nWrong selection!!! Please try again!!!\n");
}
}
}
void push(int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(top == NULL)
newNode->next = NULL;
else
newNode->next = top;
top = newNode;
printf("\nInsertion is Success!!!\n");
}
void pop()
{
Output:
Applications:
1. Expression Evolution
2. Expression conversion
1. Infix to Postfix
3. Postfix to Infix
4. Prefix to Infix
3. Parsing
4. Simulation of recursion
The way to write arithmetic expression is known as notation. An arithmetic expression can be written in 3 different
equivalent notations. They are
Infix notation
Prefix notation
Postfix notation
Infix notation:
When the operator can be in between operands such expression can be known as Infix notation
prefix notation:
When the operator can be placed ahead of operands such expression can be known as prefix notation.
postfix notation:
When the operator can be placed after operands such expression can be known as postfix notation
Recursion:
Recursion is a programming technique that allows the programmer to express operations in terms of
themselves. In C, this takes the form of a function that calls itself.
A useful way to think of recursive functions is to imagine them as a process being performed where one of
the instructions is to "repeat the process".
Recursion classifiedinto 2 types
o Direct recursion
o Indirect recursion
Direct recursion:
The process calling the same function directly to solve a problem such recursion can be known as Direct recursion
In-Direct recursion:
The process calling the same function indirectly with the help of another function to solve a problem such recursion
can be known as in-direct recursion
int main()
{
... .. ...
recurse();
... .. ...
}
void recurse()
{
... .. ...
call();
... .. ...
}
Void call()
{
... .. ...
recurse(); }
int main()
{
int number, result;
result = sum(number);
printf("sum=%d", result);
}
#include <stdio.h>
if(i <= 1) {
return 1;
}
return i * factorial(i - 1);
}
int main() {
int i = 15;
printf("Factorial of %d is %d\n", i, factorial(i));
return 0;
}
Queue:hat 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 of elements are performed at two different positions. The
insertion is performed at one end and deletion is performed at other end.
In a queue data structure, the insertion operation is performed at a position which is known as 'rear' and 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()" and deletion
operation is performed using a function called "deQueue()".
Queue data structure is a linear data structure in which the operations are performed based on FIFO
principle.
"Queue data structure is a collection of similar data items in which insertion and deletion operations are
performed based on FIFO principle".
Example
Operations on a Queue
Queue data structure can be implemented in two ways. They are as follows...
1. Using Array
When a queue is implemented using array, that queue can organize only limited number of elements. When a queue
is implemented using linked list, that queue can organize unlimited number of elements.
A queue data structure can be implemented using one dimensional array. But, queue implemented using
array can store only fixed number of data values.
The implementation of queue data structure using array is very simple, 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'.
Initially both 'front' and 'rear' are set to -1. Whenever, we want to insert a new value into the queue,
increment 'rear' value by one and then insert at that position.
Whenever we want to delete a value from the queue, then increment 'front' value by one and then display
the value at 'front' position as deleted element.
Before we implement actual operations, first follow the below steps to create an empty queue.
Step 1: Include all the header files which are used in the program and define a constant 'SIZE' with
specific value.
Step 2: Declare all the user defined functions which are used in queue implementation.
Step 3: Create a one dimensional array with above defined SIZE (int queue[SIZE])
Step 4: Define two integer variables 'front' and 'rear' and initialize both with '-1'. (int front = -1, rear =
-1)
Step 5: Then implement main method by displaying menu of operations list and make suitable function
calls to perform operation selected by the user on queue.
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 takes one integer value as parameter and inserts that value into the queue.
We can use the following steps to insert an element into the queue...
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.
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.
The deQueue() function does not take any value as parameter. We can use the following steps to delete an
element from the queue...
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. Then check whether both front and rear are equal
(front == rear), if it TRUE, then set both front and rear to '-1' (front = rear = -1).
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+1'.
Step 3: Display 'queue[i]' value and increment 'i' value by one (i++). Repeat the same until 'i' value is equal
void enQueue(int);
void deQueue();
void display();
void main()
{
int value, choice;
clrscr();
while(1){
printf("\n\n***** MENU *****\n");
printf("1. Insertion\n2. Deletion\n3. Display\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
enQueue(value);
break;
case 2: deQueue();
break;
case 3: display();
break;
case 4: exit(0);
default: printf("\nWrong selection!!! Try again!!!");
}
}
}
void enQueue(int value){
if(rear == SIZE-1)
printf("\nQueue is Full!!! Insertion is not possible!!!");
else{
if(front == -1)
front = 0;
rear++;
queue[rear] = value;
printf("\nInsertion success!!!");
}
}
void deQueue(){
if(front == rear)
printf("\nQueue is Empty!!! Deletion is not possible!!!");
else{
printf("\nDeleted : %d", queue[front]);
front++;
if(front == rear)
front = rear = -1;
}
}
void display(){
if(rear == -1)
printf("\nQueue is Empty!!!");
else{
int i;
printf("\nQueue elements are:\n");
for(i=front; i<=rear; i++)
printf("%d\t",queue[i]);
}
Output:
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'.
Queue
From the taxonomy this structure is Linear - Sequential Access - First-in-First-out
Features:
1. A list structure with two access points called the front and rear.
2. All insertions (enqueue) occur at the rear and deletions (dequeue) occur at the front.
4. Homogeneous components
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.erations
To implement queue using linked list, we need to set the following things before implementing actual
operations:
Step 1: Include all the header files which are used in the program. And declare all the user defined
functions.
Step 2: Define a 'Node' structure with two members data and next.
Step 3: Define two Node pointers 'front' and 'rear' and set both to NULL.
Step 4: Implement the main method by displaying Menu of list of operations and make suitable function
We can use the following steps to insert a new node into the queue...
Step 1: Create a newNode with given value and set 'newNode next' to NULL.
Step 4: If it is Not Empty then, set rear next = newNode and rear = newNode.
We can use the following steps to delete a node from the queue...
Step 2: If it is Empty, then display "Queue is Empty!!! Deletion is not possible!!!" and terminate from
the function
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)).
We can use the following steps to display the elements (nodes) of a queue...
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 'temp' reaches to
#include<stdio.h>
#include<conio.h>
struct Node
{
int data;
struct Node *next;
}*front = NULL,*rear = NULL;
void insert(int);
void delete();
void display();
void main()
{
int choice, value;
clrscr();
printf("\n:: Queue Implementation using Linked List ::\n");
while(1){
printf("\n****** MENU ******\n");
printf("1. Insert\n2. Delete\n3. Display\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1: printf("Enter the value to be insert: ");
scanf("%d", &value);
insert(value);
Applications of queue:
1. Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
2. In real life, Call Center phone systems will use Queues, to hold people calling them in an order, until a
service representative is free.
Note: