Data Structures Unit 1
Data Structures Unit 1
Data Structures Unit 1
AND
TELECOMMUNICATION
ENGINEERING
B.Tech V SEMESTER
Advance Data Structures and
Algorithms
Characteristics
end if
top ← top + 1
stack[top] ← data
end procedure
POP Operation
Accessing the content while removing it from the stack, is known as a Pop
Operation. A Pop operation may involve the following steps −
• Step 1 − Checks if the stack is empty.
• Step 2 − If the stack is empty, produces an error and exit.
• Step 3 − If the stack is not empty, accesses the data element at
which top is pointing.
• Step 4 − Decreases the value of top by 1.
• Step 5 − Returns success.
• Algorithm for Pop Operation
• A simple algorithm for Pop operation can be derived
as follows −
#include <iostream.h>
void main()
#include<conio.h>
{
int val;
int stack[100], n=100, top=-1;
void push(int val)
{ cout<<"\nEnter value to be pushed:\n";
if(top>=n-1) cin>>val;
cout<<"Stack Overflow"; push(val);
else display();
{ cout<<"Enter another value to be pushed:\n";
top++; cin>>val;
stack[top]=val; push(val);
} display();
} pop();
void pop() display();
{ getch();
if(top<=-1) }
cout<<"Stack Underflow\n”;
else
{
cout<<"\nThe popped element is "<< stack[top] ;
top--;
}
}
void display()
{
if(top>=0)
{
cout<<"\nStack elements are:";
for(int i=top; i>=0; i--)
cout<<stack[i]<<"\n ";
}
else
cout<<"Stack is empty";
}
QUEUE
• Queue is an abstract data type which can be implemented as a linear or
circular list. It has a front and rear.
• Queue is linear data structure, in which the first element is inserted from
one end called the REAR(also called tail), and the removal of existing
element takes place from the other end called as FRONT(also
called head).
• The Queues are also called as FIFO (First-In-First-Out) lists, Since the first
element in the list will be removed first.
• Queue Representation :
• The following diagram given below tries to explain queue representation
as data structure −
• This makes queue as FIFO(First in First Out) data structure, which
means that element inserted first will be removed first.
• Which is exactly how queue system works in real world. If you go to
a ticket counter to buy movie tickets, and are first in the queue, then
you will be the first one to get the tickets. Right? Same is the case
with Queue data structure. Data inserted first, will leave the queue
first.
• The process to add an element into queue is called Enqueue and the
process of removal of an element from queue is called Dequeue.
• The condition FRONT = NULL will indicate that
Queue is empty.
• Whenever the element is deleted from the queue
the value of the FRONT is increased by one.
FRONT = FRONT +1;
• Similarly whenever any element is added to the
queue then the REAR value is increased by one.
REAR = REAR +1;
• IF FRONT = REAR=NULL then Queue is empty.
• If FRONT = REAR!=NULL
Then only one element is present in the Queue.
• There are four types of Queue:
1. Simple Queue
2. Circular Queue
3. Priority Queue
4. Dequeue (Double Ended Queue)
1. Simple Queue
• Simple queue is a linear list which defines the
simple operation of queue in which insertion
occurs at the rear end of the list and deletion
occurs at the front of the list.
2. Circular Queue
• In a circular queue, all nodes are treated as circular.
Last node is connected back to the first node.
• Circular queue is also called as Ring Buffer.
• It is an abstract data type.
• Circular queue contains a collection of data which
allows insertion of data at the end of the queue and
deletion of data at the beginning of the queue.
3. Priority Queue
• Each node in the list contains three items of information: an
information field INFO, a priority number PRN and a link number LINK.
• Priority queue contains data items which have some preset priority.
While removing an element from a priority queue, the data item with
the highest priority is removed first.
• In a priority queue, insertion is performed in the order of arrival and
deletion is performed based on the priority.
• A node X precedes a node Y in the list when
– Node X has the higher priority than Y OR
• when both have the same priority but X was added to the list before Y.
4. Dequeue (Double Ended Queue)
Dequeue is also known as Double Ended Queue.
It is a linear list in which elements can be added
or deleted at both the ends of the queue.
Enqueue Operation
• Queues maintain two data pointers, front and rear. Therefore, its operations
are comparatively difficult to implement than that of stacks.
• The following steps should be taken to enqueue (insert) data into a queue −
• Step 1 − Check if the queue is full.
• Step 2 − If the queue is full, produce overflow error and exit.
• Step 3 − If the queue is not full, increment rear pointer to point the next
empty space.
• Step 4 − Add data element to the queue location, where the rear is pointing.
• Step 5 − return success.
Enqueue Algorithm
Dequeue Operation
Accessing data from the queue is a process of two tasks − access the data
where front is pointing and remove the data after access. The following steps are
taken to perform dequeue operation −
• Step 1 − Check if the queue is empty.
• Step 2 − If the queue is empty, produce underflow error and exit.
• Step 3 − If the queue is not empty, access the data where front is pointing.
• Step 4 − Increment front pointer to point to the next available data element.
• Step 5 − Return success.
Dequeue Algorithm
What do you mean by UNDERFLOW and OVERFLOW in
CIRCULAR QUEUE?
• Circular Queue is a linear data structure in which
the operations are performed based on FIFO
(First In First Out) principle and the last position
is connected back to the first position to make a
circle. It is also called ‘Ring Buffer’.
• If Circular Queue is full then we can not insert an
element into Circular Queue. If value of REAR
variable is greater then or equal to SIZE – 1(here
8-1=7) and value of FRONT variable is equal to 0
then we can not insert an element into Circular
Queue. This condition is known as “Overflow”.
• In order to delete an element from Circular
Queue first we have to check weather Circular
Queue is empty or not. If Circular Queue is
empty then we cannot delete an element from
Circular Queue. This condition is known
as “Underflow”.
LINKED LIST
• Linked list is a one way list, which is a linear collection of data elements, called nodes,
where the linear order is given by means of pointers.
• Each node is divided is into two parts : The first part contains the data, and the second part
, called the link field or nextpointer field contains the address of the next node in the list.
• A linked list is a linear data structure, in which the elements are not stored at contiguous
memory locations.
• The elements in a linked list are linked using pointers as shown in the below image:
• In simple words, a linked list consists of nodes where each node contains a data field and a
reference(link) to the next node in the list.
• The null pointer denoted in the diagram,
signals the end of the list.
• The linked list also contains a list pointer
variable – called START or HEAD which
contains the address of the first node in the
list;
• Hence there is an arrow drawn from the START
or HEAD to the first node.
• When START contains null then there is no
elements in the list and the list is known as
empty list.
Number Patient Next
1 KIRK
2
3 DEAN
START
4 MAXWELL
5 ADAM
6
7 LANE
8 GREEN
9 Samuels
10
11 Fields
12 Nelson
Representation Of Linked List in the
Memory
• List requires two linear arrays
1. LINK[PTR]
2. INFO[PTR]
• INFO[PTR]contains the information part of the
list.
• LINK[PTR] contains the nextpointer field of the
LIST.
S.No INFO LINK
3 O 6 • START = 9
4 T 0
• INFO[9]=N LINK[9]=3
• INFO[3]=O LINK[3]=6
5 • INFO[6]=Y LINK[6]=7
• INFO[7]=X LINK[7]=4
6 Y 7
• INFO[4]=T LINK[4]=0
7 X 4
9 N 3
TRAVERSING A LINKED LIST
• Let LIST be a LINKED LIST in the memory stored
in the linear arrays INFO and LINK and START is
pointing to the first element in the LIST,NULL is
indicating the end of the LIST.
• PTR is the pointer which points to the current
node.
• LINK[PTR] points to the next node to be
processed.
PTR :=LINK[PTR]
Above statement moves the pointer to the next
node in the LIST.
1. Initialize PTR or START.
2. Process INFO[PTR]
(Information at the first Node)
3. PTR := LINK[PTR] (Update PTR so that PTR points to the second node).
4. again process INFO[PTR](The information at second node).
5. Again update PTR by
PTR := LINK[PTR]
And process INFO[PTR] and so on…. Until
PTR = NULL,
which indicates the end of the LIST.
SEARCHING A LINKED LIST
Insertion into a linked list
In the insert operation three pointer fields are changes as follows
DEL(INFO,LINK,START,AVAIL,LOC,LOCP)
This algorithm deletes the node N with location LOC.LOCP is the
location of the node which precedes N or ,when N is the first node,
LOCP=NULL.
If LOCP=NULL, then:
Set START:=LINK[START]. [Deletes the first node.]
Else :
Set LINK[LOCP] := LINK[LOC]. [Deletes node N]
[End of If Structure]
[Return Deleted node to the AVAIL List]