### Doubly Linked List Insert Function

I cant get the Doubly Link List to print the inserted symbol. starting from the list all over again. And i am wondering if i am doing it right?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112`` ``````class Node{ public: char alphabet; Node* prev; Node* next; void put_next(Node* node); Node* Begin(); Node* End(); Node* GetFromIndex(int index, Node* node); void DisplayAll(); void DisplayAllEnd(); void push_back(int value){ Node* tempNode = new Node(); tempNode->alphabet = value; tempNode->prev = this->End(); this->End()->next = tempNode; } void push_front(int value){ Node* tempNode = new Node(); tempNode->alphabet = value; tempNode->next = this->Begin(); this->Begin()->prev= tempNode; } void swap(int index1, int index2){ Node* tempNode = new Node(); tempNode = GetFromIndex(3,this); } void insert(int index, int value, Node* &node){ /* Node* tempNode = new Node(); tempNode->alphabet = value; this->Begin(); Node* currNode = this; for(int i = 0; i <= index; i++){ currNode = currNode->next; } tempNode->prev = currNode->prev; tempNode->next = currNode; */ } }; void insert(int index, int value, Node* &node){ Node* tempNode = new Node(); tempNode->alphabet = value; node->Begin(); for(int i = 0; i <= index; i++){ node = node->next; } tempNode->prev = node->prev; node->prev = tempNode; tempNode->next = node; tempNode->prev->next = tempNode; } Node* Node::Begin(){ if(this->prev != NULL){ this->prev->Begin(); } else{ return this; } } void DisplayAll(Node* node){ cout<alphabet<next != NULL){ DisplayAll(node->next); } } int _tmain(int argc, _TCHAR* argv[]) { char newChar; Node* alphabetLinkedList = NULL; alphabetLinkedList = new Node(); alphabetLinkedList->alphabet = 'a'; for(int i = (int)'a'; i <= (int)'z'; i++){ Node* tempNode = new Node(); tempNode->alphabet = (char)i; alphabetLinkedList->put_next(tempNode); alphabetLinkedList = tempNode; } insert(6,3, alphabetLinkedList); alphabetLinkedList->Begin(); DisplayAll(alphabetLinkedList); } ``````
Here is how I have implemented a doubly linked list in java. It might give you an idea:

http://ideone.com/Auhuxu

the 'nextlist' property is inherited from a linked list class
Last edited on
If i am not wrong, yours is insert a node at the back of the linked list right?

i want to insert a node in any parts of the linked list

Thanks anyways :D
on line 61: you call `node->Begin();`, but you don't use the returned value. I.e. you use the node originally passed to `insert()`

on line 70: if `tempNode->prev` is null (i.e. the very first node) your program will crash
oh i see... now i get it.. i got it to work now THANKS ALOT!
Topic archived. No new replies allowed.