0% found this document useful (0 votes)
6 views5 pages

Singly Linked Lists

Uploaded by

NH Brothers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views5 pages

Singly Linked Lists

Uploaded by

NH Brothers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

struct node

{
int data;
struct node * link;
}

typedef struct node * NODE_PTR;

Algorithm Create_Node (int Item)


1. NODE_PTR Temp;
2. Temp = malloc (sizeof (struct node)
3. If (Temp = NULL)
4. Print (No Sufficient Memory)
5. Exit
6. Else
7. Temp->data = Item
8. Temp->link = NULL
9. Return Temp
10. Exit

Algorithm Traverse (NODE_PTR Start)


1. NODE_PTR Cur
2. Cur = Start
3. Repeat Step 4 while (Cur ≠ NULL)
4. Cur = Cur->link
5. Exit

Algorithm Display (NODE_PTR Start)


1. NODE_PTR Cur
2. Cur = Start
3. Repeat Steps 4 and 5 while (Cur ≠ NULL)
4. Print (Cur->data)
5. Cur = Cur->link
6. Exit
Algorithm Search (NODE_PTR Start, int Key)
1. NODE_PTR Cur;
2. Cur = Start
3. Repeat Steps 4 to 8 while (Cur ≠ NULL)
4. If (Cur->data = Key)
5. Print (Search Successful. Key Found)
6. Exit
7. Else
8. Cur = Cur->link
9. Print (Search Unsuccessful. Key Not Found)
10. Exit

Algorithm Insert_Front (NODE_PTR start, int Item)


1. NODE_PTR Cur
2. Cur = Create_Node (Item)
3. Cur->link = Start
4. Start = Cur
5. Return Start
6. Exit

Algorithm Insert_Rear (NODE_PTR start, int Item)


1. NODE_PTR Cur, Prev
2. Cur = Create_Node (Item)
3. If (Start = NULL)
4. Start = Cur
5. Exit
6. Prev = Start
7. Repeat Step 8 while (Prev->link ≠ NULL)
8. Prev = Prev->link
9. Prev->link = Cur
10. Exit
example null ,only 1 element, not fount.

using single varible.


Algorithm Insert_After_Node (NODE_PTR start, int Item, int Key)
1. NODE_PTR Cur, Prev, Next
2. Cur = Create_Node (Item)
3. Prev = Start
4. Next = Start->link
5. Repeat Steps 6 & 7 while (Prev ≠ NULL and Prev->data ≠ Key)
6. Prev = Next
7. Next = Next->link check if next is null or what if null then no need to start this line
8. If (Prev = NULL)
9. Print (Element not Found)
10. Exit
11. Else
12. Prev->link = Cur
13. Cur->link = Next
14. Exit

Algorithm Insert_Position (NODE_PTR Start, int Item, int Pos)


1. NODE_PTR Cur, Prev, Next
2. If (Pos = 1)
3. Insert_Front (Start, Item)
4. Cur = Create_Node (Item)
5. Loc = 2
6. Prev = Start
7. Next = Start->link
8. Repeat Steps 8, 9 & 10 while (Prev ≠ NULL and Loc ≠ Pos)
9. Prev = Next
10. Next = Next->link if next !=null;
11. Loc = Loc + 1
12. If (Prev = NULL)
13. Print (Invalid Position)
14. Exit
15. Else
16. Prev->link = Cur
17. Cur->link = Next
18. Exit
Algorithm Delete_Front (NODE_PTR start)
1. NODE_PTR Cur
2. Cur = Start
3. Start = Start->link
4. Free (Cur)
5. Return Start
6. Exit

Algorithm Delete_Rear (NODE_PTR start)


1. NODE_PTR Cur, Prev
2. Prev = Start
3. Cur = Start->link
4. If (Cur = NULL)
5. Start = NULL
6. Exit
7. Repeat Steps 8 & 9 while (Cur->link ≠ NULL)
8. Prev = Cur
9. Cur = Cur->link
10. Prev->link = NULL
11. Free (Cur)
12. Exit

Algorithm Delete_Node (NODE_PTR start, int Key)


1. NODE_PTR Cur, Prev
2. If (Start->data = Key)
3. Delete_Front ( )
4. Exit
5. Prev = Start
6. Cur = Start->link
7. Repeat Steps 8 & 9 while (Cur ≠ NULL and Cur->data ≠ Key)
8. Prev = Cur
9. Cur = Cur->link
10. If (Cur = NULL)
11. Print (Element not Found)
12. Exit
13. Prev->link = Cur->link
14. Free (Cur)
15. Exit

Algorithm Delete_Position (NODE_PTR Start, int Pos)


1. NODE_PTR Cur, Prev,
2. If (Pos = 1)
3. Delete_Front (Start)
4. Exit
5. Loc = 2
6. Prev = Start
7. Cur = Start->link
8. Repeat Steps 9, 10 & 11 while (Cur ≠ NULL and Loc ≠ Pos)
9. Prev = Cur
10. Cur = Cur->link
11. Loc = Loc + 1
12. If (Cur = NULL)
13. Print (Invalid Position)
14. Exit
15. Else
16. Prev->link = Cur->link
17. Free (Cur)
18. Exit

You might also like