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?

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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<<node->alphabet<<endl;

	if(node->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.