Skip to content

Commit b1f0b0f

Browse files
creation of doubly linked list in c++
1 parent b177018 commit b1f0b0f

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

doubly linkedList.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
// creating struct Node
5+
6+
struct Node {
7+
int data;
8+
struct Node *prev;
9+
struct Node *next;
10+
};
11+
struct Node* head = NULL;
12+
13+
//Inserting new node
14+
15+
void insert(int newdata) {
16+
struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
17+
newnode->data = newdata;
18+
newnode->prev = NULL;
19+
newnode->next = head;
20+
if(head != NULL)
21+
head->prev = newnode ;
22+
head = newnode;
23+
}
24+
//display linked list
25+
26+
void display() {
27+
struct Node* ptr;
28+
ptr = head;
29+
while(ptr != NULL) {
30+
cout<< ptr->data <<" ";
31+
ptr = ptr->next;
32+
}
33+
}
34+
int main() {
35+
insert(3);
36+
insert(1);
37+
insert(7);
38+
insert(2);
39+
insert(9);
40+
cout<<"The doubly linked list is: ";
41+
display();
42+
return 0;
43+
}

0 commit comments

Comments
 (0)