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

8-Data Structures For II-PUC

The document discusses different types of data structures including primitive and non-primitive data structures. It describes linear and non-linear data structures. It provides details about arrays, including one-dimensional arrays. It discusses operations that can be performed on arrays such as traversing, searching, sorting, insertion and deletion. It provides algorithms to traverse, search, and insert elements into a linear array.

Uploaded by

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

8-Data Structures For II-PUC

The document discusses different types of data structures including primitive and non-primitive data structures. It describes linear and non-linear data structures. It provides details about arrays, including one-dimensional arrays. It discusses operations that can be performed on arrays such as traversing, searching, sorting, insertion and deletion. It provides algorithms to traverse, search, and insert elements into a linear array.

Uploaded by

Chandana M
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Data Structures St.Joseph’s Convent Girls P.

U College, Chickballpur

Data Structure:

Data Structure is a collection of organised data items which improve the efficiency of storage and
program

Classification of Data Structure

Primitive data structures:


 Data structures that are directly operated upon by machine-level instructions are known
as primitive data structures.
 The integers, real (float), logical data, character data, pointer and reference are primitive
data structures. 

Operations on primitive data structures

The various operations that can be performed on primitive data structures are:
Create: Create operation is used to create a new data structure. This operation reserves
memory space for the program elements. It can be carried out at compile time
and run-time.
For example, int x;
Destroy: Destroy operation is used to destroy or remove the data structures from the
memory space.

Select: Select operation is used by programmers to access the data within data
structure. This operation updates or alters data.
Update: Update operation is used to change data of data structures.
For example, int x = 2; Here, 2 is assigned to x.
Again, x = 4; 4 is reassigned to x.
The value of x now is 4 because 2 is automatically replaced by 4, i.e. updated.

Dept.Of Computer Science Page 1


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Non primitive data structures:


 Non primitive data structures are derived from the primitive data structures.
 They stress on formation of groups of homogeneous and heterogeneous data
elements.
 Non-primitive data structures are classified as arrays, lists and files.

Linear data structure


 This data structure has homogenous elements.
 Each element is referred to by an index.
 Stack, Queues and Linked Lists are the linear data structures.
 Elements of linear data structures will have linear relationship between them.
 One of the methods is to have linear relationship between elements by means of
sequential memory locations.
Non-linear data structure
 In non-linear data structure, a data item is connected to several other data items.
 The data item has the possibility to reach one or more data items.
 Every data item is attached to several other data items in a way that is specific for
reflecting relationships.
 The data items are not arranged in a sequential structure.
 Trees and Graphs are the examples of non-linear data structures.

Operations on linear data structure


The basic operations on non-linear data structures are as follows:
Traversal: The process of accessing each data item exactly once to perform some
operation is called traversing.

Insertion: The process of adding a new data item into the given collection of data
items is called insertion.

Deletion: The process of removing an existing data item from the given collection of
data items is called deletion.

Searching: The process of finding the location of a data item in the given collection
of data items is called as searching.

Sorting: The process of arrangement of data items in ascending or descending order


is called sorting.

Merging: The process of combining the data items of two structures to form a single
structure is called merging.

Dept.Of Computer Science Page 2


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Arrays
An array is a collection of homogeneous elements with same name and same type of
elements
Types of Arrays:
There are three types of arrays:
 One-dimensional Array
 Two-dimensional Array
 Multi-dimensional array

One-dimension Array
 An array is a collection of homogeneous elements with same name and same type of
elements, where each element is accessed by single index value.
 Syntax data type array_name[size];
int a[5];
Here, size specifies the number of elements in the array and the index (subscript)
values ranges from 0 to n-1.

Size of the array in memory


The size of the array in memory is given by:
Total size = Length * size of data type

For example, Consider the declaration, int a[10];


Total size = 10 x 2-bytes
= 20-bytes
Length of the array
It is given by Length= UB – LB + 1

Here, UB is the largest index, called the upper bound

LB is the smallest index, called the lower bound.

A[ 0 ] A[ 1 ] A[ 2 ] A[ 3 ] A[ 4 ]

LB UB

Dept.Of Computer Science Page 3


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Representation of Linear Arrays in memory:

 Let A is a linear array in the memory of the computer.


 The elements of the array are stored in successive memory locations.

 Using base address the computer calculates the address of any element of A by the
following formula:
LOC (A[POS]) = Base (A) + w (POS-LB)

Here, w = Number of words per memory cell.

A[0] A[1] A[2] A[3] A[4]


Consider the array A that contains 5 elements.
The position of the element A[3] is calculated as:

LOC(A[POS]) = Base(A) + w(POS – LB)


LOC(A[3]) = 2000 + 1(3 – 0)
= 2000 + 3

=2003

LOC(A[POS]) = Base(A) + w(POS– LB)


LOC(A[3]) = 2000 + 2(3 – 0)
= 2000 + 2(3)

=2006

Operations on one-dimensional array

Traversing: Accessing each element of the array exactly once to do some operation.
Searching: Finding the location of an element in the array.
Sorting: Arranging the elements of the array in some order.
Insertion: Inserting an element into the array.
Deletion: Removing an element from the array.
Merging: Combining one or more arrays to form a single array.

Dept.Of Computer Science Page 4


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Traversing a Linear Array


 Traversing is the process of visiting each subscript at least once from the beginning to
last element.
Algorithm:
Let A be a linear array with LB and UB as lower bound and upper
bound. This algorithm traverses the array A by applying the
operation PROCESS to each element of A. A[ 0 ] A[ 1 ] A[ 2 ] A[ 3 ] A[ 4 ]
Step 1: for I = 0 to N-1
PROCESS A[I] I

End of for
Step 2: Exit
Searching an element
 It refers to finding the location of the element in a linear array.
 The most common methods are linear search and binary search.
Linear Search
 The element to be searched is compared with each element of the array one by one from
the beginning till end of the array.
 Since searching is one after the other it is also called as sequential search or linear search.
Algorithm: A is the name of the array with N elements. ELE is the element to be searched.
This algorithm finds the location LOC where the search ELE element is
stored.
Step 1: LOC = -1
Step 2: for I = 0 to N-1
if( A[I] == ELE)
LOC =I 50 20 40 10 30
GOTO 3
End of if A[ 0 ] A[ 1 ] A[ 2 ] A[ 3 ] A[ 4 ]
End of for
Step 3: if(LOC >= 0) I
PRINT LOC LOC
else
PRINT “Search is unsuccessful”
Step 4: Exit

Dept.Of Computer Science Page 5


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Binary Search:

 This is the best method of searching.


 This method compares the search element with the middle element of the array.
 If the comparison does not match, the element is searched either at the right-half of the
array or at the left-half of the array.
Algorithm: A is the sorted array with LB as lower bound and UB as the upper bound
respectively.
Step 1: low=0, high=n-1, loc=-1
Step 2: while (low <= high)
mid= (low+high)/2
if(ELE= = A[mid])
loc = mid
GOTO 4
else
if(ele>A[mid])
low = mid+1
else
high= mid-1
End of while
Step 4: if(loc >= 0)
PRINT loc
else
PRINT “Search is unsuccessful”
Step 5: Exit

Insertion an element
 Insertion refers to inserting an element into the array.
 A new element can be inserted provided the array is large enough to accommodate the
new element.
 When an element is to be inserted into a particular position, all the elements from the
asked position to the last element should be shifted into the higher order positions.
 But the elements should be shifted in reverse order.
Algorithm: A is the array with N elements. ITEM is the element to be inserted in the
position P.
Step 1: for I = N-1 down to Pos 10 30 40 50 60
A[I+1] = A[I]
A[ 0 ] A[ 1 ] A[ 2 ] A[ 3 ] A[ 4 ]
End of for
Step 2: A[Pos] = ITEM
Step 3: N = N+1 P
Step 4: Exit

Dept.Of Computer Science Page 6


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Deleting an element from the array


 Deletion refers to removing an element into the array.
 When an element is to be deleted from a particular position, all the subsequent elements
are shifted into the lower order positions.
Algorithm: A is the array with N elements. ITEM is the element to be deleted in the position
and it is stored into the variable Item.
Step 1: Item = A[Pos]
Step 2: for I = Pos+1 to N-1 10 20 30 40 50
A[I-1] = A[I]
End of for A[ 0 ] A[ 1 ] A[ 2 ] A[ 3 ] A[ 4 ]
Step 3: N = N-1
Step 4: Exit P

Sorting the elements


 Sorting is the arrangement of elements of the array in some order.

Insertion Sort:
The first element of the array is assumed to be in the correct position. The next
element is considered as the key element and compared with the elements before the key
element and is inserted in its correct position.
Algorithm: Let A be an array with N unsorted elements. The following algorithm sorts the
elements in order.

Step 1: for i ← 1 to n-1


j←i
while j > =1
if(A[j-1] > A[j])
temp=a[j-1]
a[j-1]= a[j]
a[j]=temp
end if
Step2: j ← j - 1
end while
end for

Dept.Of Computer Science Page 7


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Two-dimensional arrays
 A two dimensional array is a collection of homogeneous elements and each element is
identified by a pair of indices called as subscripts.
 A two-dimensional array has m rows and n columns then the elements are accessed
using two indices I and J
 The number of rows and columns in a matrix is called as the order of the matrix and
denoted as m x n.
 The number of elements can be obtained by multiplying number of rows and number of
columns.
[0] [1] [2] [3]
A[0] I n t h e a b o v e fi g u r
3 A[1] x4 = 12-elements.
A[2]

Row-major order
Let A be the array of order m x n. In row-major order, all the first-row elements are
stored in sequential memory locations and then all the second-row elements are stored and
so on.
Base(A) is the address of the first element. The memory address of any element A[I]
[J] can be obtained by the formula LOC(A[I][J]) = Base(A) + W[n(I-LB) + (J-LB)]
Here, W is the number of words per memory location.
Example: Consider the array of order 3 x 3.
[0] [1] [2]
A[0]

A[1]
A[2]

The location of the element A[2][1] is calculated


for w = 1 as follows:
LOC(A[I][J]) = Base(A)+w[n(I-LB)-(J-LB)]
LOC(A[2][1]) = 2000 + 1[3(2-0) + (1-0)]
= 2000 + 3(2)+1
= 2007

Dept.Of Computer Science Page 8


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Applications of arrays
 Arrays are used to implement other data structures such as heaps, hash tables,
queues, stacks and strings etc.
 Arrays are used to implement mathematical vectors and matrices.
 Many databases include one-dimensional arrays whose elements are records.
Advantages of arrays
 It is used to represent multiple data items of same type by using only single
name.
 It can be used to implement other data structures like linked lists, stacks, queues,
trees, graphs etc.
 Two-dimensional arrays are used to represent matrices.

Disadvantages of arrays
 Array is static structure. It means that array is of fixed size. The memory which is
allocated to array cannot be increased or reduced.
 Since array is of fixed size, if we allocate more memory than requirement then
the memory space will be wasted. If we allocate less memory than requirement,
then it will create problem.
 The elements of array are stored in consecutive memory locations. So insertions
and deletions are very difficult and time consuming.

Dept.Of Computer Science Page 9


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Stack
 A stack is an ordered collection of items where the insertion and removal of an item
always take place at only one end.
 This end is commonly referred to as the “top”.
 This ordering principle is sometimes called LIFO, last-in first-out.

B O O K S

TOP

Representation of stacks in memory


The representation of a stack in the memory can be done in two ways.
 Static representation using arrays
 Dynamic representation using linked lists

Array representation of a stack


 Stack can be represented using a one-dimensional array.
 A block of memory is allocated to store the items to the full capacity of the stack.
 The items into the stack are stored in a sequential order from the first location of the
memory block.
 A pointer TOP contains the location of the top element of the stack.
 A variable N contains the maximum number of elements that can be stored in the
stack.
 The condition TOP ==N-1 indicates that the stack is full (Overflow)
 TOP = =NULL indicates that the stack empty.
PUSH POP

10 2O 3O

S
Storage of elements in this direction TOP K
TOP O
O
B

Dept.Of Computer Science Page 10


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Operations performed on Stack

stack(): Creates a new stack that is empty. It needs no parameters and returns an
empty stack.

push(item): Adds a new item to the top of the stack. It needs the item and returns
nothing.
pop(): Removes the top item from the stack. It needs no parameters and returns
the item. The stack is modified.
peek(): Returns the top item from the stack but does not remove it. It needs no
parameters. The stack is not modified.
isEmpty(): Tests whether the stack is empty. It needs no parameters and returns a
Boolean value.
size(): Returns the number of items on the stack. It needs no parameters and
returns an integer.

Algorithm for PUSH Operation


Here, STACK Array that contains N elements.
TOP Pointer to the top element of the array
ITEM Element to be inserted

Step 1: If (TOP == N-1) then [check overflow]


PRINT “Stack is full”
Exit
End of If
Step 2: TOP = TOP + 1 [Increment the TOP]
Step 3: STACK[TOP] = ITEM [Insert the ITEM]
Step 4: Return

Algorithm for POP Operation

Here, STACK Array that store N items.


TOP Pointer to the top element of the array
Step 1: If (TOP ==-1) then [check underflow]
PRINT “Stack is empty”
Exit
End of If
Step 2: ITEM = STACK[TOP] [Copy the top element]
Step 3: TOP = TOP – 1 [Decrement the top]
Step 4: Return

Dept.Of Computer Science Page 11


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Application of Stacks

 The simplest application of a stack is to reverse a word.


 Another application is an “undo” mechanism in text editors;
 Backtracking: This is a process when you need to access the most recent data
element in a series of elements.
 Language processing:
 Conversion of decimal number into binary
 To solve tower of Hanoi
 Expression evaluation and syntax parsing
 Conversion of infix expression into prefix and postfix.
 Rearranging railroad cars
 Quick sort

Infix to Postfix Expression

1. Print operands as they arrive.

2. If the stack is empty or contains a left parenthesis on top, push the incoming
operator onto the stack.

3. If the incoming symbol is a left parenthesis, push it on the stack.

4. If the incoming symbol is a right parenthesis, pop the stack and print the operators
until you see a left parenthesis. Discard the pair of parentheses.

5. If the incoming symbol has higher precedence than the top of the stack, push it on
the stack.

6. If the incoming symbol has equal precedence with the top of the stack, use
association. If the association is left to right, pop and print the top of the stack and
then push the incoming operator. If the association is right to left, push the incoming
operator.

7. If the incoming symbol has lower precedence than the symbol on the top of the
stack, pop the stack and print the top operator. Then test the incoming operator
against the new top of stack.

8. At the end of the expression, pop and print all operators on the stack. (No
parentheses should remain.)

Dept.Of Computer Science Page 12


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Example

A- B + C becomes A B - C +

  current symbol operator stack postfix string

       

1 A   A

2 - - A

3 B - AB

4 + + AB-

5 C + AB-C

6     AB-C+

Dept.Of Computer Science Page 13


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Queues
 A queue is an ordered collection of items where insertion of items and removal of
items always take place at different ends.
 In a queue, two operations are allowed - enqueue and dequeue.
 Enqueueis the process of inserting anitem into the queue.
 Dequeueis the process of removing an item from the queue.
 Queue is also called as FIFO list, i.e. First-In-First-Out.
 Insertions are made at the end called the rear and deletions are made at the end
called front.

Types of queues
Queue can be of four types:

 Simple Queue
 Circular Queue
 Priority Queue
 Dequeue (Double Ended queue)

Simple Queue:In Simple queue, insertion occurs at the rear end of the list, and deletion
occurs at the front end of the list.

FRONT REAR

FRONT = 1
A B C D E
REAR = 5
0 1 2 3 4 5 6 7 8

Circular Queue: A circular queue is a queue in which all items are treated as circular such
that the last item follows the first item.

FRONT
REAR

FRONT = 1
F D E A B C
REAR = 5
0 1 2 3 4 5 6 7 8

Dept.Of Computer Science Page 14


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Priority Queue: A priority queue is a queue that contains items that have some preset
priority. An element can be inserted or removed from any position depending on some
priority.

Dequeue (Double Ended queue):

It is a queue in which insertion and deletion takes place at both the ends.

Insertion Insertion
Deletion Deletion

Operations on queue

Queue() creates a new queue that is empty. It needs no parameters and returns
an empty queue.

Enqueue(item) adds a new item to the rear of the queue. It needs the item and
returns nothing.

Dequeue() removes the front item from the queue. It needs no parameters and
returns the item. The queue is modified.

isEmpty() tests to see whether the queue is empty. It needs no parameters and
returns a boolean value.

size() returns the number of items in the queue. It needs no parameters and
returns an integer.
Memory representation of a queue using arrays
 Queue is represented in memory using linear array.
 Let QUEUE is a linear queue.
 Two pointer variables called FRONT and REAR are maintained.
 FRONT contains the location of the element to be removed
 REAR contains location of the last element inserted.
 The condition FRONT = =NULL indicates that the queue is empty
REAR == N -1 indicates that the queue is full

10 2O 3O 40
Deletion Insertion

FRONT REAR FRONT REAR


Dept.Of Computer Science Page 15

FRONT = 1
A B C D E
REAR = 5
0 1 2 3 4 5 6 7 8
Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Inserting a item into the queue (Enqueue)


Algorithm:
Here, QUEUE Linear array consisting of N elements.
FRONT Pointer that contains the location of the element to be deleted
REAR Pointer that contains the location of the last element
ITEM Element to be inserted

Step 1: If (REAR= = N-1) Then [Check for overflow]


PRINT “Overflow”
Exit
IF END
Step 2 : If (FRONT = =NULL) Then [Check whether QUEUE is empty]
FRONT = 0
REAR = 0
Else
REAR = REAR + 1 [Increment REAR Pointer]
Step 3: QUEUE[REAR] = ITEM [Copy ITEM to REAR position]
Step 4: Return

Algorithm for dequeue(delete)

Here, QUEUE Linear array consisting of N elements.


FRONT Pointer that contains the location of the element to be deleted
REAR Pointer that contains the location of the last element

Step 1: If (FRONT == NULL) Then [Check whether QUEUE is empty]


PRINT “Underflow”
Exit
IF END
Step 2: ITEM = QUEUE[FRONT]
Step 3: If( FRONT == REAR) Then [If QUEUE has only one element]
FRONT = NULL
REAR = NULL
Else
FRONT = FRONT + 1 [Increment FRONT pointer]
Step 3: Return

Dept.Of Computer Science Page 16


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Applications of queues
 Simulation
 Various features of operating system
 Multi-programming platform systems
 Different type of scheduling algorithm
 Round robin technique or Algorithm
 Printer server routines
 Various applications software is also based on queue data structure
 Operating systems often maintain a queue of processes that are ready to execute or
that are waiting for a particular event to occur.

Dept.Of Computer Science Page 17


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

LINKED LISTS

 Disadvantage of using arrays is that arrays are static structures and therefore cannot be
easily extended or reduced to fit the data set.
 Linked Lists addresses these limitations of arrays. The linked list uses dynamic memory
allocations.

A linked list is a linear collection of data elements called nodes and the linear order is
given by means of pointers.
 Each node contains two fields: the data field and link field.

Data Link

 The data field contains the information and the link field contains the address of the
next node in the list.
 Here is a linked list with 4 with nodes. A pointer START gives the location of the first
node.
 This pointer is also called as HEAD.
 The link field of the last node contains NULL.
START

50 40 70 20 NULL

Types of linked lists


There are three types of linked lists.
1. Singly linked list (SLL)
2. Doubly linked list (DLL)
3. Circular linked list (CLL)
Single linked list
 There is only one link field in each node.
 A singly linked list contains two fields in each node – the data field and link field.
 The data field contains the data of that node and the link field contains address of the
next node.

Dept.Of Computer Science Page 18


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

START

50 40 70 20 NULL

Circular linked lists


 The link field of the last node contains the address of the first node.
 In a circular linked list, it is possible to reach any node from any other node.

STAR
T
Header node

Doubly linked lists

 Each node is points both to the next node and also to the previous node.
 In doubly linked list each node contains three parts – FORW, BACK and INFO.

INFO

BACK: It is a pointer field containing the address of the previous node.


FORW:It is a pointer field that contains the address of the next node.
INFO:It contains the actual data.
 In the first node, if BACK contains NULL and FORW field of the last node contains NULL.

Operations on linked lists:


 The operations that are performed on linked lists are:
 Creating a linked list
 Traversing a linked list
 Inserting an item into a linked list

Dept.Of Computer Science Page 19


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

 Deleting an item from the linked list


 Searching an item in the linked list
 Merging two or more linked lists

Creating a linked list


 We start with a node and create nodes, link to the starting node sequentially.
 The pointer START contains the location of the first node.
 Also the link field of the last node contains NULL.
 Elements can be inserted anywhere in the linked list and any node can be deleted.
 The nodes of a linked list can be created by the following structure declaration.
struct Node
{
int data;

Node *link;
}
Node *node1, *node2;

 The above declaration creates two variable structures, node1 and node2.
 Each structure will be taken as node.

Traversing a linked list


 Traversing is the process of accessing each node of the linked list exactly once to perform
some operation.
 To traverse a linked list followed the steps:
1. Move to the first node.
2. Fetch the data from the node and perform the required operation.
3. Advance the pointer to the next node.
4. Repeated Step 2 and step 3 until all the nodes are visited.

Algorithm Here, START Pointer contains the address of the first node.
p Pointer temporarily used to visit all the nodes from the
beginning to the end.

Step 1: p = START [Initialize p to first node]


Step 2: while p !=NULL
Step 3: PROCESS data(p) [Fetch the data]
Step 4: p = link(p) [Advance p to next node]
Step 5: End of while
Step 6: Return

Dept.Of Computer Science Page 20


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Algorithm (inst-beg):This algorithm inserts a node at the beginning of the linked list.
Step 1. p  new Node
Step 2. data(p) num
Step 3. link(p)  START
Step 4. START  p
Step 5. Return

Inserting a node at the end of the linked list


To insert at the end, we have to traverse the liked list to know the address of the last
node.
Algorithm (inst-end) START

Step 1. START 50 40 70

Step 2. [Identify the last node in the list]


p  START 20 NULL
New node
while p != NULL
p next(p)
while end
Step 3. N  new Node [Create new node copy the address to the pointer N]
Step 4. data(N) item
Step 5. link(N) NULL
Step 6. link(p)  N
Step 7. RETURN

Inserting a node at a given position


Algorithm (INST-POS): This algorithm inserts item into the linked list at the given position.
Step 1. START
Step 2. Count 0
p1  START START

Step 3. while (p1 !=NULL) 50 40 20 NUL


countcount +1
p1 link(p1) 70

while end New node

Step 4. if (pos = 1)

Dept.Of Computer Science Page 21


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

call function INST-BEG()


else
if (pos = count +1)
call function INST-END()
else
if( pos<= count)
p1  START
for i = 1 to pos
p1 next(p1)
for end
p2  new node
data(p2)  item
link(p2)  link(p1)
link(p1)  p2
else
PRINT “ Invalid position “
Step 5. RETURN
Algorithm (DELE-BEG): This algorithm first copy data in the first node to a variable and
deletes the first node of the linked list.
Step 1. START
Step 2. p START
Step 3. PRINT data(p)
Step 4. START link(p)
Step 5. free(p)
Step 6. RETURN

Deleting a node at the end


Algorithm (DELE-END)
 This used two pointers p1 and p2.
 Pointer p1 is used to traverse the linked list and pointer p2 keeps the location of the
previous node of p1.
Step 1. START
Step 2. p2 START
Step 3. while ( link(p2) != NULL)
p1  p2
p2 link(p2)
while end
Step 4. PRINT data(p2)
Step 5. link(p1)  NULL
Step 6. STOP

Dept.Of Computer Science Page 22


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Deleting a node at the given position


To delete a node at the given position, the following procedure is used.
1. Find location of the node to be deleted and the previous node.
2. Delete the node by changing the location of the previous node of the node to be
deleted to the next node of the node to be deleted.
Algorithm (DELE-POS)
Here, p2 Pointer is used to traverse the linked list.
Pointer that contains the location of the node to be deleted
p1 keeps the location of previous node of p2.

Step 1. START
Step 2. count 0
p1  START
Step 3. while(p1!=NULL)
count = count +1
p1link(p1)
while end
Step 4. if(pos = =1)
CALL DELE-BEG()
else
if(pos = =count)
CALL DELE-END()
else
if(pos<= count)
p2  START
for i = 1 to pos
p1  p2
p2 link(p2)
for end
PRINT data(p2)
link(p1) link(p2)
free(p2)
if end
Step 5. RETURN

Dept.Of Computer Science Page 23


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Non-linear data structure

 A non-linear data structure is a data structure in which a data item is connected to


several other data items, so that a given data item has the possibility to reach one-or-
more data items.
 The data items in a non-linear data structure represent hierarchical relationship. Each
data item is called a node.
 Examples of non-linear data-structures are Graphs and Trees.

Pros 1 Uses memory efficiently as contiguous memory is not required for allocating
data items.
2 The length of the data items is not necessary to be known prior to allocation.
Cons Overhead of the link to the next data item.

TREES
A tree is a data structure consisting of nodes organized as a hierarchy - see figure.
Terminology
 A node is a structure which may contain a value, a condition, or represent a separate
data structure.
 Each node in a tree has zero or more child nodes
 A node that above the child is called the parent node.
 A node has at most one parent.
 Nodes that do not have any children are called leaf nodes.
They are also referred to as terminal nodes.
 The height of a node is the length of the longest
downward path to a leaf from that node.
 The height of the root is the height of the tree.
 The depth of a node is the length of the path to its root (i.e., its root path).
 The topmost node in a tree is called the root node. The root node will not have parent.
 An internal node or inner node is any node of a tree that has child nodes and is thus not
a leaf node.
 A subtree of a tree T is a tree consisting of a node in T and all of its descendants in T.

Dept.Of Computer Science Page 24


Data Structures St.Joseph’s Convent Girls P.U College, Chickballpur

Binary trees
 The simplest form of tree is a binary tree.
 A binary tree is a tree in which each node has at most two
descendants - a node can have just one but it can’t have more
than two.
 A binary tree consists of:
a. a node (called the root node) and
b. left and right sub-trees.
Both the sub-trees are themselves binary trees.

Root Node
 Node at the “top” of a tree - the one from which all operations on the tree begins.
 The root node may not exist (a NULL tree with no nodes in it) or have 0, 1 or 2 children
in a binary tree.
Leaf Node Node at the “bottom” of a tree - farthest from the root.
Leaf nodes have no children.
Complete Tree Tree in which each leaf is at the same distance from the root. i.e. all the
nodes have maximum two subtrees.
Height Number of nodes which must be traversed from the root to reach a leaf of a
tree.
GRAPHS
 A graph is a set of vertices and edges which connect them.
 A graph is a collection of nodes called vertices, and the
connections between them, called edges.
Undirected and directed graphs
 When the edges in a graph have a direction, the graph is called a directed
graph or digraph, and the edges are called directed edges or arcs.
Neighbours and adjacency
 A vertex that is the end-point of an edge is called a neighbour of the vertex. i.e., its
starting-point.
 The first vertex is said to be adjacent to the second.
 The diagram shows a graph with 5 vertices and 7 edges.
 The edges between A and D and B and C are pairs that make a bidirectional connection,
represented here by a double-headed arrow.

Dept.Of Computer Science Page 25

You might also like