File tree Expand file tree Collapse file tree 1 file changed +67
-0
lines changed Expand file tree Collapse file tree 1 file changed +67
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include<stdio.h>
2
+ #include<stdlib.h>
3
+ struct Node {
4
+ int data;
5
+ struct Node* next;
6
+ struct Node* prev;
7
+ };
8
+ struct Node* head;
9
+ struct Node* GetNewNode(int x) {
10
+ struct Node* newNode
11
+ = (struct Node*)malloc(sizeof(struct Node));
12
+ newNode->data = x;
13
+ newNode->prev = NULL;
14
+ newNode->next = NULL;
15
+ return newNode;
16
+ }
17
+ void InsertAtHead(int x) {
18
+ struct Node* newNode = GetNewNode(x);
19
+ if(head == NULL) {
20
+ head = newNode;
21
+ return;
22
+ }
23
+ head->prev = newNode;
24
+ newNode->next = head;
25
+ head = newNode;
26
+ }
27
+ void InsertAtTail(int x) {
28
+ struct Node* temp = head;
29
+ struct Node* newNode = GetNewNode(x);
30
+ if(head == NULL) {
31
+ head = newNode;
32
+ return;
33
+ }
34
+ while(temp->next != NULL) temp = temp->next; // Go To last Node
35
+ temp->next = newNode;
36
+ newNode->prev = temp;
37
+ }
38
+ void Print() {
39
+ struct Node* temp = head;
40
+ printf("Forward: ");
41
+ while(temp != NULL) {
42
+ printf("%d ",temp->data);
43
+ temp = temp->next;
44
+ }
45
+ printf("\n");
46
+ }
47
+ void ReversePrint() {
48
+ struct Node* temp = head;
49
+ if(temp == NULL) return;
50
+ while(temp->next != NULL) {
51
+ temp = temp->next;
52
+ }
53
+ printf("Reverse: ");
54
+ while(temp != NULL) {
55
+ printf("%d ",temp->data);
56
+ temp = temp->prev;
57
+ }
58
+ printf("\n");
59
+ }
60
+
61
+ int main() {
62
+ head = NULL;
63
+ InsertAtTail(10); Print(); ReversePrint();
64
+ InsertAtTail(2); Print(); ReversePrint();
65
+ InsertAtHead(6); Print(); ReversePrint();
66
+ InsertAtTail(3); Print(); ReversePrint();
67
+ }
You can’t perform that action at this time.
0 commit comments