Skip to content

Commit bd98fff

Browse files
Working on Delete at Position in Doubly Linked List
1 parent 276912f commit bd98fff

3 files changed

+92
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
class Node
5+
{
6+
public:
7+
int val;
8+
Node *prev;
9+
Node *next;
10+
Node(int val)
11+
{
12+
this->val = val;
13+
this->prev = NULL;
14+
this->next = NULL;
15+
}
16+
};
17+
18+
void print_normal(Node *head)
19+
{
20+
Node *tmp = head;
21+
while (tmp != NULL)
22+
{
23+
cout << tmp->val << " ";
24+
tmp = tmp->next;
25+
}
26+
cout << endl;
27+
}
28+
29+
void print_reverse(Node *tail)
30+
{
31+
Node *tmp = tail;
32+
while (tmp != NULL)
33+
{
34+
cout << tmp->val << " ";
35+
tmp = tmp->prev;
36+
}
37+
cout << endl;
38+
}
39+
40+
void insert_tail(Node *&head, Node *&tail, int val)
41+
{
42+
Node *newNode = new Node(val);
43+
if (tail == NULL)
44+
{
45+
head = newNode;
46+
tail = newNode;
47+
return;
48+
}
49+
tail->next = newNode;
50+
newNode->prev = tail;
51+
tail = newNode;
52+
}
53+
54+
void delete_at_position(Node *head, int pos)
55+
{
56+
Node *tmp = head;
57+
for (int i = 1; i < pos; i++)
58+
{
59+
tmp = tmp->next;
60+
}
61+
Node *deleteNode = tmp->next;
62+
tmp->next = tmp->next->next;
63+
tmp->next->prev = tmp;
64+
delete deleteNode;
65+
}
66+
67+
void delete_head(Node *head, int pos)
68+
{
69+
// Node *tmp =
70+
}
71+
72+
int main()
73+
{
74+
Node *head = NULL;
75+
Node *tail = NULL;
76+
77+
while (true)
78+
{
79+
int val;
80+
cin >> val;
81+
if (val == -1)
82+
break;
83+
insert_tail(head, tail, val);
84+
}
85+
86+
print_normal(head);
87+
print_reverse(tail);
88+
89+
return 0;
90+
}
-164 Bytes
Binary file not shown.

output.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
10 20 30 40 50 60 80
2-
80 60 50 40 30 20 10
1+
10 20 30 40 50 60 70 80
2+
80 70 60 50 40 30 20 10

0 commit comments

Comments
 (0)