-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion1.c
116 lines (98 loc) · 1.91 KB
/
question1.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
Program to traverse a single linked list
Time complexity: O(n)
Space complexity: O(1)
*/
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node *link;
};
void printList(struct node *t, struct node *head){
//printing
while(t){
printf("%d --> ", t->data);
t=t->link;
}
printf("\n");
//head points to
printf("head is at: %d\n", head->data);
}
int main(){
//assigning the head
struct node *head = (struct node *)malloc(sizeof(struct node));
struct node *t;
t = head;
int counter = 1; //creating a list dynamically
while(counter <=10){
t->data = counter*10;
if(counter == 10){
t->link = NULL;
}else{
t->link = (struct node *)malloc(sizeof(struct node));
}
t = t->link;
counter++;
}
t = head;
printList(t,head);
//inserting one at the start
struct node *new = (struct node *)malloc(sizeof(struct node));
new->data = 0;
new->link = head;
head = new;
t= head;
printList(t,head);
t = head;
while(t->link){
t = t->link;
}
//inserting node at the end
//t is now at the end so..
t->link = (struct node *)malloc(sizeof(struct node));
t = t->link;
t->data = 200;
t->link = NULL;
printf("t link is %d\n", t->data);
t = head;
//printing the whole thing
printList(t,head);
t = head;
//inserting the new node after value is 30
while(t->data != 30){
t = t->link;
}
struct node *n2 = (struct node *)malloc(sizeof(struct node));
n2->data = 9999;
n2->link = t->link;
t->link = n2;
t = head;
printList(t,head);
t = head;
//deleting a node from beginning
head = t->link;
free(t);
t = head;
printList(t,head);
t = head;
//deleting node from the ending
while(t->link->link){
t = t->link;
}
free(t->link);
t->link = NULL;
t = head;
printList(t,head);
t = head;
//deleting a node after value is 30
while(t->link->data != 9999){
t=t->link;
}
struct node *t1 = t->link;
t->link = t1->link;
free(t1);
t = head;
printList(t,head);
t = head;
}