-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublyLinkedlist.cpp
More file actions
43 lines (37 loc) · 753 Bytes
/
DoublyLinkedlist.cpp
File metadata and controls
43 lines (37 loc) · 753 Bytes
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
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *prev;
struct Node *next;
};
struct Node* head = NULL;
void addElement(int data) {
struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));
newnode->data = data;
newnode->prev = NULL;
newnode->next = head;
if(head != NULL)
head->prev = newnode ;
head = newnode;
}
void display() {
struct Node* ptr;
ptr = head;
while(ptr != NULL) {
cout<< ptr->data <<" ";
ptr = ptr->next;
}
}
int main() {
cout<<"Please enter the limit of input :" ;
int n,temp;
cin>>n;
for(int i=0;i<n;i++){
cin>>temp;
addElement(temp);
}
cout<<"Doubly linked list is: ";
display();
return 0;
}