Download as pdf or txt
Download as pdf or txt
You are on page 1of 79

SUNITA D.

RATHOD
DEFINATION
Data Structure is a way to store and organize data so that it can be
used efficiently.

we have already seen one of the data structures, i.e., array in C


language.

Array is a collection of memory elements in which data is stored


sequentially, i.e., one after another.
TYPES OF DATA STRUCTURES
TYPES OF DATA STRUCTURES
There are two types of data structures:

• Primitive data structure

• Non-primitive data structure

Primitive Data structure

The primitive data structures are primitive data types. The int, char,
float, double, and pointer are the primitive data structures that can
hold a single value.

Non-Primitive Data structure

The non-primitive data structure is divided into two types:

• Linear data structure

• Non-linear data structure


TYPES OF DATA STRUCTURES
Linear Data Structure

The arrangement of data in a sequential manner is known as a linear


data structure.

The data structures used for this purpose are Arrays, Linked list,
Stacks, and Queues.

In these data structures, one element is connected to only one another


element in a linear form.
TYPES OF DATA STRUCTURES

Queue

Linked List
TYPES OF DATA STRUCTURES
Non-Linear Data Structure

When one element is connected to the 'n' number of elements


known as a non-linear data structure.

The best example is trees and graphs.

In this case, the elements are arranged in a random manner.


MAJOR OPERATIONS
The major or the common operations that can be performed on the
data structures are:

• Searching: We can search for any element in a data structure.

• Sorting: We can sort the elements of a data structure either in an


ascending or descending order.

• Insertion: We can also insert the new element in a data structure.

• Updation: We can also update the element, i.e., we can replace the
element with another element.

• Deletion: We can also perform the delete operation to remove the


element from the data structure.
CLASSIFICATION
Data structures can also be classified as:

Static data structure: It is a type of data structure where the size is


allocated at the compile time. Therefore, the maximum size is fixed.

Dynamic data structure: It is a type of data structure where the size is


allocated at the run time. Therefore, the maximum size is flexible.
ADVANTAGES OF DATA STRUCTURES
Efficiency: If the choice of a data structure for implementing a
particular ADT is proper, it makes the program very efficient in terms of
time and space.

Reusability: The data structure provides reusability means that


multiple client programs can use the data structure.

Abstraction: The data structure specified by an ADT also provides the


level of abstraction. The client cannot see the internal working of the
data structure, so it does not have to worry about the implementation
part. The client can only see the interface.
STACK
A Stack is a linear data structure that follows the LIFO (Last-In-First-
Out) principle.
It contains only one pointer top pointer 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 the top of stack.
STACK OPERATIONS
The following are some common operations implemented on the
stack:

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.

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.

isEmpty(): It determines whether the stack is empty or not.


STACK OPERATIONS
isFull(): It determines whether the stack is full or not.'

peek(): It returns the element at the given position.

count(): It returns the total number of elements available in a stack.

change(): It changes the element at the given position.

display(): It prints all the elements available in the stack.


PUSH OPERATION
IMPLEMENTATION OF PUSH
POP OPERATION
IMPLEMENTATION OF POP
IMPLEMENTATION OF PEEK
APPLICATIONS OF STACK
• Balancing of symbols

• String reversal

• UNDO/REDO

• Recursion

• DFS(Depth First Search)

• Backtracking

• Expression conversion

• Memory management
C PROGRAM FOR STACK
#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*********");
C PROGRAM FOR STACK
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;
}
C PROGRAM FOR STACK
case 2: case 4:
{ {
pop(); printf("Exiting....");
break; break;
} }
case 3: default:
{ {
show(); printf("Please Enter valid choice ");
break;
}
}
};
}
}
C PROGRAM FOR STACK
void push ()
{
int val;
if (top == n )
printf("\n Overflow");
else
{
printf("Enter the value?");
scanf("%d",&val);
top = top +1;
stack[top] = val;
}
}
C PROGRAM FOR STACK
void pop ()
{
if(top == -1)
printf("Underflow");
else
top = top -1;
}
C PROGRAM FOR STACK
void show()
{
for (i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
if(top == -1)
{
printf("Stack is empty");
}
}
STACK APPLICATION
Conversion of Arithmetic Expression into various Notations:
STACK APPLICATION
Infix to postfix conversion
STACK APPLICATION
Infix to postfix conversion
STACK APPLICATION
Evaluation postfix expression
STACK APPLICATION
Delimiter Checking
STACK APPLICATION
Reverse a String
QUEUE
• Queue can be defined as an ordered list which enables insert
operations to be performed at one end called REAR and
delete operations to be performed at another end
called FRONT.

• Queue is referred to be as First In First Out list (FIFO).

• For example, people waiting in line for a rail ticket form a


queue.
TYPES OF QUEUE

• Simple Queue
• Circular Queue
• Priority Queue
• Double ended Queue
SIMPLE QUEUE / LINEAR QUEUE
In Linear Queue, an insertion takes place from one end while the
deletion occurs from another end.

The end at which the insertion takes place is known as the rear
end, and the end at which the deletion takes place is known as
front end.

It strictly follows the FIFO rule.


QUEUE OPERATIONS
Enqueue: It insert the element at the rear end of the queue.

Dequeue: It performs the deletion from the front-end of the queue.

Peek: It returns the element, which is pointed by the front pointer in the
queue but does not delete it.

Queue overflow (isfull): It shows the overflow condition when the


queue is completely full.

Queue underflow (isempty): It shows the underflow condition when


the Queue is empty, i.e., no elements are in the Queue.
IMPLEMENTATION OF INSERT
IMPLEMENTATION OF DELETE
DRAWBACK OF SIMPLE QUEUE
The major drawback of using a linear Queue is that insertion is done
only from the rear end.

If the first three elements are deleted from the Queue, we cannot
insert more elements even though the space is available in a Linear
Queue.

In this case, the linear Queue shows the overflow condition as the
rear is pointing to the last element of the Queue.
CIRCULAR QUEUE
The drawback that occurs in a linear queue is overcome by using
the circular queue.

In Circular Queue, all the nodes are represented as circular.

It is similar to the linear Queue except that the last element of the
queue is connected to the first element.

A B C
CIRCULAR QUEUE REPRESENTATION
DIAGRAMMATIC EXPLANATION
A] E]

B] F]

C] G]

D] H]
ALGORITHM FOR INSERT
Step 1: IF (REAR+1)%MAX = FRONT
Write " OVERFLOW "
Goto step 4
[End OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE IF REAR = MAX - 1 and FRONT ! = 0
SET REAR = 0
ELSE
SET REAR = (REAR + 1) % MAX
[END OF IF]
Step 3: SET QUEUE[REAR] = VAL
Step 4: EXIT
ALGORITHM FOR DELETE
Step 1: IF FRONT = -1
Write " UNDERFLOW "
Goto Step 4
[END of IF]
Step 2: SET VAL = QUEUE[FRONT]
Step 3: IF FRONT = REAR
SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END of IF]
[END OF IF]

Step 4: EXIT
APPLICATION OF CIRCULAR QUEUE
• Memory management

• CPU Scheduling

• Round robin scheduling

• Traffic system
PRIORITY QUEUE
It is a special type of queue in which the elements are arranged
based on the priority.

It is a special type of queue data structure in which every element


has a priority associated with it.

Suppose some elements occur with the same priority, they will be
arranged according to the FIFO principle.
PRIORITY QUEUE
There are two types of priority queue :

Ascending priority queue - In ascending priority queue, elements


can be inserted in arbitrary order, but only smallest can be deleted
first.

Descending priority queue - In descending priority queue, elements


can be inserted in arbitrary order, but only the largest element can be
deleted first.
DEQUE (OR, DOUBLE ENDED QUEUE)
In Deque or Double Ended Queue, insertion and deletion can be done
from both ends of the queue either from the front or rear.

It means that we can insert and delete elements from both front and
rear ends of the queue.

A B C D E B B F
DEQUE (OR, DOUBLE ENDED QUEUE)
There are two types of deque :

• Input restricted deque - As the name implies, in input


restricted queue, insertion operation can be performed at only
one end, while deletion can be performed from both ends.
DEQUE (OR, DOUBLE ENDED QUEUE)
• Output restricted deque - As the name implies, in output
restricted queue, deletion operation can be performed at only
one end, while insertion can be performed from both ends.
LINKED LIST
• Linked list is a linear data structure that includes a series of
connected nodes.

• Linked list can be defined as the nodes that are randomly stored in
the memory.

• A node in the linked list contains two parts, i.e., first is the data part
and second is the address part.

• The last node of the list contains a pointer to the null.

• After array, linked list is the second most used data structure.

• In a linked list, every link contains a connection to another link.


REPRESENTATION OF A LINKED LIST

struct node
{
int data;
struct node *next ;
}
TYPES OF LINKED LIST

• Singly-linked list

• Doubly linked list

• Circular singly linked list

• Circular doubly linked list


SINGLY LINKED LIST
• Linked List can be defined as collection of objects
called nodes that are randomly stored in the memory.

• A node contains two fields i.e. data stored at that particular


address and the pointer which contains the address of the next
node in the memory.

• The last node of the list contains pointer to the null.


NODE CREATION

struct node

int data;

struct node *next;

};

struct node *head, *ptr;

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


LINKED LIST OPERATION
• INSERT BEGINING

• INSERT MIDDLE/ AFTER NODE

• INSERT LAST

• DELETE BEGINNING

• DELETE MIDDLE / AFTER NODE

• DELETE LAST

• TRAVERSING

• SEARCHING
INSERT BEGINNING
INSERT BEGINNING
INSERT END
INSERT END
INSERT MIDDLE
INSERT MIDDLE
DELETE BEGINNING
DELETE BEGINNING
DELETE LAST
DELETE LAST
DELETE MIDDLE
DELETE MIDDLE
DISPLAY / TRAVERSING
DOUBLY LINKED LIST
Doubly linked list is a complex type of linked list in which a node
contains a pointer to the previous as well as the next node in the
sequence.

In a doubly linked list, a node consists of three parts: node data,


pointer to the next node in sequence (next pointer) , pointer to the
previous node (previous pointer).
NODE STRUCTURE

struct node

struct node *prev;

int data;

struct node *next;

}
OPERATIONS ON DOUBLY LINKED LIST
• INSERT BEGINING

• INSERT MIDDLE/ AFTER NODE

• INSERT LAST

• DELETE BEGINNING

• DELETE MIDDLE / AFTER NODE

• DELETE LAST

• TRAVERSING

• SEARCHING
INSERT BEGINNING
g
INSERT END
INSERT MIDDLE
DELETE BEGINNING
DELETE END
DELETE MIDDLE
APPLICATION OF LINKED LIST
• Mailing list

• Memory management

• Linked allocation of files

• Addition of long positive integers

• Addition of 2 Polynomials

• Multiplication of 2 polynomials.
Thank you

You might also like