-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquestion2.c
61 lines (47 loc) · 861 Bytes
/
question2.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
/*
Move the last node to the begining of a linked list
*/
#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(){
struct node *head = (struct node *)malloc(sizeof(struct node));
struct node *t;
t = head;
int counter = 1; //creating a list dynamically
while(counter <=5){
t->data = counter*10;
if(counter == 5){
t->link = NULL;
}else{
t->link = (struct node *)malloc(sizeof(struct node));
}
t = t->link;
counter++;
}
t = head;
printList(t,head);
struct node *p;
while(t->link){
p = t;
t=t->link;
}
free(t);
t->link = head;
p->link = NULL;
head = t;
printList(t,head);
}