-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap.cpp
60 lines (58 loc) · 1.01 KB
/
swap.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
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct node
{
int data;
struct node *next;
}node;
void create(node ** head,int n)
{
node *temp;
if(*head == NULL) {
temp = (node *)malloc(sizeof(node));
temp->data = n;
*head = temp;
(*head)->next = NULL;
}else {
create((&(*head)->next),n);
}
}
void print(node *head)
{
while(head != NULL) {
cout << head->data << "-->";
head = head->next;
}
}
void swap(node **head)
{
node *temp,*current,*temp1;
current = *head;
while(current != NULL && current->next != NULL) {
temp->data = current->next->data;
//cout << "next is " << temp->data << endl;
current->next->data = current->data;
current->data = temp->data;
//cout << "new values are " << current->data << " and " << temp->data << endl;
current = current->next->next;
}
}
int main()
{
node *head = NULL;
while(1) {
//create(&head,n);
int n;
cin >> n;
if(n == 999) {
break;
}else {
create(&head,n);
}
}
print(head);
swap(&head);
print(head);
return 0;
}