-
Notifications
You must be signed in to change notification settings - Fork 0
/
double_link.cpp
93 lines (90 loc) · 1.53 KB
/
double_link.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
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct node
{
int data;
struct node *next;
struct node *prev;
}node;
node *create(node ** head,int n)
{
node *temp,*temp1,*temp2,*tail;
if(*head == NULL) {
temp = (node *)malloc(sizeof(node));
temp->data = n;
*head = temp;
(*head)->next = NULL;
(*head)->prev = NULL;
}else {
temp1 = (node *) malloc(sizeof(node));
temp1->data = n;
temp2 = *head;
while(temp2->next != NULL) {
temp2 = temp2->next;
}
temp2->next = temp1;
temp1->prev = temp2;
temp1->next = NULL;
tail = temp1;
}
return(tail);
}
void print(node *head)
{
while(head != NULL) {
cout <<(head)->data << "-->";
(head) = (head)->next;
}
cout << endl;
}
void print_reverse(node *tail)
{
while(tail != NULL) {
cout << tail->data << "-->";
tail = tail->prev;
}
cout << endl;
}
void delete_node(node **head,int k)
{
int count = 1;
node *temp;
node *current = *head;
while(count < k) {
//cout << "current data is " << current->data << endl;
current = current->next;
count++;
}
current->prev->next = current->next;
current->next->prev = current->prev;
free(current);
}
int main()
{
node *head = NULL;
node *tail = NULL;
while(1) {
//create(&head,n);
int n,count;
count = 0;
cin >> n;
if(n == 999) {
break;
}else {
count++;
tail = create(&head,n);
//if(count == 1) {
// init = &head;
//}
}
}
print(head);
print_reverse(tail);
cout << "enter the node to be deleted\n";
int k;
cin >> k;
delete_node(&head,k);
print(head);
return 0;
}