| /* Doubly Linked List implementation */ | |
| #include<stdio.h> | |
| #include<stdlib.h> | |
| struct Node { | |
| int data; | |
| struct Node* next; | |
| struct Node* prev; | |
| }; | |
| struct Node* head; // global variable - pointer to head node. | |
| //Creates a new Node and returns pointer to it. | |
| struct Node* GetNewNode(int x) { | |
| struct Node* newNode | |
| = (struct Node*)malloc(sizeof(struct Node)); | |
| newNode->data = x; | |
| newNode->prev = NULL; | |
| newNode->next = NULL; | |
| return newNode; | |
| } | |
| //Inserts a Node at head of doubly linked list | |
| void InsertAtHead(int x) { | |
| struct Node* newNode = GetNewNode(x); | |
| if(head == NULL) { | |
| head = newNode; | |
| return; | |
| } | |
| head->prev = newNode; | |
| newNode->next = head; | |
| head = newNode; | |
| } | |
| //Inserts a Node at tail of Doubly linked list | |
| void InsertAtTail(int x) { | |
| struct Node* temp = head; | |
| struct Node* newNode = GetNewNode(x); | |
| if(head == NULL) { | |
| head = newNode; | |
| return; | |
| } | |
| while(temp->next != NULL) temp = temp->next; // Go To last Node | |
| temp->next = newNode; | |
| newNode->prev = temp; | |
| } | |
| //Prints all the elements in linked list in forward traversal order | |
| void Print() { | |
| struct Node* temp = head; | |
| printf("Forward: "); | |
| while(temp != NULL) { | |
| printf("%d ",temp->data); | |
| temp = temp->next; | |
| } | |
| printf("\n"); | |
| } | |
| //Prints all elements in linked list in reverse traversal order. | |
| void ReversePrint() { | |
| struct Node* temp = head; | |
| if(temp == NULL) return; // empty list, exit | |
| // Going to last Node | |
| while(temp->next != NULL) { | |
| temp = temp->next; | |
| } | |
| // Traversing backward using prev pointer | |
| printf("Reverse: "); | |
| while(temp != NULL) { | |
| printf("%d ",temp->data); | |
| temp = temp->prev; | |
| } | |
| printf("\n"); | |
| } | |
| int main() { | |
| /*Driver code to test the implementation*/ | |
| head = NULL; // empty list. set head as NULL. | |
| // Calling an Insert and printing list both in forward as well as reverse direction. | |
| InsertAtTail(2); Print(); ReversePrint(); | |
| InsertAtTail(4); Print(); ReversePrint(); | |
| InsertAtHead(6); Print(); ReversePrint(); | |
| InsertAtTail(8); Print(); ReversePrint(); | |
| } |
This comment has been minimized.
This comment has been minimized.
sayem612
commented
Oct 11, 2015
|
This is a clean code. Thanks for sharing. |
This comment has been minimized.
This comment has been minimized.
JuanbiB
commented
Nov 16, 2015
|
Nicely written! |
This comment has been minimized.
This comment has been minimized.
rickyefarr
commented
Feb 14, 2016
|
I just happened upon you're code after googling "doubly linked list". I'm implementing my own doubly linked list for an open source project and I'm too lazy to pull out a piece of paper to draw a picture, thus seeing someone else's implementation is quite helpful. I hope you don't mind me pointing out a small (could be huge) problem, that most casual programmers can usually ignore. Your code causes a memory leak. This is due to using malloc in the function GetNewNode, but the allocated memory isn't freed in the end. At program termination, the memory is freed automatically...so it really doesn't matter with the small example you've given. On the other hand, if I edited the main function to insert an integer in a for loop with lots of iterations, this could starve the computer of memory resources until the program terminates. This is evident by the output of program, valgrind, below: $ valgrind --leak-check=full --show-leak-kinds=all ./list To fix this, a minor edit to the ReversePrint function to free each node can fix this. Again, hope you don't mind me pointing this out. I only wrote this for some poor undergrad or amateur programmer who happened upon this source code. |
This comment has been minimized.
This comment has been minimized.
rev118588
commented
May 14, 2016
|
only signed up to say this: God bless you |
This comment has been minimized.
This comment has been minimized.
learning-dev
commented
Jul 3, 2016
•
|
kudos to the "King of the Coding!" |
This comment has been minimized.
This comment has been minimized.
chinmayyande
commented
Aug 10, 2016
|
what a great help! God Bless you |
This comment has been minimized.
This comment has been minimized.
teja1600
commented
Aug 17, 2016
|
i want deleting operation at front, at last, at a position |
This comment has been minimized.
This comment has been minimized.
karbfg10k
commented
Aug 24, 2016
|
Quick question, when inserting at the tail why not use head->prev instead of iteration. Doubly linked makes tail insertion much faster this way right? Or have I made a wrong assumption? |
This comment has been minimized.
This comment has been minimized.
Ganeshbg
commented
Nov 26, 2016
|
Excellent :) |
This comment has been minimized.
This comment has been minimized.
SalahHamza
commented
Dec 22, 2016
•
|
Hi
but when i used this it works just fine
Thanks in advance; |
This comment has been minimized.
This comment has been minimized.
coredamnwork
commented
Feb 26, 2017
|
Thank you very much, this helped me a lot with one of my assignments1 |
This comment has been minimized.
This comment has been minimized.
bhavikbhansali
commented
Mar 3, 2017
|
Thank for such a nice code. It is good to understand doubly link list with this code. |
This comment has been minimized.
This comment has been minimized.
bitnahian
commented
Apr 3, 2017
|
Thank you for this awesome piece of code! Makes understanding very easy. |
This comment has been minimized.
This comment has been minimized.
Praveer1981
commented
Apr 25, 2017
|
Neat and clean code. Excellent !!! |
This comment has been minimized.
This comment has been minimized.
deep5050
commented
Apr 26, 2017
|
This comment has been minimized.
This comment has been minimized.
deep5050
commented
Apr 26, 2017
|
This comment has been minimized.
This comment has been minimized.
madogan
commented
May 11, 2017
|
Thank you |
This comment has been minimized.
This comment has been minimized.
LefterisIkaria
commented
Dec 1, 2017
|
The head pointer change by ref or by value in the InsertAtHead ? |
This comment has been minimized.
This comment has been minimized.
IT-17005
commented
Feb 17, 2018
|
nice |
This comment has been minimized.
This comment has been minimized.
krizedward
commented
Apr 5, 2018
|
nice |
This comment has been minimized.
This comment has been minimized.
AnjanaSamindraPerera
commented
May 8, 2018
|
gd work |
This comment has been minimized.
This comment has been minimized.
Djihanegh
commented
Sep 21, 2018
|
thank you good work ! |
This comment has been minimized.
This comment has been minimized.
SanjayBoricha
commented
Nov 26, 2018
|
best doubly linked list program |
This comment has been minimized.
This comment has been minimized.
oguzsk
commented
Nov 27, 2018
|
where is deletion function? |
This comment has been minimized.
This comment has been minimized.
ermatesg
commented
Jan 5, 2019
|
Thank you! |
This comment has been minimized.
This comment has been minimized.
bashish2711
commented
Jul 29, 2019
|
Good work, easy to understand basic concept |
This comment has been minimized.
This comment has been minimized.
devparkk
commented
Oct 6, 2019
|
I faced a lot of problems in implementation of linked list in c ... whatever resource I explore , everywhere the implementation is different and complex at the same time ... but this is quite unambiguous ... thanks a lot ... |
This comment has been minimized.
This comment has been minimized.
Aiman-Jannat
commented
Oct 30, 2019
|
Easily understandable.Thank You! |
This comment has been minimized.
This comment has been minimized.
TakbeerAli
commented
Dec 25, 2019
|
Thank you |
This comment has been minimized.
This comment has been minimized.
tridibsamanta
commented
Feb 18, 2020
•
|
Thanks a ton man ! |
This comment has been minimized.
cameronellis commentedSep 10, 2015
This is very clean and understandable. Thank you for posting it!