Linked List: by Mrs. Preeti S. Patil
Linked List: by Mrs. Preeti S. Patil
Linked List: by Mrs. Preeti S. Patil
Preeti S. Patil
Linked List
Non-sequential collection of data items For every data item, there is an associated pointer that would give memory location of the next data item in the linked list. Data items are not in a consecutive memory locations.
Start 10
Preeti S. Patil
20
30
40 Null
Preeti S. Patil
Data
Link
3000
Data
Link
4000
Data
Link
0
10
20
30
40
1000
Preeti S. Patil
2000
3000
4000
Null Pointer: Link field of last node contains zero rather than valid address.it is a null pointer and indicates the end of the list. External pointer(start): it is a pointer to the very first node in the linked list. Enables to access the entire list. Empty List/Null list: if the nodes are not present in the linked list. Start=Null;
Preeti S. Patil Linked List MPSTME, SHIRPUR
Every node in the linked list is a structure containing two fields such as data and link field. Physical view:
Data Link
Node
First is an integer data item & second is a link (pointer) to the next node of the same type.
Preeti S. Patil Linked List MPSTME, SHIRPUR
member1
Preeti S. Patil
Example
Node with the four fields:
Roll No Stud _Name Percentage link
Preeti S. Patil
Snode1.percentage
Snode1.link
Snode2.percentage
Snode2.link
Establishing link between two node is possible with following stn. Snode1.link=&snode2; // link field of snode1 holds the address of snode2
Rollno stud_name percentage 2000 Rollno stud_name percentage
Snode1 information
Snode1.Roll_no=123; Snode1.stud_name=kumar; Snode1.percentage=85.00;
Snode2 information
Snode2.Roll_no=147; Snode2.stud_name=saraswati; Snode2.percentage=89.00;
Note: link field of snode2 will be set to zero to signal end of the list. Snode2.link=0;
Preeti S. Patil Linked List MPSTME, SHIRPUR
Preeti S. Patil
struct node { int num; struct node *ptr; //Pointer to node }; typedef struct node NODE; //type definition making it abstract
data type
NODE * head; //pointer to the node of linked list head=(NODE *) malloc(sizeof(NODE)); //Dynamic
memory allocation
Preeti S. Patil
Inserting nodes
Three things to be done
Allocating a node Assigning a data. Adjusting the pointers.
Preeti S. Patil
Preeti S. Patil
20
Node 2
30
Node 3
40
New node
Preeti S. Patil
Preeti S. Patil
head 10
Node 1
20
Node 2
30
Node 3
40
New node
Preeti S. Patil
Preeti S. Patil
Preeti S. Patil
head 10 20
Node A
30
Node B
40
New node
Preeti S. Patil
Deleting Nodes
Three instances for deleting node
1. Deleting the first node of the linked list. 2. Deleting the last node of the linked list. 3. Deleting the specified node within the linked list.
Preeti S. Patil
Preeti S. Patil
head
temp
10
20
30
Preeti S. Patil
Preeti S. Patil
Preeti S. Patil
Preeti S. Patil
Preeti S. Patil
temp2=temp1; while(temp1 != NULL) { if(temp1->num ==item) { temp2->ptr = temp1->ptr; free(temp1); return; } temp2=temp1; temp1=temp1->ptr; } } }
Preeti S. Patil
Advantages
Accessibility of a node in the forward direction is easier. Insertion and deletion of nodes is easier.
Preeti S. Patil
Disadvantages
Accessing the preceding node of a current node is not possible as there is no backward traversal. Accessing a node is time consuming.
Preeti S. Patil
Header node
Exists at the beginning of the linked list. Only node present in an empty list. Data field of header node hold the global info (like number of items in the linked list) about the entire linked list. Traversing the entire linked list to know the number of nodes in linked list is not required.
Preeti S. Patil Linked List MPSTME, SHIRPUR
Preeti S. Patil
Preeti S. Patil
Linked Stacks
A linked Stack is a linear linked list in which all the insertion and deletions are always performed at the front of a list.
Preeti S. Patil
Horizontal Representation
Top is an external pointer to the very first node of a linked list.
Top 40 30 20 10
Null
Preeti S. Patil
Vertical representation
Top
Data Link
40 30
20
10 Null
Preeti S. Patil
No wastage of Memory as in sequential representation. Multiple stacks can be represented efficiently. PUSH & POP are easier and efficient.
DisAdvantages
1. A node in a linked requires more memory than a corresponding element in an array representation
Preeti S. Patil
Linked Queues
Linked queue does not suffer the limitation of size. Insertion of a new node is done at the end of the linked list Deletion is done from the beginning of the linked list.
Preeti S. Patil
Front is the external pointer pointing to the very first node in the linked queue. Rear is the pointer pointing to the last node in the linked queue
Preeti S. Patil
Preeti S. Patil