Skip to content

Commit

Permalink
Create linked list and traversal of the linked list
Browse files Browse the repository at this point in the history
  • Loading branch information
ronitsrma16 authored Aug 20, 2024
1 parent 740b3cb commit 8cabe00
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions ronit/c/linked_list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct{
int data;
struct Node *next_address;
}Node;


void print_linkedList(Node *head_pointer);
int main()
{
int run_again = 0;


Node *Head = (Node*)malloc(sizeof(Node));
Node *first_node = (Node*)malloc(sizeof(Node));
Node *second_node = (Node*)malloc(sizeof(Node));

// Initialize head Node
Head->next_address = (struct Node*)first_node;
// Initialize first Node
first_node->data = 1;
first_node->next_address = (struct Node*)second_node;
// Initialize second Node
second_node->data = 2;
second_node->next_address = 0;

do{

print_linkedList(Head);

printf("\n");
printf("Do you run program again\r\n");
scanf("%d",&run_again);
}while(run_again);

return 0;
}

// Traversal Of the Nodes
void print_linkedList(Node *head_pointer)
{
int counter = 1;
// To avoid printing of the head Node data
if(head_pointer->next_address == 0)
{
return;
}
else
{
head_pointer = (Node*)head_pointer->next_address;
}
while(head_pointer != 0)
{
printf("Node %d value - %d\r\n", counter++, head_pointer->data);
head_pointer = (Node*)head_pointer->next_address;
}
}

0 comments on commit 8cabe00

Please sign in to comment.