-
Notifications
You must be signed in to change notification settings - Fork 54
/
reverselinklist.c
96 lines (81 loc) · 1.74 KB
/
reverselinklist.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
#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
};
void makeLinkList(struct Node *head,int data){
struct Node *a=NULL;
a=(struct Node*)malloc(sizeof(struct Node));
a->data=data;
a->next=NULL;
struct Node *move=head;
while(move->next!=NULL){
move=move->next;
}
move->next=a;
}
void reverseLinkList(struct Node **head){
struct Node *tempNode=(struct Node*)malloc(sizeof(struct Node));
struct Node *nextNode=NULL;
tempNode->next=NULL;
while((*head)->next!=NULL){
nextNode=(*head)->next;
tempNode->data=(*head)->data;
(*head)->next=tempNode;
tempNode=(*head);
(*head)=nextNode;
}
tempNode->data=(*head)->data;
(*head)=tempNode;
}
void printLinkList(struct Node *head){
while(head->next!=NULL){
printf("%d\n",head->data);
head=head->next;
}
printf("%d\n",head->data);
}
void rotateByUnit(struct Node *head,int k){
struct Node *kth=NULL;
struct Node *kthone=NULL;
struct Node *move=head;
int length=0;
while(move->next!=NULL){
length+=1;
if(length==k){
kth=move;
kthone=move->next;
}
move=move->next;
}
length+=1;
move->next=head;
kth->next=NULL;
head=kthone;
printLinkList(head);
}
void reverseInGroup(struct Node* head,int k){
//use stack method put first k
//elements in stack and then pop to make reverse k elements.
}
int main(int argc, char const *argv[])
{
struct Node *head=NULL;
head=(struct Node*)malloc(sizeof(struct Node));
head->data=1;
head->next=NULL;
makeLinkList(head,2);
makeLinkList(head,3);
makeLinkList(head,4);
makeLinkList(head,5);
makeLinkList(head,6);
makeLinkList(head,7);
makeLinkList(head,8);
// printLinkList(head);
// rotateByUnit(head,3);
// reverseInGroup(head,3);
// reverseLinkList(&head);
// printLinkList(head);
return 0;
}