UNIT-4 Data Structures: Data Structures in C Are An Inevitable Part of Programs

You might also like

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

UNIT-4

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 in C are an inevitable part of programs.

Data structure Definition

Data Structures is a collection of data and are used to store data in an organized and efficient
manner.

Linear Data Structures


● Array: Array is collection of similar data type, you can insert and deleted element form
array without follow any order.
● Stack: Stack work on the basis of Last-In-First-Out (LIFO). Last entered element removed
first.
● Queue: Queue work on the basis of First-In-First-Out (FIFO). First entered element
removed first.
● Linked List: Linked list is the collection of nodes, Here you can insert and delete data in
any order.
Difference between Linear and Non Linear Data Structure

Linear Data Structure Non Linear Data Structure

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.

These do not provide us with efficient Efficient memory utilization is experienced


memory utilization. while working with the non linear data
structures.

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.

Fig: stack Data structure

⮚ At the initial stage of the stack top=-1


⮚ Stack performs only 2 operations push() and pop()

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.

Algorithm for push an element into stack


1.if top<Max size-1 //CHECKING FOR EMPTY SPACE
a. top=top+1
b. Read, item
c. stack[top]=item
2. else Print "stack overflow"

Example push operation can be visualized as:

Initially top=-1 push() push() push() push()

top=-1,item=10 top=0,item=20 top=1,item=30 top=2,item=40

If(top<Max-1) If(top<Max-1) If(top<Max-1) If(top<Max-1) false

(-1<2) (0<2) 1<2 2<2

Max=3 top++🡪top=0 top++🡪top=1 top++🡪 top=2 Stack Overflow

Stack[top]=item Stack[top]=item Stack[top]=item

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.

Algorithm to pop an element from stack.

1: If TOP != -1 then
a.Print STACK[TOP] is deleted;
b.TOP:=TOP – 1
2.Otherwise
Print “Stack is Underflow”
Example:

Top=2 pop() pop() pop() pop()

30 top=2 top=1 top=0 top=-1

20 If(Top!=-1) If(Top!=-1) If(Top!=-1) If(Top!=-1) false


10 2!=-1 1!=-1 0 !=-1 -1!=-1
2 🡨 top Stack[top]-deleted Stack[top]->deleted Stack[top]-deleted else
1 30 deleted, 20 deleted, 10 deleted, Stack Underflow
0 Top-- -> top=1 Top-- ->top=0 Top-- ->top=-1

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:

⮚ Expression Evaluation. Stack is used to evaluate prefix, postfix and


infix expressions. (a+b) trough stack
⮚ Expression Conversion. infix->prefix, infix->post
⮚ An expression can be represented in prefix, postfix or infix notation.
⮚ Syntax Parsing.
⮚ Backtracking.
⮚ Parenthesis Checking.
⮚ Function Call.

Summary of stack

⮚ It is type of linear data structure.


⮚ It follows LIFO (Last In First Out) property.
⮚ It has only one end TOP that points the last or top most element of Stack.
⮚ Insertion and Deletion in stack can only be done from TOP only.
⮚ Insertion in stack is also known as a PUSH operation.
⮚ Deletion from stack is also known as POP operation in stack.
⮚ Initial value of TOP is -1.
⮚ TOP==-1 is the UNDERFLOW conditions of stack, means stack is empty.
⮚ TOP>=Maxsize-1 is OVERFLOW conditions of stack. Stack is Full.

Implement Stack using Arrays

#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");
}

Output: Enter the Choice


Enter the Choice 1.PUSH
1.PUSH 2.POP
2.POP 3.DISPLAY
3.DISPLAY 4.EXIT
4.EXIT 3
1 The elements of STACK
Enter an item to be pushed:10 30
20
Enter the Choice 10
1.PUSH Enter the Choice
2.POP 1.PUSH
3.DISPLAY 2.POP
4.EXIT 3.DISPLAY
1 4.EXIT
Enter an item to be pushed:20 2
The popped element is 30
Enter the Choice Enter the Choice
1.PUSH 1.PUSH
2.POP 2.POP
3.DISPLAY 3.DISPLAY
4.EXIT 4.EXIT
1 3
Enter an item to be pushed:30 The elements of STACK
20
10

Queue

⮚ Queue is a linear Data structure.


⮚ Queue follows the property First-In First-Out (FIFO).
⮚ The objects or elements can insert and removed according to the First-in first-out
(FIFO) principle.
⮚ Queue will have two ends, called as front and rear.
⮚ Elements can be added to queue at rear index and removed from the queue at the
front index.
⮚ At the initial stage of queue front=-1 and rear=-1

Fig: Queue Data structure

⮚ 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.

Algorithm for enqueue() to add an element to queue


1. if rear<Max size-1
a. rear=rear+1
b. Read item
c. queue[rear]=item
2. else Print "Queue overflow"

initially a)Enqueue() b)Enqueue()


rear=-1,
front=-1,
rear=-1, front=-1 rear=0,front=-1
MAX=3
if (rear<MAX-1) if (rear<MAX-1)
queue is empty
-1<2 true 0<2 true
a)rear++🡪rear=0, a)rear++🡪rear=1,
b)item=10
b) item=20
c)q[rear]=10 ->q[0]=10
c)q[rear]=20 ->q[1]=20

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

Algorithm for dequeue() to remove an element from queue.

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

Front=-1 if(front==-1) true if(front==-1) false

Max=3 front=0 1==-1


if(rear==-1 || front>rear) if(rear==-1 || front>rear)

10 20 30 2==-1 || 0>2 false 2==-1 || 1>2 false


0 1 2 Else Else
a) Q[front] a) Q[front]- q[1]-20 is
deleted
rear ->q[0] 10 is deleted
b) Front++ 🡪front=2
b)Front++ 🡪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)

2==-1 || 2>2 false

Else if(rear==-1 || front>rear)

a)Q[front] - q[2] 30 is deleted 2==-1 || 3>2 true

b)Front++ 🡪front=3 Queue Underflow

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

⮚ It is type of linear data structure.


⮚ It follows FIFO (First In First Out) property.
⮚ It has 2 ends FRONT and REAR points.
⮚ Insertion can be done at the REAR end.
⮚ Deletion in queue can be done at FRONT end.
⮚ At the initial stage of queue front=rear=-1
⮚ Insertion in queue is also known as a ENQUEUE operation.
⮚ Deletion from queue is also known as DEQUEUE operation.
⮚ REAR==-1 or FRONT>REAR are the UNDERFLOW conditions of queue,
means queue is empty.
⮚ REAR>Maxsize-1 is OVERFLOW conditions of queue. Queue is Full.

Implement Queue using Arrays

#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

Enter an item to be insert:20 2


The deleted element is 10

Enter the Choice Enter the Choice


1. ENQUEUE 1. ENQUEUE

2.DEQUEUE 2.DEQUEUE
3.DISPLAY 3.DISPLAY

4.EXIT 4.EXIT
1 3

Enter an item to be insert:30 The elements of Queue


20

Enter the Choice 30

You might also like