-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest1.cpp
129 lines (115 loc) · 2.42 KB
/
test1.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
//doubly linked list
include <iostream>
using namespace std;
class node{
public:
int data;
node* next;
node* pre;
node(int d){
this->data = d;
this->next = NULL;
this->pre = NULL;
}
};
void insertH(node**head,int data){
node* newnode = new node(data);
newnode->next = *head;
*head = newnode;
newnode->pre = NULL;
}
void insertE(node**head,int data){
node* newnode = new node(data);
node* temp = *head;
if(temp==NULL){
newnode->data = data;
newnode->next = NULL;
newnode->pre = NULL;
*head = newnode;
}
else{
while(temp->next!=NULL){
temp = temp->next;
}
temp->next = newnode;
newnode->pre = temp;
}
}
void insertSP(node**head,int data,int pos){
node* newnode = new node(data);
if(pos==0){
newnode->data = data;
newnode->next = NULL;
newnode->pre = NULL;
*head = newnode;
}
else{
node* temp = *head;
for(int i=0;i<pos-1;i++){
temp = temp->next;
if(temp==NULL){
cout<<"Glt position hai bhai"<<endl;
return;
}
}
newnode->next = temp->next;
temp->next = newnode;
newnode->pre = temp;
}
}
void deleteB(node**head){
if(*head==NULL){
cout<<"List is empty"<<endl;
return;
}
else{
node* temp = *head;
*head = (*head)->next;
(*head)->pre = NULL;
}
}
void deleteSP(node**head,int pos){
if(pos==0){
cout<<"Khali hai"<<endl;
}
if(pos==1){
deleteB(head);
}
else{
node* del = *head;
node* temp;
for(int i=0;i<pos-1;i++){
temp = del;
del = del->next;
if(del==NULL){
cout<<"GALAT PSOITION HAI"<<endl;
break;
}
}
temp->next = del->next;
del->next = NULL;
delete del;
}
}
void print(node*head){
node* temp = head;
while(temp!=NULL){
cout<<temp->data<<endl;
temp = temp->next;
}
cout<<endl;
}
int main() {
node* head = NULL;
insertH(&head,10);
insertH(&head,20);
insertE(&head,30);
insertE(&head,40);
insertSP(&head,25,3);
print(head);
deleteB(&head);
print(head);
deleteSP(&head,4);
print(head);
return 0;
}