Linked List
Linked List
Introduction
If more space required at the time of execution, then enhance is not possible.
Linked list
When new elements need to be added then nodes are created and appended to LL.
Each elements are referred as nodes. Each node as two parts Data field and link field. Called INFO and NEXT.
INFO NEXT
Node representation.
Advantages of LL
2. Accessing elements is more difficult.- traversing required because no index to linked list.
Linked List Implementation.
Two tasks
struct node
{
int INFO;
struct node *NEXT;
};
typedef struct node NODE;
1. Insert
2. Delete
3. Search
4. Print.
Insert Operation
Insert (value)
Step 1: start
Step 2: set PTR= addressof (newnode) /* Allocate a new node and assign its address to PTR*/
3 possible deletion
Step 1: start
Step
2: set LOC=search(value) /* Allocate a new node and assign its address to PTR*/
Step 3: If LOC=NULL goto step 4 else step 5;
Step 4: return and print “deletion unsuccessful” and stop.
Step 5: if LOC=FIRST then goto step 6 else step 10
Step 6: if FIRST=LAST then goto step 7 else step 8;
Step 7:set FIRST=LAST=NULL and goto step 9;
Step 8: FIRST=FIRST->NEXT;
Step 9: return “Deletion is successful” and stop.
Step 10: set TEMP=LOC-1;
Step 11: set TEMP->NEXT=LOC->NEXT;
Step 12: if LOC=LAST goto step 13 else step 14.
Step 13: set LAST=TEMP.
Step 14: return “deletion is successful”
Step 15: STOP
Search
3 steps
3. Return the search failure notification if entire list is traversed without any match.
search(value)
Step 1: start
Step 2: If FIRST=NULL goto step 3 else step 4.
Step 3: return “search unsuccessful, element not found” and stop;
Step 4: set PTR=FIRST;
Step 5: Repeat step 6-8 until PTR!=LAST;
Step 6: If PTR->INFO=value goto step 7 else goto step 8;
Step 7: Return “Search is Successful “, PTR and stop;
Step 8: set PTR=PTR->NEXT;
Step 9: If LAST->INFO=value goto step 10 else step 11.
Step 10: Return “Search is Successful “, LAST and stop.
Step 11: return “search unsuccessful, element not found” and stop.
Step 12: STOP;
Print
Display the list contents.
Print()
Step 1: start
Step 2: if FIRST=NULL goto step 3 else step 4
Step 3: Display “Empty list” and stop;
Step 4: if FIRST=LAST then goto step 5 else goto step 6
Step 5: Display (FIRST->INFO) and stop
Step 6: set PTR=FIRST.
Step 7: Repeat setp 8-9 UNTIL PTR!=LAST;
Step 8: Display (PTR->INFO);
Step 9: PTR=PTR->NEXT.
Step 10: Display (LAST->INFO).
Step 11: STOP.
Types of LL
1. Single LL.
2. Circular LL
3. Doubly LL.
Doubly Linked List
LAST
Operation on DLL
Insert
Delete
Search
Print
Linked list implementation of Stack
PUSH
POP
Display
Priority Queue