-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSWAP1.CPP
141 lines (116 loc) · 2.17 KB
/
SWAP1.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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include<iostream.h>
#include<conio.h>
#include<malloc.h>
struct node //defination of struture
{
int data;
node *prev;
node *next;
} ;
node *head=NULL;
node *start=NULL;
node *end=NULL;
node *create() //node creation
{ node * newn ;
newn = (node *) malloc(sizeof(node)) ;
newn->next=NULL;
newn->prev=NULL;
return(newn);
}
/* ****** funtion declartions ********* */
void input();
void display();
void revdisplay();
void insert();
void swap();
void swap()
{ node *ptr,*next,*end,*pre;
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
end=ptr;
next=ptr->prev;
ptr=ptr->prev;
ptr=ptr->prev;
pre=ptr;
pre->next=end;
end->prev=pre;
end->next=next;
next->prev=end;
next->next=NULL;
int temp;
temp=end->data;
end->data=next->data;
next->data=temp;
}
void input()
{
node *in,*trav,*t;
in=create();
cout<<"enter the data ";
cin>>in->data ;
if(head==NULL)
{
head=in;
start=in;
}
else
in->prev=head;
head->next=in;
head=in;
end=head;
}
void display()
{ int a=0;
node * prnt;
prnt=start;
while(prnt->next!=NULL)
{
cout<<prnt->data<<endl;
prnt=prnt->next;
if(a>10){break;}a++;
}
cout<<prnt->data;
}
void revdisplay()
{ int a=0;
node * prnt;
prnt=end;
while(prnt->prev!=NULL)
{
cout<<prnt->data<<endl;
prnt=prnt->prev;
if(a>10){break;}a++;
}
cout<<prnt->data;
}
void insert()
{
node *first;
first=create();
cout<<"\nenter the data ";
cin>>first->data;
start->prev=first;
first->next=start;
first->prev=NULL;
start=first;
}
void main()
{
int ele,i;
clrscr();
cout<<"enter the no of nodes you want ";
cin>>ele;
for(i=0;i<ele;i++)
{
input();
}
display();
//insert();
swap();
cout<<endl;
display();
getch();
}