You are on page 1of 23

12304 DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES

Data Structure: A Data Structure is a data object together with the relationships that
exist among the instances and among the individual elements that compose an instance.
In other words, we define Data Structure as a way of organizing data that considers not
only the items stored but also their relationship to each other.

Once a data structure has been chosen for a particular application, they have to be
handled in an efficient way. Hence there comes the necessity of studying Algorithms.

The two categories data structures are,

1. Linear Data Structures


2. Non-linear Data Structures

Linear data structures are those in which the elements form a sequence whereas non-
linear data structures are those in which the elements do not form a sequence.

LINEAR DATA STRUCTURES

There are two methods by which we can construct a linear data structure. They
are,
Method 1: By using a sequence of memory locations. These are implemented using
arrays.
Method 2: By using pointers or links. These are implemented using linked lists.

The different types of linear data structures are:

1. Lists
- The list can be implemented using arrays  these are called linear
lists or ordered lists.
- The list can be implemented using pointers  these are called
linked lists.
2. Stacks
- The stacks can be implemented using arrays.
- The stacks can be implemented using linked.
3. Queues
- The queue can be implemented using arrays.
- The queue can be implemented using linked lists.

LINEAR LIST

Linear List: A Linear list is a data object whose instances are of the form ( e 1, e2, …en),
where n is a finite natural number.
ei  Element of the list.
n  length of the list.
s  size of the element.
1.1
For example a list of names, list of marks, etc.
The operations that are performed on a linear list are,

1. Create a list.
2. Delete a list.
3. Determine whether the list is empty.
4. Determine the length of the list.
5. Find the kth element.
6. Search for a given element.
7. Delete the kth element
8. Insert a new element.
9. Display all elements.

Abstract Data Type (ADT): An Abstract Data Type is a representation in which we


provide a specification of the instances as well as of the operations that are to be
performed. More precisely,
An Abstract Data Type is a data type that is organized in such a way that the
specification of the object and the specification of the operation on the object are
separated from the representation of the object and the implementation of the operation.

Hence an Abstract Data Type representation separates the implementation of the


operations from their specification, thus leading the way to object oriented programming.

Formula Based Representation:

In this method we use an array to represent the instances of an object. Each


position of array is called a cell or a node. Individual elements of an instance are located
in the array using a mathematical formula as shown.

Location (i) = i - 1  ith element is in position i-1 (if array starts with position zero)

(or)

Location (i) = i  ith element is in position i (if array starts with position one)

A Linear list may be specified as an abstract data type (ADT) in which we provide a
specification of the instances as well as of the operations that are to be performed.

AbstractDataType LinearList
{
instances
ordered finite collection of zero or more elements
operations
Create( ): Create an empty linear list.
Destroy( ): Erase the list.
IsEmpty( ): Return true if the list is empty, false otherwise.
Length( ): Return the list size.
Find(k, x): Return kth element of the list in x and return false if not found.
Search(x): Return the position of x in the list otherwise return -1 if not found.

1.2
Delete(k): Delete the kth element.
Insert(k, x): Insert x just after kth element
Display( ): Display all elements of the list
}

LINKED LIST

Linked List: A Linked list is a collection of elements called nodes, each of which stores
two items called info and link. Info is an element of the list and a link is a pointer to the
next element. The linked list is also called a chain.

The different types of Linked lists are,

1. Singly linked list.


2. Doubly linked list
3. Circular linked list

SINGLY LINKED LIST

Singly Linked List: A singly linked list is a linked list in which each node contains only
one link pointing to the next node in the list.

In a singly linked list, the first node always pointed by a pointer called HEAD. If the link
of the node points to NULL, then that indicates the end of the list.

Operations on a singly linked list are,

1. Count the number of elements.


2. Add an element at the beginning of the list.
3. Add an element at the end of the list.
4. Insert an element at the specified position in the list.
5. Delete an element from the list.
6. Search x in the list.
7. Display all the elements of the list.

The Abstract Data Type of the linked list may be written as shown.

AbstractDataType LinkedList
{
instances
1.3
finite collection of zero or more elements linked by pointers
operations

Count( ): Count the number of elements in the list.


Addatbeg(x): Add x to the beginning of the list.
Addatend(x): Add x at the end of the list.
Insert(k, x): Insert x just after kth element.
Delete(k): Delete the kth element.
Search(x): Return the position of x in the list otherwise return -1 if not found
Display( ): Display all elements of the list
}

Algorithm:

Count( )

The first operation count( ) counts the number of elements in the list. Let Head be
a pointer pointing to the beginning of the list. If the head points to NULL, then the list is
empty. In the count algorithm given below, we first create a temporary pointer Temp and
make it point to the node where Head is pointing. Since Head always points to the
beginning of the list, Temp is now pointing to the first node in the list. A counter is
initialized to zero. The list is traversed using a while loop and every time we move to the
next node, the counter is incremented. If Temp doesn’t point to NULL, that means the
end of the list is not reached and hence temp is made to point to the next node. This is
repeated till the end of the list and finally we have the number of elements of the list in
the count variable.

COUNT( )

Temp = Head
Count = 0
While Temp ≠ NULL
Count = Count +1
Temp = Link (Temp)
End While
Return Count
End COUNT

1.4
Addatbeg( x )

The operation Addatbeg( x ), adds the element k at the beginning of the existing
list. First a temporary pointer is created and made to point to the node where head is
pointing. The given element K is stored in a new node. Check if the list is empty. If so
make link of new node point to NULL and make Head to point to new, making it the first
element of the list. If the list is not empty, then the link of new is made to point to the
first element of the existing list and Head is made to point to the new node.

1.5
ADDATBEG( K )

Info(New) = K
If Head = NULL
Then
Link(New) = NULL
Else
Link(New) = Head
Endif
Head = New
End ADDATBEG

Addatend( x )

The operation Addatend( x ), adds the given element x at the end of the existing
list. Initially it is checked whether the list is empty or not. If the list is empty then create
a new node ‘New’ and add the value x to the info part of the node. The link part is made
to point to NULL. Then the Head is made to point this new node making it the first node.
If the list already has some nodes and now a new node is to be added to the end of the
list, then create a temporary pointer called Temp and make it to point to the Head
initially. Then move the pointer the last node and now create a new node with value x.
Make the link part point to NULL. Now the link of temp is made to point to the new
node, thus making the new node as the last node of the list.

1.6
ADDATEND( X )

If Head = NULL
Info(Temp) = x
Link(Temp) = NULL
Head = Temp
Else
Temp = Head
While link(Temp) ≠ NULL
Temp = link(Temp)
End Whjle
Info(New) = x
Link(New)=NULL
Link(Temp) = New
Endif
End ADDATEND( )

Insert( k, x )

The operation Insert( k, x ) inserts the given element x in the k th position. A


temporary pointer Temp is created and made to point to Head. Now the Temp pointer is
moved to the k – 1 th node. A new node with value x is created and the link of the new
node is made to point to the position where the link of temp is pointing. Then the link of
temp is made to point to the new node. Thus the given element is inserted in the position
k.

1.7
INSERT ( k, x )

Temp = Head
I=1
While I < k-1
Temp = link(Temp)
I=I+1
End While
Info(New) = x
Link(New) = Link(Temp)
Link(Temp) = New
End INSERT( )

Delete ( x )

The operation Delete( x ), deletes the node with value x. The given value x is
compared with each node starting from the first node and continued till the last node. If
the node to be deleted is the first node, then simply Temp pointer is made to point to
Head and then Head is safely moved to point the next node. Now the first node is deleted
by deleting the Temp. If the node to be deleted is not the first node, then make it the old
node and move the temp pointer to the next node. Check if that is the node to be deleted.
If so, make the link of old point to link of temp and delete the node pointed by temp.
Now the node pointed by temp can be deleted. If this is also not the node to be deleted
then make this old node and move the Temp pointer to the next node and the process is
repeated till we reach the last node or the specified node is deleted.

DELETE( x )

Temp = Head
While Temp ≠ NULL
If Info(Temp) = x
Then
If Temp = Head
Then
Head = link(Temp)
Else
Link(old) = Link(Temp)
End If
Delete Temp
Else
Old = Temp
Temp = Link(Temp)
End If
End While
End DELETE( )

1.8
Search( x )

The operation Search( x ), searches the given value x in the list. If found returns
the node position where it is found. A temporary pointer Temp is created and made to
point to the Head. Now info part of Temp is compared with the value x. If the value
matches the node position number is returned otherwise Temp pointer is moved to the
next node. This is repeated till the end of the list is reached or till the element to be
searched is found.

1.9
SEARCH( x )

Temp = Head
Count = 1
While Temp ≠ NULL
If Info(Temp) = x
Then
Print count
End If
Count = Count + 1
Temp = Link(Temp)
End While
End SEARCH( )

Display( )

The operation Display( ), displays all the value in each node of the list. A
temporary pointer is created and made to point to Head initially. Now info part of Temp
is printed and Temp is moved to the next node. This is repeated till the end of the list is
reached.

DISPLAY( x )

Temp = Head
While Temp ≠ NULL
Print Info(Temp)
End While
End DISPLAY( )

CIRCULAR LINKED LIST

Circular Linked List: Circular linked list is a linked list which consists of collection of
nodes each of which has two parts, namely the data part and the link part. The data part
holds the value of the element and the link part has the address of the next node. The last
node of list has the link pointing to the first node thus making the circular traversal
possible in the list.

Logical representation of the circular linked list:

DOUBLY LINKED LIST

1.10
Doubly linked list: The Doubly linked list is a collection of nodes each of which consists
of three parts namely the data part, prev pointer and the next pointer. The data part
stores the value of the element, the prev pointer has the address of the previous node and
the next pointer has the value of the next node.

In a doubly linked list, the head always points to the first node. The prev pointer
of the first node points to NULL and the next pointer of the last node points to NULL.

Operations on a Doubly linked list are,


1. Count the number of elements.
2. Add an element at the beginning of the list.
3. Add an element at the end of the list.
4. Insert an element at the specified position in the list.
5. Delete an element from the list.
6. Display all the elements of the list.

The Abstract Data Type of the linked list may be written as shown.

AbstractDataType DoublyLinkedList
{
instances
finite collection of zero or more elements linked by two pointers, one pointing the
previous node and the other pointing to the next node.
operations

Count( ): Count the number of elements in the list.


Addatbeg(x): Add x to the beginning of the list.
Addatend(x): Add x at the end of the list.
Insert(k, x): Insert x just after kth element.
Delete(k): Delete the kth element.
Search(x): Return the position of x in the list otherwise return -1 if not found
Display( ): Display all elements of the list
}

Algorithm:
The operations count( ), Search(x) and Delete(x) are similar to that of the singly
linked list.

Addatbeg(x)

The operation Addatbeg(x) adds a given element x at the beginning of the list. A
new node R is created and the value x is store in the data part of R. The prev pointer of R
is made to point to NULL and the next pointer is made to point to head. Then the prev
pointer of head is made to point to R and head is now moved to point to R making it the
first node. Thus the new node is added at the beginning of the doubly linked list.

1.11
ADDATBEG(x)

Info(R) = x
Prev(R) = NULL
Next(R) = head
Prev(head) = R
Head = R
End ADDATBEG( )

Addatend(x)

The Addatend(x) operation adds the given element x at the end of the doubly
linked list. If the given list is empty then create a new node R and store the value of x in
the data part of R. Now make the prev pointer and the next pointer of R point to NULL.
Then head is pointed to R. If the list already contains some elements then, a temporary
pointer is created and made to point to head. The temp pointer is now moved to the last
node and then a new node R is created. The value x is stored in the data part of R and
next pointer of R is made to point to NULL. The prev pointer of R is made to point to
temp and next pointer of Temp is made to point to R. Thus the new node is added at the
end of the doubly linked list.

1.12
ADDATEND(x)

If head = NULL
Then
Info(R) = x
Next(R) = NULL
Prev(R) = NULL
Head = R
Else
Temp = head
While next(temp) ≠ NULL
Temp = next(temp)
End While
Info(R) = x
Next(R) = NULL
Prev(R) = Temp
Next(Temp) = R
End If
End ADDATEND( )

Insert(x, k)

The Insert(x) operation inserts a given element x at the specified position k. A


temporary pointer is created and made to point to head. Then it is moved to the k-1 th
1.13
node. Now a new node R is created and the data part is stored with value of x. The next
pointer of R is made to point to next(temp) and the prev pointer of next(temp) is made to
point to R. Thus the links on the right side of the new node is established. Now the next
of Temp is made to point to R and the prev pointer of R is made to point to temp thus
establishing the links on the left side of the new node. Now the new node is inserted at
the position k.

INSERT(x, k)

Temp = head
I=1
While I < k -1
Temp = next (temp)
I=I+1
End while
Next(R) = Next(temp)
Prev(Next(Temp) = R
Next(Temp) = R
Prev(R) = Temp
End INSERT( )

1.14
Delete(x)

The Delete(x) operation deletes the element x from the doubly linked list. A
temporary pointer is created and made to point to head. Now the data of temp is
compared with x and if it matches that is the node to be deleted otherwise move to the
next node and again compare. If the node to be deleted is first node, then
prev(next(temp)) is made to point to NULL and head is pointed to next(temp). The node
pointed by temp is deleted. When the node to be deleted is not the first node, then
next(prev(temp)) is made to point to next(temp) and prev(next(temp)) is made to point to
prev(temp). The node pointed by temp is deleted.

DELETE(x)

Temp = head
While temp ≠ NULL
If x = info(temp)
Then
If head = temp
Then
Prev(next(temp) = NULL
Head = next(temp)
Delete temp
Else
Prev(next(temp)) = prev(temp)
Next(prev(temp))= next(temp)
1.15
Delete temp
End if
Else
Temp = next(temp)
End if
End while
End DELETE( )

STACKS

The data structures seen so far, allows insertion and deletion of elements at any
place. But sometimes it is required to permit the addition and deletion of elements only
at one end that is either at the beginning or at the end.

Stacks: A stack is a data structure in which addition of new element or deletion of an


existing element always takes place at the same end. This end is often known as top of
stack. When an item is added to a stack, the operation is called push, and when an item
is removed from the stack the operation is called pop. Stack is also called as Last-In-
First-Out (LIFO) list.

Operations on Stack:

There are two possible operations done on a stack. They are pop and push
operation.

 Push: Allows adding an element at the top of the stack.


 Pop: Allows removing an element from the top of the stack.

The Stack can be implemented using both arrays and linked lists. When dynamic
memory allocation is preferred we go for linked lists to implement the stacks.

ARRAY IMPLEMENTATION OF THE STACK

Push operation:

If the elements are added continuously to the stack using the push operation then
the stack grows at one end. Initially when the stack is empty the top = -1. The top is a
variable which indicates the position of the topmost element in the stack.

1.16
PUSH(x)

If top = MAX – 1
Then
Print “Stack is full”
Return
Else
Top = top + 1
A[top] = x
End if
End PUSH( )

Pop operation:

On deletion of elements the stack shrinks at the same end, as the elements at the
top get removed.

POP( )

If top = -1
Then
Print “Stack is empty”
Return
Else
Item = A[top]
A[Top] = 0
Top = top – 1
Return item
1.17
End if
End POP( )

If arrays are used for implementing the stacks, it would be very easy to manage
the stacks. However, the problem with an array is that we are required to declare the size
of the array before using it in a program. This means the size of the stack should be
fixed. We can declare the array with a maximum size large enough to manage a stack.
As result, the stack can grow or shrink within the space reserved for it. The following
program implements the stack using array.

LINKED LIST IMPLEMENTATION OF STACK

Initially, when the stack is empty, top points to NULL. When an element is added
using the push operation, top is made to point to the latest element whichever is added.

Push operation:

Create a temporary node and store the value of x in the data part of the node.
Now make link part of temp point to Top and then top point to Temp. That will make the
new node as the topmost element in the stack.

PUSH(x)

Info(temp) = x
Link(temp) = top
Top = temp
End PUSH( )

Pop operation

The data in the topmost node of the stack is first stored in a variable called item.
Then a temporary pointer is created to point to top. The top is now safely moved to the
next node below it in the stack. Temp node is deleted and the item is returned.

1.18
POP( )

If Top = NULL
Then
Print “Stack is empty”
Return
Else
Item = info(top)
Temp = top
Top = link(top)
Delete temp
Return item
End if
End POP( )

The following program implements the stack using linked lists.

APPLICATION OF STACKS

Conversion of Infix Expression to Postfix Expression

The stacks are frequently used in evaluation of arithmetic expressions. An


arithmetic expression consists of operands and operators. The operands can be numeric
values or numeric variables. The operators used in an arithmetic expression represent the
operations like addition, subtraction, multiplication, division and exponentiation.

The arithmetic expression expressed in its normal form is said to be Infix


notation, as shown:

A+B

The above expression in prefix form would be represented as follows:


1.19
+ AB

The same expression in postfix form would be represented as follows:

AB +

Hence the given expression in infix form is first converted to postfix form and then
evaluated to get the results.

The function to convert an expression from infix to postfix consists following steps:

1. Every character of the expression string is scanned in a while loop until the end of
the expression is reached.
2. Following steps are performed depending on the type of character scanned.
(a) If the character scanned happens to be a space then that character is
skipped.
(b) If the character scanned is a digit or an alphabet, it is added to the target
string pointed to by t.
(c) If the character scanned is a closing parenthesis then it is added to the
stack by calling push( ) function.
(d) If the character scanned happens to be an operator, then firstly, the
topmost element from the stack is retrieved. Through a while loop, the
priorities of the character scanned and the character popped ‘opr’ are
compared. Then following steps are performed as per the precedence rule.
i. If ‘opr’ has higher or same priority as the character scanned, then
opr is added to the target string.
ii. If opr has lower precedence than the character scanned, then the
loop is terminated. Opr is pushed back to the stack. Then, the
character scanned is also added to the stack.
(e) If the character scanned happens to be an opening parenthesis, then the
operators present in the stack are retrieved through a loop. The loop
continues till it does not encounter a closing parenthesis. The operators
popped, are added to the target string pointed to by t.
3. Now the string pointed by t is the required postfix expression.

Evaluation of Expression entered in postfix form

The program takes the input expression in postfix form. This expression is
scanned character by character. If the character scanned is an operand, then first it is
converted to a digit form and then it is pushed onto the stack. If the character scanned is
a blank space, then it is skipped. If the character scanned is an operator, then the top two
elements from the stack are retrieved. An arithmetic operation is performed between the
two operands. The type of arithmetic operation depends on the operator scanned from
the string s. The result is then pushed back onto the stack. These steps are repeated as
long as the string s is not exhausted. Finally the value in the stack is the required result
and is shown to the user.

1.20
QUEUE

Queue: Queue is a linear data structure that permits insertion of new element at one end
and deletion of an element at the other end. The end at which the deletion of an element
take place is called front, and the end at which insertion of a new element can take place
is called rear. The deletion or insertion of elements can take place only at the front or
rear end of the list respectively.

The first element that gets added into the queue is the first one to get removed
from the list. Hence, queue is also referred to as First-In-First-Out list (FIFO). Queues
can be represented using both arrays as well as linked lists.

ARRAY IMPLEMENTATION OF QUEUE

If queue is implemented using arrays, the size of the array should be fixed
maximum allowing the queue to expand or shrink.

Operations on a Queue

There are two common operations one in a queue. They are addition of an
element to the queue and deletion of an element from the queue. Two variables front and
rear are used to point to the ends of the queue. The front points to the front end of the
queue where deletion takes place and rear points to the rear end of the queue, where the
addition of elements takes place. Initially, when the queue is full, the front and rear is
equal to -1.

Add(x)

An element can be added to the queue only at the rear end of the queue. Before
adding an element in the queue, it is checked whether queue is full. If the queue is full,
then addition cannot take place. Otherwise, the element is added to the end of the list at
the rear side.

ADDQ(x)

If rear = MAX – 1
Then
Print “Queue is full”
Return
Else
Rear = rear + 1
A[rear] = x
If front = -1

1.21
Then
Front = 0
End if
End if
End ADDQ( )

Del( )

The del( ) operation deletes the element from the front of the queue. Before
deleting and element, it is checked if the queue is empty. If not the element pointed by
front is deleted from the queue and front is now made to point to the next element in the
queue.

DELQ( )

If front = -1
Then
Print “Queue is Empty”
Return
Else
Item = A[front]
A[front] = 0
If front = rear
Then
Front = rear = -1
Else
Front = front + 1
End if
Return item
End if
End DELQ( )

LINKED LIST IMPLEMENTATION OF QUEUE

Queue can be represented using a linked list. Linked lists do not have any
restrictions on the number of elements it can hold. Space for the elements in a linked list
is allocated dynamically; hence it can grow as long as there is enough memory available
for dynamic allocation. The queue represented using linked list would be represented as
shown. The front pointer points to the front of the queue and rear pointer points to the
rear of the queue.

1.22
Addq(x)

In linked list representation of queue, the addition of new element to the queue
takes place at the rear end. It is the normal operation of adding a node at the end of a list.

ADDQ(x)

If front = NULL
Then
Rear = front = temp
Return
End if
Link(rear) = temp
Rear = link(rear)
End ADDQ( )

Delq( )

The delq( ) operation deletes the first element from the front end of the queue.
Initially it is checked, if the queue is empty. If it is not empty, then return the value in the
node pointed by front, and moves the front pointer to the next node.

DELQ( )

If front = NULL
Print “Queue is empty”
Return
Else
While front ≠ NULL
Temp = front
Front = link(front)
Delete temp
End while
End if
End DELQ( )
-------End of Unit I-------

1.23

You might also like