0% found this document useful (0 votes)
3 views

Data Structures

Uploaded by

arshsolkar4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Data Structures

Uploaded by

arshsolkar4
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

INTRODUCTION

TO DATA
STRUCTURE

BY
S.SANKARESWARI
What is Data Structure ?

1. Data structure is a representation of data and the operations


allowed on that data
2. A data structure is a way to store and organize data in order to
facilitate the access and modifications.
⦿ Data Structure are the method of representing of logical
relationships between individual data elements related to the
solution of a given problems
⦿
Basic Data Structure

Basic Data Structures

Primitive Non-Primitive

int Char Pointer Linear Non-Linear


Float
Basic Data Structure
Non - Primitive

Linear Non-Linear

Array Linked Stack Queue


List Tree Graph Hash
tables
Types of Data Structure

Linear: In Linear data structure values are arrange in


linear fashion

1. Array : Fixed Size


2. Linked List : Variable size
3. Stack: Add to top and remove from
top
4. Queue :Add to back and remove from
front
Types of Data Structure
Non Linear :The data values in this structure
are not arranged in order

1. Hash tables: Unordered lists which use a “hash


function” to insert and search
2. Tree: Data is organized in branches
3. Graph: A more general branching structure with less
strict connection conditions than for a tree
Operations on data structure

1. Searching
2. Sorting
3. Insertion
4. Deletion
5. Updation
6. Coping
Stack
It is an ordered group of homogeneous items of elements.
Elements are added to and removed from the top of the stack
(the most recently added items are at the top of the stack).
The last element to be added is the first to be removed (LIFO:
Last In, First Out).
Examples of Stack
Stack operation

⦿ These are two basic operations associated with


stack:
⦿ Push() is the term used to insert/add an element
into a stack.
⦿ Pop() is the term used to delete/remove an
element from a stack.
Push operation
Pop operation
Algorithm for PUSH
Algorithm for POP
Queue
⦿ A queue is a linear list of elements in which deletion can take
place only atone end, called the front, and insertions can take
place only at the other end, called the rear. The term “front”
and “rear” are used in describing a linear list only when it is
implemented as a queue.
⦿ Queue is also called first-in-first-out (FIFO) lists
Operations:
⦿ Enqueue: insert an element at the rear of the queue
⦿ Dequeue: remove an element from the front of the queue
Insertions in queue
Deletion in queue
Queue operation
Display(queue)
⦿ Display (queue,maxsize)
this procedure display the elements of queue
1. if (front == -1) then
print (Queue is empty)
return
else
for (i=front; i<=rear ; i++)
printf(“%d” ,queue[i]);
Display of stack
⦿ if(top ==-1)
⦿ {
⦿ printf("\n\nStack empty.."); return;
}
⦿ else
⦿ {
⦿ printf("\n\nElements in stack:");
⦿ for(i = top; top>=0; i--)
⦿ printf("\t%d", stack[i]);
⦿ }
Circular Queue
⦿ A more efficient queue representation is obtained by regarding the array
Q[MAX] as circular. Any number of items could be placed on the queue.
This implementation of a queue is called a circular queue because it uses
its storage array as if it were a circle instead of a linear list.
There are two problems associated with linear queue.They are:
⦿ Time consuming: linear time to be spent in shifting the elements to the
beginning of the queue.
⦿ Signaling queue full: even if the queue is having vacant position.
For example, let us consider a linear queue status as follows:

11 12 13 if rear == size -1 “Q-full”


0 1 2 3 4 5 front=3 , rear =5
Continue-
Next insert another element, say 66 to the queue. We cannot insert 66 to
the queue as the rear crossed the maximum size of the queue (i.e., 5).
There will be queue full signal. The queue status is as follows:
⦿ This difficulty can be overcome if we treat queue position with index zero
as a position that comes after position with index four then we treat the
queue as a circular queue.
⦿ In circular queue if we reach the end for inserting elements to it, it is
possible to insert new elements if the slots at the beginning of the circular
queue are empty.
Insertion on circular-queue
⦿ void insertCQ()
⦿ {
⦿ int data;
⦿ if(count == MAX)
⦿ {
⦿ printf("\n Circular Queue is Full");
⦿ }
⦿ else
⦿ {
⦿ printf("\n Enter data: ");
⦿ scanf("%d", &data);
⦿ CQ[rear] = data;
⦿ rear = (rear + 1) % MAX;
⦿ count ++;
⦿ printf("\n Data Inserted in the Circular Queue ");
⦿ }
⦿ }
⦿
Deletion on circular queue
⦿ void deleteCQ()
⦿ {
⦿ if(count == 0)
⦿ {
⦿ printf("\n\nCircular Queue is Empty..");
⦿ }
⦿ else
⦿ {
⦿ printf("\n Deleted element from Circular Queue is %d ", CQ[front]);
⦿ front = (front + 1) % MAX;
⦿ count --;
⦿ }
⦿ }
⦿
display
⦿ void displayCQ()
⦿ {
⦿ int i, j;
⦿ if(count == 0)
⦿ {
⦿ printf("\n\n\t Circular Queue is Empty ");
⦿ }
⦿ else
⦿ {
printf("\n Elements in Circular Queue are: ");
j = count;
⦿ for(i = front; j != 0; j--)
⦿ {
⦿ printf("%d\t", CQ[i]);
⦿ i = (i + 1) % MAX;
⦿ }
⦿ }
⦿ }
display
⦿ void displayCQ()
⦿ {
⦿ int i, j;
⦿ if(count == 0)
⦿ {
⦿ printf("\n\n\t Circular Queue is Empty ");
⦿ }
⦿ else
⦿ {
printf("\n Elements in Circular Queue are: ")
⦿ for(i = front; I<=size-1 ; i++)
⦿ {
⦿ printf("%d\t", CQ[i]);
⦿ }
⦿ for(i =0; I<=rear ; i++)
⦿ {
⦿ printf("%d\t", CQ[i]);
⦿ }

⦿ }

You might also like