-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSAssignmentQ6.cpp
87 lines (61 loc) · 955 Bytes
/
DSAssignmentQ6.cpp
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
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
struct node
{
struct node *next;
int index;
};
typedef struct node node;
node *start=NULL;
void display();
int create()
{
node *temp,*ptr;
int n,i;
printf("Enter the number 'n' such that the linked list would contain (n+1) nodes\n");
scanf("%d",&n);
ptr=start;
for(i=0;i<=n;i++)
{
temp=(node *)malloc(sizeof(node));
temp->index=i;
temp->next=NULL;
if(i==0)
ptr=start=temp;
else
{
ptr->next=temp;
ptr=temp;
}
}
return n;
}
int main()
{
int n=create();
int i;
node *ptr,*temp;
ptr=start;
for(i=1;i<=(n/2);i++)
{
temp=ptr;
while(temp->next->next!=NULL)
temp=temp->next;
temp->next->next=ptr->next;
ptr->next=temp->next;
temp->next=NULL;
ptr=ptr->next->next;
}
display();
}
void display()
{
node *ptr=start;
while(ptr->next!=NULL)
{
printf("%d -> ",ptr->index);
ptr=ptr->next;
}
printf("%d \n",ptr->index);
}