Data_Structure_Linked_List
Data_Structure_Linked_List
1 s t r u c t Node {
2 i n t data ;
3 s t r u c t Node∗ n e x t ;
4 };
Listing 1: Node Structure in C
Function to Create a New Node
Discussion Prompt:
▶ Discuss how each of these operations can be implemented in a
singly linked list.
▶ Consider the complexity and edge cases for each operation.
Insertion at the Beginning
1 v o i d i n s e r t A t B e g i n n i n g ( s t r u c t Node ∗∗ head , i n t d a t a ) {
2 s t r u c t Node∗ newNode = c r e a t e N o d e ( d a t a ) ;
3 newNode−>n e x t = ∗ head ;
4 ∗ head = newNode ;
5 }
Listing 3: Inserting at the Beginning
Insertion at the End
1 v o i d i n s e r t A t E n d ( s t r u c t Node ∗∗ head , i n t d a t a ) {
2 s t r u c t Node∗ newNode = c r e a t e N o d e ( d a t a ) ;
3 i f ( ∗ head == NULL) {
4 ∗ head = newNode ;
5 return ;
6 }
7 s t r u c t Node∗ temp = ∗ head ;
8 w h i l e ( temp−>n e x t != NULL) {
9 temp = temp−>n e x t ;
10 }
11 temp−>n e x t = newNode ;
12 }
Listing 4: Inserting at the End
Insertion After a Specific Node
1 v o i d i n s e r t A f t e r N o d e ( s t r u c t Node∗ prevNode , i n t d a t a ) {
2 i f ( prev Node == NULL) {
3 p r i n t f ( ” P r e v i o u s node c a n n o t be NULL . \ n” ) ;
4 return ;
5 }
6 s t r u c t Node∗ newNode = c r e a t e N o d e ( d a t a ) ;
7 newNode−>n e x t = prevNode−>n e x t ;
8 prevNode−>n e x t = newNode ;
9 }
Listing 5: Inserting After a Specific Node
Deleting a Node by Value
1 v o i d d e l e t e F r o m B e g i n n i n g ( s t r u c t Node ∗∗ head ) {
2 i f ( ∗ head == NULL) {
3 p r i n t f ( ”The l i s t i s empty . \ n” ) ;
4 return ;
5 }
6 s t r u c t Node∗ temp = ∗ head ;
7 ∗ head = ( ∗ head )−>n e x t ;
8 f r e e ( temp ) ;
9 }
Listing 7: Deleting the First Node
Deleting the Last Node
▶ The ‘printList‘ function traverses the linked list and prints the
data in each node.
▶ It starts from the head and follows the ‘next‘ pointers until it
reaches a node with ‘next‘ pointing to NULL.
▶ The list is displayed in the format: ‘data1 -¿ data2 -¿ ... -¿
NULL‘.
Printing the Linked List (contd.)
1 v o i d p r i n t L i s t ( s t r u c t Node∗ head ) {
2 s t r u c t Node∗ temp = head ;
3 w h i l e ( temp != NULL) {
4 p r i n t f ( ”%d −> ” , temp−>d a t a ) ;
5 temp = temp−>n e x t ;
6 }
7 p r i n t f ( ”NULL\n” ) ;
8 }
Listing 9: Printing the List
Searching in the Linked List
1 v o i d r e v e r s e L i s t ( s t r u c t Node ∗∗ head ) {
2 s t r u c t Node∗ p r e v = NULL ;
3 s t r u c t Node∗ c u r r e n t = ∗ head ;
4 s t r u c t Node∗ n e x t = NULL ;
5 w h i l e ( c u r r e n t != NULL) {
6 n e x t = c u r r e n t −>n e x t ;
7 c u r r e n t −>n e x t = p r e v ;
8 prev = current ;
9 current = next ;
10 }
11 ∗ head = p r e v ;
12 }
Listing 12: Reversing the List
Finding the Length of the List
1 i n t g e t L e n g t h ( s t r u c t Node∗ head ) {
2 int length = 0;
3 s t r u c t Node∗ temp = head ;
4 w h i l e ( temp != NULL) {
5 l e n g t h ++;
6 temp = temp−>n e x t ;
7 }
8 return length ;
9 }
Listing 13: Finding the Length of the List