Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

Unit 1

List

Data Structure
• Way of organizing and storing data in a computer system
• This organization is done under a common name.
• Depicts the logical representation of data in computer memory.

Data Structure can be defined as the group of data elements which provides an efficient way
of storing and organising data in the computer so that it can be used efficiently. Some
examples of Data Structures are arrays, Linked List, Stack, Queue, etc. Data Structures are widely
used in almost every aspect of Computer Science i.e. Operating System, Compiler Design,
Artificial intelligence, Graphics and many more.

Types of Data Structure

Arrays
 Collection of similar type data elements stored at consecutive locations in the
memory.

 Advantages
⚫ Individual data elements can be easily referred.
 Limitations
⚫ Wastage of memory space.
Linked Lists
 To store data in the form of a list of nodes connected to each other through
pointers.
 Each node has two parts – data and pointer to next data

 Advantages
⚫ Optimize the use of storage space.
 Limitations
⚫ Individual elements cannot be referred directly

Stacks
 Maintains a list of elements in a manner that elements can be inserted or
deleted onlyfrom one end (referred as top of the stack) of the list.


 LIFO-Last In First Out principle.
 Applications
⚫ Implementations of system processes like program control, recursion control

Queues
 Maintains a list of elements such that insertion happens at rear end and deletion
happensat front end.
 FIFO – First In First Out principle
Trees
 Represent data containing hierarchical relationship between elements.
 Example: family trees, records and table of contents.
Applications
⚫ Implementing search algorithms

Graphs
 It is a linked data structure that comprises of vertices and a group of edges.
 Edges are associated with certain values called weights.
 Helps to compute the cost of traversing the graph through a certain path.


1.1 ABSTRACT DATA TYPE
 An abstract data type (ADT) is a set of operations and mathematical abstractions,
which can be viewed as how the set of operations is implemented. 
 Objects like lists, sets and graphs, along with their operation, can be viewed as abstract
data types, just as integers, real numbers and Booleans.
 For example, a data structure and its operations can be packaged together into an entity
called an ADT.
 The lower-level implementation details of the data structure are hidden from view of the
rest of the program.
Features of ADT

o Modularity
 Divide program into small functions
 Easy to debug and maintain
 Easy to modify
o Reuse
 Define some operations only once and reuse them in future 
o Easy to change the implementation
1.2 Array Implementation of List ADT
Array is a collection of specific number of data stored in consecutive memory locations.

20 10 30 40 50 60
A[0] A[1] A[2] A[3] A[4] A[5]
Operations on Array

 Insertion
 Deletion
 Merge
 Traversal
 Find

Insertion Operation on Array


 It is the process of adding an element into the existing array. It can be done at
anyposition.
 Insertion at the end is easy as it is done by shifting one position
towards right of lastelement.
 Shift the element (10) one position right (from location 1 to 2)

20 10 30
A[0] A[1] A[2] A[3] A[4] A[5]
Now insert element (40) at location 1
20 40 10 30
A[0] A[1] A[2] A[3] A[4] A[5]
• Routine to insert an element in an array
void insert(int X, int P, int A[], int N)
{
if(P==N)
printf(“Array overflow”);
else
{
for(int i=N-1;i>=P;i--)
A[i+1]=A[i];
A[P]=X;
N=N+1;
}
}
Deletion operation on an Array
• It is the process of removing an element from the array at any position
• Routine
int deletion(int P,int A[],int N)
{
if (N==0)
{
printf(“Empty List – Underflow”);
return;
}
else if(P==N-1)
temp=A[P];
else
{
for(i=P;i<N-1;i++)
A[i]=A[i+1];
}
N=N-1;
return temp;
}
Merits and demerits of array implementation of lists
Merits
• Fast, random access of elements
• Memory efficient – very less amount of memory is required
Demerits
• Insertion and deletion operations are very slow since the elements should be moved.
• Redundant memory space – difficult to estimate the size of array.

1.3 THE LIST ADT


List is an ordered set of elements.
The general form of the list is
A1, A2, A3, ,AN
A1 - First element of the list
AN - Last element of the list
N - Size of the list
If the element at position i is Ai then its successor is Ai+1 and its predecessor is Ai-1.
Various operations performed on List
1. Insert (X, 5) - Insert the element X after the position 5.
2. Delete (X) - The element X is deleted
3. Find (X) - Returns the position of X.
4. Next (i) - Returns the position of its successor element i+1.
5. Previous (i) - Returns the position of its predecessor i-1.
6. Print list - Contents of the list is displayed.
7. Makeempty - Makes the list empty.

1.4 LINKED LIST IMPLEMENTATION


Linked list consists of series of nodes. Each node contains the element and a pointer to its
successor node. The pointer of the last node points to NULL.

DATA NEXT
ELEMENT POINTER

NODE
Insertion and deletion operations are easily performed using linked list.
Types of Linked List
1. Singly Linked List 2. Doubly Linked List 3. Circular Linked List.
1.5 SINGLY LINKED LIST
A singly linked list is a linked list in which each node contains only one link field pointing
to the next node in the list.

700 1000 800

10 20 30 40

550 700 1000 800

Fig. 1.5 (a) Linked List

Header L

700 1000 800


10 20 30 40
550 700 1000 800

Fig. 1.5 (b) Linked List with a Header


Linear Data Structures - Lists 1.3

Declaration for Linked List

typedef struct Node *List ;


typedef struct Node *Position ;
Struct Node
{
int element ;
position Next ;
};

Routine to insert an element in the list

void Insert (int X, List L, Position P)


/* Insert after the position P*/
{

position Newnode;
Newnode = malloc (size of (Struct Node));If
(Newnode! = NULL)
{
Newnode  Element = X;

Newnode  Next = P Next;


P Next = Newnode;
}
}
550
Header L
700 1000 800
10 20 30 40

550 700 1000 800


1000
25
1200 Newnode
1.4 Data Structures

Routine to check whether the list is empty

int IsEmpty (List L) /*Returns 1 if L is empty */


{
if (L Next = = NULL)
return (1);
}

HEADER L

Empty List

ROUTINE TO CHECK WHETHER THE CURRENT POSITION IS LAST

int IsLast (position P, List L) /* Returns 1 is P is the last position in L


*/
{
if (P Next = = NULL)
return (1);
L 10 20 25
P
}
Linear Data Structures - Lists 1.5
Find Routine
position Find (int X, List L)

/*Returns the position of X in L; NULL if X is not found */

position P;

P = L  Next;

while (P! = NULL && P Element ! = X)

P = P Next;

return P;

L 10 20 25
P X

}
Find Previous Routine

position FindPrevious (int X, List L)

/* Returns the position of the predecessor */

position P;

P = L;

while (P  Next ! = Null && P Next Element ! = X)

P = P  Next;

return P;

L 10 20 25
P X
1.6 Data Structures
Findnext Routine
position FindNext (int X, List L)
{
/*Returns the position of its successor */
P = L  Next;
while (P Next! = NULL && P Element ! = X)
P = P Next;
return P Next;
}

L 10 20 25

X, P P Next

Routine to delete an element from the list

void Delete(int X, List L)


{
/* Delete the first occurence of X from the List */
position P, Temp;
P = Findprevious (X,L);
If (!IsLast(P,L))
{
Temp = P Next;
P Next = Temp Next;
Free (Temp);
}
}

X
L 10 25 20

P Temp

BEFORE DELETION

L 10 20

AFTER DELETION
Linear Data Structures - Lists 1.7
Routine to delete the list

void DeleteList (List L)


{
position P, Temp;
P = L Next;
L Next = NULL;
while (P! = NULL)
{
Temp = P Next
free (P);
P = Temp;
}
}

Header 10 20 L Next = NULL

L P

10 20 Temp = P Next

P Temp

20 free (P)
P P = Temp

Temp, P = NULL

1.6 CIRCULAR LINKED LIST


In circular linked list the pointer of the last node points to the first node. Circular linked list
can be implemented as Singly linked list and Doubly linked list with or without headers.

Singly Linked Circular List

A singly linked circular list is a linked list in which the last node of the list points to the first
node.
1.8 Data Structures

Header

10 20 30

Fig. 1.6.1 Singly Linked Circular List With Header

Doubly Linked Circular List

A doubly linked circular list is a Doubly linked list in which the forward link of the last node
points to the first node and backward link of the first node points to the last node of the list.

10 20 30

Fig. 1.6.2 Doubly Linked Circular List With Header


Advantages of Circular Linked List
• It allows to traverse the list starting at any point.
• It allows quick access to the first and last records.
• Circularly doubly linked list allows to traverse the list in either direction.
1.7 DOUBLY LINKED LIST
A Doubly linked list is a linked list in which each node has three fields namely data field,
forward link (FLINK) and Backward Link (BLINK). FLINK points to the successor node in the
list whereas BLINK points to the predecessor node.

DATA
BLINK ELEMENT FLINK

Fig. 1.7 (a) Node in doubly linked list


Linear Data Structures - Lists 1.9

Header

10 20 30 40

Fig. 1.7 (b) Doubly Linked List

Structure Declaration : -
Struct Node

int Element;

Struct Node *FLINK;

Struct Node *BLINK

};
Routine to insert an element in a doubly linked list
void Insert (int X, list L, position P)

Struct Node * Newnode;

Newnode = malloc (size of (Struct Node));

If (Newnode ! = NULL)

Newnode  Element = X;

Newnode  Flink = P  Flink;

P  Flink Blink = Newnode;

P  Flink = Newnode ;

Newnode Blink = P;

}
1.10 Data Structures

Header

10 20 30

15

Routine to delete an element


void Delete (int X, List L)
{
position P;
P = Find (X, L);
If ( IsLast (P, L))
{
Temp = P;
P  Blink  Flink = NULL;
free (Temp);
}
else
{
Temp = P;
P  Blink  Flink = P  Flink;
P  Flink Blink = P Blink;
free (Temp);
}
}
Linear Data Structures - Lists 1.11

Header

10 20 30

Advantage

* Deletion operation is easier.

* Finding the predecessor & Successor of a node is easier.

Disadvantage

* More Memory Space is required since it has two pointers.

1.8 APPLICATIONS OF LINKED LIST


1. Polynomial ADT
2. Radix Sort
3. Multilist
Polynomial ADT
We can perform the polynomial manipulations such as addition, subtraction and
differentiation etc.
Declaration for linked list implementation of polynomial ADT
Struct poly
{
int coeff ;
int power;
Struct poly *Next;
}*list 1, *list 2, *list 3;
Creation of the Polynomial
poly create (poly *head1, poly *newnode1)
{
poly *ptr;
if (head1= =NULL)
1.12 Data Structures
{
head1 = newnode1;
return (head1);
}
else
{
ptr = head1;
while (ptr next ! = NULL)
ptr = ptr next;
ptr next = newnode1;
}
return (head1);
}
Addition of two Polynomials
void add ( )
{
poly *ptr1, *ptr2, *newnode;
ptr1 = list1;
ptr2 = list2;
while (ptr1! = NULL && ptr2! = NULL)
{
newnode = malloc (sizeof (Struct poly));
if (ptr1 power = = ptr2 power)
{
newnode coeff = ptr1 coeff + ptr2 coeff;
newnode power = ptr1 power;
newnode next = NULL;
list 3 = create (list3, newnode);
ptr1 = ptr1 next;
ptr2 = ptr2 next;
}
else
Linear Data Structures - Lists 1.13

{
if (ptr1 power > ptr2 power)
{
newnode coeff = ptr1 coeff;
newnode power = ptr1 power;
newnode next = NULL;
list3 = create (list3, newnode);
ptr1 = ptr1 next;
}
else
{
newnode coeff = ptr2 coeff;
newnode power = ptr2 power;
newnode next = NULL;
list3 = create (list3, newnode);
ptr2 = ptr2 next;
}
}
}

Subtraction of two polynomial


void sub ( )
{
poly *ptr1, *ptr2, *newnode;
ptr1 = list1 ;
ptr2 = list 2;
while (ptr1! = NULL && ptr2! = NULL)
{
newnode = malloc (sizeof (Struct poly));
if (ptr1 power = = ptr2 power)
{
newnode coeff = (ptr1 coeff) - (ptr2 coeff);
1.14 Data Structures
newnode power = ptr1 power;
newnode next = NULL;
list3 = create (list 3, newnode);
ptr1 = ptr1 next;
ptr2 = ptr2 next;
}
else
{
if (ptr1 power > ptr2 power)
{
newnode coeff = ptr1 coeff;
newnode power = ptr1 power;
newnode next = NULL;
list 3 = create (list 3, newnode);
ptr1 = ptr1 next;
}
else
{
newnode coeff = - (ptr2 coeff);
newnode power = ptr2 power;
newnode next = NULL;
list 3 = create (list 3, newnode);
ptr2 = ptr2 next;
}
}
}
}

Polynomial Differentiation
void diff ( )
{
poly *ptr1, *newnode;
ptr1 = list 1;
while (ptr1 ! = NULL)
{
Linear Data Structures - Lists 1.15

newnode = malloc (sizeof (Struct poly));


newnode coeff = ptr1 coeff *ptr1 power;
newnode power = ptr1 power - 1;
newnode next = NULL;
list 3 = create (list 3, newnode);
ptr1 = ptr1 next;
}
}
Radix Sort : - (Or) Card Sort
Radix Sort is the generalised form of Bucket sort. It can be performed using buckets from
0 to 9.
In First Pass, all the elements are sorted according to the least significant bit.
In second pass, the numbers are arranged according to the next least significant bit and so
on this process is repeated until it reaches the most significant bits of all numbers.
The numbers of passes in a Radix Sort depends upon the number of digits in the numbers
given.
PASS 1 :
INPUT : 25, 256, 80, 10, 8, 15, 174, 187

10 15
80 174 25 256 187 8
0 1 2 3 4 5 6 7 8 9
Buckets
After Pass 1 : 80, 10, 174, 25, 15, 256, 187, 8
PASS 2 :
INPUT : 80, 10, 174, 25, 15, 256, 187, 8

15 187
8 10 25 256 174 80
0 1 2 3 4 5 6 7 8 9

After Pass 2 : 8, 10, 15, 25, 256, 174, 80, 187


1.16 Data Structures
PASS 3 :
INPUT : 8, 10, 15, 25, 256, 174, 80, 187

80
25
15
10 187 256
8 175
0 1 2 3 4 5 6 7 8 9

After pass 3 : 8, 10, 15, 25, 80, 175, 187, 256


Maximum number of digits in the given list is 3. Therefore the number of passes required
to sort the list of elements is 3.
Multi Lists
More complicated application of linked list is multilist. It is useful to maintain student
registration, Employee involved in different projects etc., Multilist saves space but takes more
time to implement.

E1 E2 E3

P1

P2

Fig. 1.8 Multilist Implementation For Employee Project Allocation


An employee can involve in any number of projects and each project can be implemented
by any number of employees.
Employee E1 is working on project P1, E2 is working on project P2 & E3 is working on
project P1.
Project P1 is implemented by the Employees E1 & E3. Project P2 is implemented by the
Employee E2.
Linear Data Structures - Lists 1.17
1.9 POLYNOMIAL MANIPULATION
1.9.1 Create & Insert a Polynomial
Step 1.

6x5 10x0 10x9


temp  next = current start = temp;
Start
NULL
Start 100
(-6, 5) 100 5

Step 2 : (10, 9)

100 5

Temp
200 9

start
200 9 5
1.18 Data Structures

Step 3 : (10, 0)
start
200 9 5

temp
600 0

PN* Insert_poly (PN* start, int coeff, int exp)

PN* temp, *current, *prev;

current = prev = start;

while (current ! = NULL)

if (ex > current  exp)


break ;
prev = current;
current = current  next;
}
temp = (PN*) malloc (size of (PN))
temp  exp = ex; temp  coeff = coeff;

if (start = = current)
start temp;
else

prev  next = temp;


return start;
}
Example :

head 0 1 2 3 NULL
Linear Data Structures - Lists 1.19

1.9.2 Deleting Polynomial Operations


Void erase (polypointer *ptr)

/* erase the polynomial pointer to by ptr*/

polypointer temp;

while (*ptr)

temp = *ptr;

*ptr = (*ptr)  link;

free (temp);

When a polynomial objects / ptr goes out of scope, The system first checks to see if a
destructor exists for that class.

If so, the destructor is executed. We have not defined a destructor on polynomial, so a


destructor is not executed. Next if the class object contains other class objects as data members,
they are deleted. Polynomial contains a single data member poly of type List. Since a destructor
is defined on List, it is executed on Poly. Finally, the data members of Poly are deleted.

You might also like