C_Practice
Functions
linked_list.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include "linked_list.h"

Functions

void linked_list_dynamic_init (P_Node *p_node)
 
void printList (P_Node head)
 
void printList_middle (P_Node head_ref)
 
void linked_list_push_node (P_Node *head_ref, int data)
 
void linked_list_reverse (P_Node *head_ref)
 

Function Documentation

◆ linked_list_dynamic_init()

void linked_list_dynamic_init ( P_Node p_node)

This function is allocating the dynamic memory to the node...

Parameters
Passa pointer-to-lined-list. it can change the memory address stored at that memory location.
Returns
none
11 {
12  * p_node = (P_Node)malloc(sizeof(Node));
13 }
struct Node_T * P_Node
Definition: linked_list.h:6

Referenced by main().

◆ linked_list_push_node()

void linked_list_push_node ( P_Node head_ref,
int  data 
)

This function is pushing a new node at the front...

Parameters
Passa pointer-to-struct. it can change the memory address stored at that memory location.
Assigndata to a new node.
Returns
none
58 {
59  P_Node init_node = (P_Node)malloc(sizeof(Node));
60 
61  init_node->data = data;
62  init_node->next = (struct Node*) (* head_ref);
63  (* head_ref) = init_node;
64 }
struct Node_T * P_Node
struct Node * next
Definition: linked_list.h:8
int data
Definition: linked_list.h:7
Definition: linked_list.h:6

Referenced by main().

◆ linked_list_reverse()

void linked_list_reverse ( P_Node head_ref)

This function is reversing the linked list... Reverse function (Input: 1->2->3->4->NULL; Output: 4->3->2->1->NULL)

  1. Initialize three pointers: prev (NULL), curr (head), next (NULL).
  2. Iterate trough the linked list.
  • next = curr->next // store next node
  • curr->next = prev // actual reserving happens
  1. One step forward
  • prev = curr
  • curr = next
Parameters
Passa pointer-to-lined-list. it can change the memory address stored at that memory location.
Returns
none
82 {
83 
84  P_Node prev = NULL;
85  P_Node curr = * head_ref;
86  P_Node next = NULL;
87 
88  while (curr != NULL) {
89  // store next node
90  next = (P_Node) curr->next;
91 
92  // reverse
93  curr->next = (struct Node *) prev;
94 
95  // One step forward
96  prev = curr;
97  curr = next;
98 
99  }
100  *head_ref = prev;
101 }
struct Node_T * P_Node
struct Node * next
Definition: linked_list.h:8
Definition: linked_list.h:6

Referenced by main().

◆ printList()

void printList ( P_Node  head)

This function is printing contents of linked list starting from the given node...

Parameters
Startingnode.
Returns
none
20 {
21  P_Node temp = head;
22 
23  // Protect
24  while(temp != NULL)
25  {
26  printf("%d ", temp->data);
27  temp = (P_Node) temp->next;
28  }
29  printf("\n");
30 }
struct Node_T * P_Node
struct Node * next
Definition: linked_list.h:8
int data
Definition: linked_list.h:7
Definition: linked_list.h:6

Referenced by main().

◆ printList_middle()

void printList_middle ( P_Node  head_ref)

This function is printing middle of linked list...

Parameters
Startingnode.
Returns
none
37 {
38  P_Node p_fast_node = head_ref;
39  P_Node p_slow_node = head_ref;
40 
41  if(head_ref != NULL) {
42  while (p_fast_node != NULL && p_fast_node->next != NULL) {
43  p_fast_node = (P_Node) p_fast_node->next;
44  p_fast_node = (P_Node) p_fast_node->next;
45  p_slow_node = (P_Node) p_slow_node->next;
46  }
47  printf("The meddle elements is: %d \n", p_slow_node->data);
48  }
49 
50 }
struct Node_T * P_Node
struct Node * next
Definition: linked_list.h:8
int data
Definition: linked_list.h:7
Definition: linked_list.h:6

Referenced by main().