-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinklist.cpp
More file actions
101 lines (94 loc) · 1.99 KB
/
linklist.cpp
File metadata and controls
101 lines (94 loc) · 1.99 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
#include <iostream>
using namespace std;
typedef struct st{
int data;
struct st* next;
}node;
node* newnode(int d){
node * tmp=new node;
tmp->data=d;
tmp->next=NULL;
return tmp;
}
void insert(node ** head,int x){
node* z=newnode(x);
node *y=*head;
*head=z;
z->next=y;
}
void print(node* head){
node* x=head;
while(x!=NULL) {cout<<x->data<<' ';x=x->next;}
cout<<endl;}
void reverse(node** head){
node *prev=NULL, *curr=*head , *next;
while(curr){
next=curr->next;
curr->next=prev;
*head=curr;
prev=curr;
curr=next;
}}
void mergealternate(node* head1,node* head2){
if(!head1||!head2) return ;
node *x=head1->next,*y=head2->next;
head1->next=head2;
head2->next=x;
mergealternate(x,y);
}
void removedup_sorted(node* head){
node *curr=head;
while(curr){
node* next=curr->next;
if(next && curr->data==next->data) {curr->next=next->next;delete next;}
else curr=curr->next;
}}
void remnode(node** head,int d){
if(!*head) return ;
node *h=*head,*pre;
if(d==h->data) {*head=h->next;return ;}
while(h&&h->data!=d) {pre=h;h=h->next;}
if(!h) return ;
pre->next=h->next;
delete(h);
}
void reverse_rec(node** head){
if(!*head) return ;
if((*head)->next==NULL) return ;
node * y=*head;
*head=(*head)->next;
reverse_rec(head);
y->next->next=y;
y->next=NULL;
}
void removedup(node** head)
{
node *curr=*head;
while(curr){
cout<<curr->data<<endl;
node *f=*head,*p;
while(f!=curr && f->data!=curr->data) {p=f;f=f->next;}
if(f==*head) {node *c=*head; *head=(*head)->next;delete(c);curr=curr->next;continue;}
if(f==curr) {curr=curr->next;}
else {p->next=f->next;delete(f);curr=curr->next;}
}
}
int main(){
node *head1=NULL,*head2=NULL;
insert(&head1,1);
insert(&head1,2);
insert(&head1,3);
insert(&head1,4);
insert(&head1,5);
insert(&head2,1);
insert(&head2,3);
insert(&head2,5);
insert(&head2,7);
insert(&head2,9);
reverse_rec(&head1);
reverse_rec(&head2);
mergealternate(head1,head2);
removedup(&head1);
//remnode(&head1,5);
print(head1);
return 0;}