UNIT – V
Linear Data Structures using Linked Organization
[9 Hrs]
Limitations of static memory allocation. Dynamic memory allocation
in C.
Concept of linked organization, Singly linked list, Doubly linked
list, Circular linked list. Operations like insertion, deletion,
traversal & other operations on these data structures.
Applications: Representation & manipulation of polynomials using circular linked lists, Application of doubly linked list in dynamic storage management, garbage collection and compaction. Representation of polynomial using generalized linked list (implementation not expected), Concept of skip list.
Analysis
of the algorithms used.
Memory :
Computer memory is just collection of number of registers, which is having capacity of storing data. When we write program we have to make use of more amount of data and to store data their must be memory. There are two ways to allocate memory in computer system.
We know that how program executes in C/C++ programming languages.
Static memory Allocation : Definition:
“When memory to data structures gets allocated during the time of program compilation, such type of memory allocation is called static memory allocation”.
Dynamic Memory Allocation: Definition:
“When memory to data structures gets allocated during the time of program execution, such type of memory allocation is called dynamic memory allocation”.
Limitations of static memory allocation. Dynamic memory allocation in C.
Introduction:
Important Points about Link list:
1. A linked list is a dynamic data structure that can grow and shrink based on need during the time of program execution.
2. The elements are not necessarily at a fixed distance apart.
3. In a linked list, the elements are placed in non-contiguous blocks of memory, and each block is linked to its previous block.
4. To link the next element to the previous element, the address of the next element is stored in the previous element itself.
8. A circular list is a list in which the link field of the last node is made to point to the start/first node of the list
9. A doubly linked list (DLL) is a linked list in which every node contains two links, called the left link and right link, respectively.
10. The left link of the node in a DLL is made to point to the previous node, whereas the right link is made to point to the next node.
11. A DLL can be traversed in both directions.
12. Having two pointers in a DLL provides safety, because even if one of the pointers get corrupted, the node still remains linked.
13. Deleting a particular node from a list, therefore, does not require keeping track of the previous node in a DLL.
Link List :
Definition:
“Link list is an ordered collection of nodes, each node is connected to
another node with the help of addresses.”
Link list is a dynamic data structure which is used to store similar type of data for temporary purpose. It is a collection of elements called node. It is a list of elements in which the elements of the list can be placed anywhere in memory, and these elements are linked with each other using an explicit link field, that is, by storing the address of the next element in the link field of the previous element.
Each node contains two basic parts:
a) Data field or information field
b) Link field i.e. Address of next coming node in the list.
Node
Data Field Link field
Here,
Data Field = information
Link Field = Address of next coming node
Why to use link list?What are advantages of link list?
1) Link list is a commonly used linear or non linear data structure for storing similar type of data in to computer memory. It may allocates contiguous memory or random memory locations for nodes in the link list.
2) It is dynamic data structure so it don’t have fixed limit on number of nodes in the list. Due to this it will allocate whatever memory required only, and
avoid wastage of memory.
3) To implement insert and delete operations on link List is easier.
4) Due to use of pointers memory retrieval becomes faster.
5) Flexible because it will allows us to allocate memory dynamically so that whatever memory required that many only gets reserved. Once memory block use is over memory gets deallocated by using free function.
What are the drawbacks of array data structure?
Solution:
Array is a data structure which is having following drawbacks.
1. Size of array is fixed because of that wastage of memory takes place.
e.g. int arr[50];
Here, we have declared integer array arr having 50 elements size. So compiler will allocate total of
2 * 50 =100bytes of memory for this array arr. If programmer is making use of this array for storing only 10 elements then it will take only
Sr Array Sr Link List
1. Array is a collection of elements of similar data types which allocates contiguous memory locations in the memory having fixed size.
1 Link list is a collection of nodes and each node gets connected with another node with the help of links. Each node contains data field and link field. Link field contain address of next coming node.
2 Array is having fixed size. 2. Link list has no fixed size. It is dynamic in nature. It grows and shrink during the time of program execution.
3. Insertion and deletion operations on array is tedious work because for implementing insertion and deletion operations on array, it needs to shift elements to the right or left side positions in the array.
3. To insert and delete any node in the link list is very simple task because here we have just need to manipulate the addresses of the nodes in the link lists.
4. Allocates static memory for elements.
4. Allocates dynamic memory for elements.
5. We can randomly access any element in the array.
5. We can not randomly access any element or node in the link list. To access any element we have to traverse link list from starting node to the required node.
6. Array elements gets stored in adjacent memory locations.
6. In link list nodes gets stored in memory randomly wherever free memory is available. That may adjacent or may not
7. Arrays are linear data structure. 7. Link list is linear as well non linear data structure. According to access point of view link list is a linear data structure & according to storage point of view link list is non linear data structure.
8. Arrays are RANDOM ACCESS structures, where you can access
In some cases array memory becomes insufficient say suppose programmer want to store more than 50 elements into array arr , it is not possible because we have already declared array arr for 50 elements only and we can not increase array size dynamically.Insert , delete operations on array become tedious job because for every time we need to shift elements either to left or right side in the array for deleting and inserting any element in the array.
6.2 Terminologies Node, Address, Pointer, Information,
Next, Null pointer, Empty list
Define the following terminologies related to linked list:
[a] Node [b] Pointer [c] Information [d] Next and Null
1) Node :
In case of singly link list,
Node is a element of link list which contains two basic parts i.e. data field or information field and link field or address of the next coming node.
Node
Data Field Link field i.e.
Address of next coming node
Fig.(a) General structure of node in singly link list
Example:- Node will contain actual data and addresses of next coming node in the link list.
Node
int roll;
char name[20];
float percentage;
Address of next coming node
i.e. 2000
In case of doubly link list,
Node contains three basic parts i.e. address of previous node, data field and address of next coming node.
Node
Address of previous
node in the list
Data Field Link field i.e.
Address of next coming node
Fig.(a) General structure of node in doubly link list
Example :- Node will contain address of previous node in the list , actual data and addresses of next coming node in the list.
Node
Address of previous node
i.e. 1000
int roll;
char name[20];
float percentage;
Address of next coming node
i.e. 2000
Fig.(a) Example of structure of node in doubly link list
2) Pointer :-
Pointer is a variable which holds address of the another variable. Another variable may be pointer variable or normal variable.
E.g. int x=20;
int *p;
p=&x;
x
2000
Address :-
Address tells that where values or data or node gets stored in computer memory. e.g. in above example 2000 and 4000 is the addresses of x and p variables.
Information or data :
Information is nothing but actual value or data stored in data field in node. Here information may be character, integer or float type.
It is declared as follows,
Int x,y;
Float per;
Char ch;
Empty list :
If link list don’t contain any node then it is called as empty link list. In such situation pointer pointing to start node is set to NULL.
e.g. say suppose start is a pointer pointing to starting node in the link list and say suppose link list is empty then,
start = NULL;
NULL :->
Here, Null is nothing but zero value. NULL is a value assigned to next field of last node in the singly or doubly link list. When node contains NULL value that means that node is the last node in the link list. So NULL value is used to identify last node in the link list.
7) Next field
Here, in case of link list, every node contains next filed to store the address of next coming node.
6.3. Operations on list Searching, Insertion and Deletion
We can perform following operations on link list:
a) Insertion
b) deletion
c) update
d) count
e) traverse or display list
f) search
i) sorting
j) Merge
k) Erase
l) append
6.4 Types of lists Linked list and Circular list
There are following types of link lists:
6.5 Array stacks, queues, implementation using list.
How to delete a node from link list.
Conditions to check to delete node from link list..
1) To delete start node
2) To delete node in between
3) To delete last node
//Delete node
temp=start;
t=NULL;
printf("\n\n\t Enter data to delete : ");
scanf("%d",&deleted);
temp=start;
while(temp->next!=NULL)
{
t=temp; //keep track of previous node to delete
temp=temp->next;
if(temp->data==deleted)//delete intermediate node
{
t->next=temp->next;
free(temp);
break;
}
}
if(temp->next==NULL) //delete last node
{
if(temp->data==deleted)
{
t->next=NULL;
free(temp);
}
}
}
Applications of Link Lists:
1. Sparse matrix representation
2. Polynomial manipulation
3. Dynamic memory storage
4. Symbol table
5. Implement various other data structure like Array, Stack, Queue, Graph,Treee link list is used.
keep track of the allocated and free portions of memory, the memory manager is required to maintain a linked list of allocated and free segments.
7. Compiler construction : To create parse tree , syntax tree, DAG (Directed Acyclic Graph)