CS3301 - DS Unit 1 New
CS3301 - DS Unit 1 New
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.
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
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.
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.
10 20 30 40
Header L
position Newnode;
Newnode = malloc (size of (Struct Node));If
(Newnode! = NULL)
{
Newnode Element = X;
HEADER L
Empty List
position P;
P = L Next;
P = P Next;
return P;
L 10 20 25
P X
}
Find Previous Routine
position P;
P = L;
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
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
L P
10 20 Temp = P Next
P Temp
20 free (P)
P P = Temp
Temp, P = NULL
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
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
DATA
BLINK ELEMENT FLINK
Header
10 20 30 40
Structure Declaration : -
Struct Node
int Element;
};
Routine to insert an element in a doubly linked list
void Insert (int X, list L, position P)
If (Newnode ! = NULL)
Newnode Element = X;
P Flink = Newnode ;
Newnode Blink = P;
}
1.10 Data Structures
Header
10 20 30
15
Header
10 20 30
Advantage
Disadvantage
{
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;
}
}
}
Polynomial Differentiation
void diff ( )
{
poly *ptr1, *newnode;
ptr1 = list 1;
while (ptr1 ! = NULL)
{
Linear Data Structures - Lists 1.15
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
80
25
15
10 187 256
8 175
0 1 2 3 4 5 6 7 8 9
E1 E2 E3
P1
P2
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
if (start = = current)
start temp;
else
head 0 1 2 3 NULL
Linear Data Structures - Lists 1.19
polypointer temp;
while (*ptr)
temp = *ptr;
free (temp);
When a polynomial objects / ptr goes out of scope, The system first checks to see if a
destructor exists for that class.