forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Length_of_Linked_List.c
71 lines (58 loc) · 1.64 KB
/
Length_of_Linked_List.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Length of Linked List
#include <stdio.h>
#include <stdlib.h>
/*
This code is written for Char to store in LinkedList, if you want to store numericals or any other datatype,
just replace relevent data type in place of char in class Node.
*/
struct Node
{
char data;
struct Node* next;
};
void pushing_data_to_LL(struct Node** headPointer, char newData)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode -> data = newData;
newNode -> next = (*headPointer);
(*headPointer) = newNode;
}
int gettingLength(struct Node* head)
{
int count = 0;
struct Node* current = head;
while(current != NULL)
{
count++;
current = current -> next;
}
return count;
}
int main()
{
struct Node* head = NULL;
printf("Currently Length of Linkedlist is: %d", gettingLength(head));
pushing_data_to_LL(&head, 'G');
pushing_data_to_LL(&head, 'S');
pushing_data_to_LL(&head, 'S');
pushing_data_to_LL(&head, 'O');
pushing_data_to_LL(&head, 'C');
//Intermediate Checking
printf("\n Currently Length of Linkedlist is: %d", gettingLength(head));
pushing_data_to_LL(&head, '-');
printf("\n Currently Length of Linkedlist is: %d", gettingLength(head));
pushing_data_to_LL(&head, '2');
pushing_data_to_LL(&head, '0');
pushing_data_to_LL(&head, '2');
pushing_data_to_LL(&head, '0');
printf("\n Currently Length of Linkedlist is: %d", gettingLength(head));
return 0;
}
/*
TestCase-1:
Sample Input: G S S O C 2 0 2 0
Sample Output: 9 //9 letters
TestCase-2:
Sample Input: O p e n - S o u r c e
Sample Output: 11 //11 letters
*/