-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab3(b)Q4.cpp
More file actions
122 lines (120 loc) · 2.51 KB
/
Lab3(b)Q4.cpp
File metadata and controls
122 lines (120 loc) · 2.51 KB
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
#include<iostream>
using namespace std;
//in question it wasnt mention either linklist is singly or doubly so i chose singly but same method can be used for doubly linklist just we have to update prev pointer
class Node{
public:
int data;
Node*next;
Node(int d){
data=d;
next=NULL;
}
};
void insertattail(Node*& head, Node*& tail,int val) {
Node* temp=new Node(val);
if (head==NULL) {
head=temp;
tail=temp;
} else{
tail->next=temp;
tail=temp;
}
}
void display(Node*head){
Node*curr=head;
while(curr!=NULL){
cout<<curr->data<<"->";
curr=curr->next;
}
cout<<endl;
}
Node*reverse(Node*head){
Node*curr=head;
Node*prev=NULL;
Node*forward=NULL;
while(curr!=NULL){
forward=curr->next;
curr->next=prev;
prev=curr;
curr=forward;
}
return prev;
}
Node*alternate2(Node*head,Node*tail){
Node*curr=head;
Node*prev=NULL;
Node*temp=NULL;
if(head==NULL||head->next==NULL){
return head;
}
Node*newhead=NULL;
while(curr!=NULL&&curr->next!=NULL){
temp=curr->next;
curr->next=temp->next;
temp->next=newhead; //basically similar tu insertathead function
newhead=temp;
prev=curr;
curr=curr->next;
}
if(prev!=NULL){
prev->next=newhead;
}else{
head=newhead;
}
return head;
}
Node*alternate(Node*head,Node*tail){//it uses extra space to reverse nodes
Node*curr=head;
Node*prev;
Node*newhead=NULL;
Node*newtail=NULL;
Node*newnode=NULL;
if(head==NULL||head->next==NULL){
return head;
}
while(curr!=NULL&&curr->next!=NULL){
newnode=curr->next;
curr->next=newnode->next;
newnode->next=NULL;
insertattail(newhead,newtail,newnode->data);
prev=curr;
curr=curr->next;
}
newhead=reverse(newhead);
if(prev!=NULL){
prev->next=newhead;
}else{
head=newhead;
}
return head;
}
int main(){
Node*head=NULL;
Node*tail=NULL;
Node*newhead=NULL;
Node*newtail=NULL;
insertattail(head,tail,10);
insertattail(head,tail,4);
insertattail(head,tail,9);
insertattail(head,tail,1);
insertattail(head,tail,3);
insertattail(head,tail,5);
insertattail(head,tail,9);
insertattail(head,tail,4);
cout<<"Original"<<endl;
display(head);
cout<<"By Method 1:"<<endl;
Node*al=alternate(head,tail);
display(al);
insertattail(newhead,newtail,10);
insertattail(newhead,newtail,4);
insertattail(newhead,newtail,9);
insertattail(newhead,newtail,1);
insertattail(newhead,newtail,3);
insertattail(newhead,newtail,5);
insertattail(newhead,newtail,9);
insertattail(newhead,newtail,4);
cout<<"Without Extra Space"<<endl;
Node*al2=alternate2(newhead,newtail);
display(al2);
}