-
Notifications
You must be signed in to change notification settings - Fork 0
/
week7-2.c
85 lines (77 loc) · 1.74 KB
/
week7-2.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
#include <stdio.h>
#include <stdlib.h>
struct node{
int data;
struct node * next;
}*head;
int n=0;
struct node * newnode(int value){
struct node * temp=(struct node*)malloc(sizeof(struct node));
temp->data=value;
temp->next=NULL;
return temp;
}
void InsertAtBeg(struct node ** head,int x){
struct node * temp=newnode(x);
temp->next=*head;
*head=temp;
n++;
}
void InsertAtEnd(struct node ** head,int x){
struct node *temp=newnode(x);
if(*head==NULL){
*head=temp;
}
else{
struct node * Lastnode=*head;
while(Lastnode->next!=NULL){
Lastnode=Lastnode->next;
}
Lastnode->next=temp;
}
n++;
}
void print(struct node * root){
while(root!=NULL){
printf("%d ",root->data);
root=root->next;
}
}
void reverse(struct node ** head,int m,int l){
struct node * prev1=NULL;
struct node * curr=*head;
int index=1,count=0;
while(index!=l){
prev1=curr;
curr=curr->next;
index++;
}
//printf("%d current\n%d prev",curr->data,prev1->data);
struct node * curr1=curr;
struct node * prev=NULL;
struct node * next;
while(curr!=NULL&&count!=m){
next=curr->next;
curr->next=prev;
prev=curr;
curr=next;
count++;
}
prev1->next=prev;
curr1->next=curr;
}
int main(){
int m=3,l=2;
head=NULL;
InsertAtBeg(&head,7);
InsertAtBeg(&head,6);
InsertAtBeg(&head,5);
InsertAtBeg(&head,4);
InsertAtBeg(&head,3);
InsertAtBeg(&head,2);
InsertAtBeg(&head,1);
printf("%d\n",n);
reverse(&head,3,2);
print(head);
return 0;
}