UNIT-4 Data Structures: Data Structures in C Are An Inevitable Part of Programs
UNIT-4 Data Structures: Data Structures in C Are An Inevitable Part of Programs
UNIT-4 Data Structures: Data Structures in C Are An Inevitable Part of Programs
Data Structures
Computer programs frequently process data, so we require efficient ways in which we
can access or manipulate data. Some applications may require modification of data
frequently, and in others, new data is continuously added or deleted. So we need efficient
ways of accessing data to act on it and build efficient applications.
Data Structures is a collection of data and are used to store data in an organized and efficient
manner.
The elements are inserted adjacent to each Elements which are stored in a non linear
other and can also be retrieved similarly. data structure have certain relationship
among them while being stored or
retrieved. There is a certain definite pattern
which always govern the addition of a new
element to the structure
Data elements are easy to be retrieved as Data elements are not easy to be retrieved
they can be just accessed in one run. or stored as they follow a strict relationship
among the various elements
The Linear Data Structures are The Non Linear Data Structures are
comparatively simpler and provide a certain complex data structures which can prove to
ease of working with be tricky to an extent.
The elements are inserted adjacent to each Elements which are stored in a non linear
other and can also be retrieved similarly. data structure have certain relationship
among them while being stored or
retrieved. There is a certain definite pattern
which always govern the addition of a new
element to the structure
Examples: Linked List, Stack, Queue etc Examples: Trees, graphs etc.
Stack
⮚ Stack is a linear Data structure.
⮚ A stack is a container of objects, it follows the property Last-In First-Out (LIFO).
⮚ The objects or elements can insert and removed according to the last-in first-out
(LIFO) principle.
⮚ Stack will have only one end, it is called as top of the stack.
⮚ A stack is a limited access data structure - elements can be added and removed from
the stack only at the top end.
1. push()
● An element can be inserted into stack using push operation.
● top index is incremented by 1.
● Each Push operation increases the size of stack by 1.
● Top>=Maxsize-1 If the stack is full, then it is said to be an
Overflow condition.
30
20 20
10 10 10
2 2 2 🡨 top
1 1 🡨 top 1
0 🡨 top 0 0
2. pop()
● An element is removed at the top index from stack using pop
operation.
● top index is decremented by 1.
● Each pop operation decreases the size of the stack by 1.
● Top==-1 If the stack is empty, then it is said to be an Underflow
condition.
1: If TOP != -1 then
a.Print STACK[TOP] is deleted;
b.TOP:=TOP – 1
2.Otherwise
Print “Stack is Underflow”
Example:
20
10 10
2 2 2
1 🡨 top 1 1
0 0 🡨 top 0
Expressions
Infix -> a+b
Prefix -> +ab
Postfix -> ab+
Applications of stack:
Summary of stack
#include<stdio.h>
#define MAX 100
void push();
void pop();
void display();
int stack[MAX],top=-1,item;
void main()
{
int choice;
while(1) //infinite loop
{
printf(" \nEnter the Choice\n");
printf("1.PUSH\n 2.POP\n3.DISPLAY\n4.EXIT\n");
scanf("%d",&choice);
switch(choice)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(1); //exit terminates program
break;
}
}
}
void push()
{
if(top>=MAX-1)
printf("STACK is over flow\n");
else
{
top++;
printf(" Enter an item to be pushed:");
scanf("%d",&item);
stack[top]=item;
}
}
void pop()
{
if(top==-1)
printf("Stack is under flow\n");
else
{
printf("The popped element is %d\n", stack[top]);
top--;
}
}
void display()
{
int i;
if(top!=-1)
{
printf("The elements of STACK \n");
for(i=top; i>=0; i--)
printf("%d\n",stack[i]);
}
else
printf("The STACK is empty\n");
}
Queue
⮚ Queue performs 2 operations called enqueue() to insert item into queue and
dequeue() for remove element
1. Enqueue()
● An element can be inserted into queue using enqueue operation.
● rear index is incremented by 1 in enqueue() operation.
● Each enqueue() operation increases the size of queue by 1.
● When rear be MAX, queue is full, then it is said to be an Overflow
condition.
10
1 20
0 1 0
2
0 1 2
rear
rear
c)Enqueue() d)Enqueue()
rear=1, front=-1
if (rear<MAX-1) rear=2, front=-1
1<2 true if (rear<MAX-1)
a) rear++🡪 rear=2, 2<2 false
b)item=30 Queue Overflow
c) q[rear]=20 ->q[2]=30
10 20 30
0 1 2
rear
2. dequeue()
● An element is removed from queue using dequeue operation.
● front index is incremented by 1 in dequeue() operation.
● Each dequeue() operation decreases the size of the queue by 1.
⮚ When rear==-1 or front >rear, the queue is empty, then it is said to be an Underflow
condition
1.if front==-1
front=0
2. If rear == -1 or front > rear then
Print “Queue is Underflow”
3.Otherwise
a. Print queue[front] is deleted;
b. front = front+ 1;
a) dequeue() b) dequeue()
Rear=2 rear=2, front=-1 rear=2, front=1
30
20 30 0 1 2
0 1 2
rear
front rear front
c) dequeue() d) dequeue()
rear=2, front=2
rear=2, front=3
if(front==-1) false
if(front==-1) false
if(rear==-1 || front>rear)
0 1 2 3
rear front
Applications of Queues
● Serving requests on a single shared resource, like a printer, CPU task scheduling etc.
● In real life scenario, Call Center phone systems uses Queues to hold people calling
them in an order, until a service representative is free.
● Handling of interrupts in real-time systems. The interrupts are handled in the same
order as they arrive i.e First come first served.
Summary of Queues
#include<stdio.h>
#define MAX 100
void enqueue();
void dequeue();
void display();
int queue[100],front=-1,rear=-1,item;
void main()
{
int choice;
while(1)
{
printf(" \nEnter the Choice\n");
printf("1.ENQUEUE\n 2.DEQUEUE\n3.DISPLAY\n4.EXIT\n");
scanf("%d",&choice);
switch(choice)
{
case 1: enqueue ();
break;
case 2: dequeue ();
break;
case 3: display();
break;
case 4: exit(1);
break;
}
}
}
void enqueue ()
{
if(rear<MAX-1)
{
rear++;
printf(" Enter an item to be insert:");
scanf("%d",&item);
queue[rear]=item;
}
else
printf("\n\QUEUE is over flow");
}
void dequeue ()
{
if(front==-1)
front=0;
if( rear==-1 || front>rear)
printf("\n\t QUEUE is under flow");
else
{
printf("The deleted element is %d\n", queue[front]);
front++;
}
}
void display()
{
int i;
if(front==-1)
front=0;
if( rear==-1 || front>rear )
printf("The QUEUE is empty\n");
else
{
printf("\n The elements of QUEUE\n");
for(i=front; i<=rear; i++)
printf("%d\n",queue[i]);
}
}
Output: 1. ENQUEUE
Enter the Choice 2.DEQUEUE
1.ENQUEUE 3.DISPLAY
2.DEQUEUE 4.EXIT
3.DISPLAY 3
4.EXIT The elements of Queue
1 10
Enter an item to be insert:10 20
Enter the Choice 30
1. ENQUEUE Enter the Choice
2.DEQUEUE 1. ENQUEUE
3.DISPLAY 2.DEQUEUE
4.EXIT 3.DISPLAY
1 4.EXIT
2.DEQUEUE 2.DEQUEUE
3.DISPLAY 3.DISPLAY
4.EXIT 4.EXIT
1 3