Linked List

Hi so this is my code,

1.After I input the elements then use 000 to exit, it also takes "000" as an element, which I dont need.

2.When it reach search, it crashes, and im not sure how to get the memory address.

#include<iostream>
#include<stdlib.h>
#include <conio.h>
using namespace std;

void display();
void create(int v);
void search(int v);
void insertend(int v);
void insertstart(int v);
void insertspeci(int k, int v);
void remove(int v);
int j=0;

struct Node
{
int item;
Node *next;
}*t, *p, *head, *tail;

int main()
{
int value, key;
cout << endl<<endl;
cout << "\t\t***************************\n";
cout << "\t\t LINKED LIST [000]TO EXIT\n";
cout << "\t\t***************************\n\n";

do
{
cout << "\t\tEnter data to be added: ";
cin >> value;
create(value);
}while (value!=000);
display();

//End

cout << "\n\n\t\tEnter data at the end of the node:";
cin >> value;
insertend(value);
display();


//Start
cout << "\n\n\t\tEnter data at the start of the node:";
cin >> value;
insertstart(value);
display();


//Insert Specified
cout << "\n\n\t\tEnter data at the specified key:";
cin >> key;
cout << "\n\t\tEnter data to be added in the specified key:";
cin >> value;
insertspeci(key, value);
display();


//Search
cout << "\n\n\t\tEnter the data you want to search:";
cin >> value;
search(value);

//Deletion
cout << "Enter the data you wish to delete:";
cin >> value;
remove(value);

display();

return 0;
}

void display()
{
p = head;
cout << endl;
cout << "\t\t***************************\n";
cout << "\t\tLinked List: ";
while (p!=NULL)
{
cout << p->item;
cout << " ";
p = p->next;
}
cout << "\n\t\t***************************\n";
cout << "\t\tPress any key to continue...";
getch();
system("cls");
}

void create(int v)
{
p = new Node;
p -> item = v;
p -> next = NULL;
if (head==0)
{
head = p;
tail = p;
p = 0;
}
else
{
tail->next=p;
tail=p;
}

}

void insertend(int v)
{
p = new Node;
p -> item = v;
p -> next = NULL;
tail->next = p;
tail = p;
}

void insertstart(int v)
{
p = new Node;
p -> item = v;
p -> next = head;
head = p;
}

void insertspeci(int k, int v)
{
p = head;
while (k!=p->item)
{
p = p->next;
}
t = new Node;
t->item = v;
t->next = p->next;
p->next = t;
}

void search(int v)
{
while ( v!= p->item)
{
if(v==p->item)
{
break;
}
else
{
p = p->next;
}
if (p==NULL)
{
cout << "Data does not exist.";
}
}
cout << "\t\tData " << p->item<< " has been found. " << " Memory Address: " <<&p;
cout << "\n\t\tPress any key to continue...";
getch();
system ("cls");

}

void remove (int v)
{
p=head;
while (v!=p->item)
{
t = p;
p = p->next;
}
t->next = p->next;
delete p;
}
Last edited on
your code, indented
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <conio.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

void display();
void create(int v);
void search(int v);
void insertend(int v);
void insertstart(int v);
void insertspeci(int k, int v);
void remove(int v);
int j = 0;

struct Node {
	int item;
	Node *next;
} * t, *p, *head, *tail;

int main() {
	int value, key;
	cout << endl << endl;
	cout << "\t\t***************************\n";
	cout << "\t\t LINKED LIST [000]TO EXIT\n";
	cout << "\t\t***************************\n\n";

	do {
		cout << "\t\tEnter data to be added: ";
		cin >> value;
		create(value);
	} while(value != 000);
	display();

	// End

	cout << "\n\n\t\tEnter data at the end of the node:";
	cin >> value;
	insertend(value);
	display();

	// Start
	cout << "\n\n\t\tEnter data at the start of the node:";
	cin >> value;
	insertstart(value);
	display();

	// Insert Specified
	cout << "\n\n\t\tEnter data at the specified key:";
	cin >> key;
	cout << "\n\t\tEnter data to be added in the specified key:";
	cin >> value;
	insertspeci(key, value);
	display();

	// Search
	cout << "\n\n\t\tEnter the data you want to search:";
	cin >> value;
	search(value);

	// Deletion
	cout << "Enter the data you wish to delete:";
	cin >> value;
	remove(value);

	display();

	return 0;
}

void display() {
	p = head;
	cout << endl;
	cout << "\t\t***************************\n";
	cout << "\t\tLinked List: ";
	while(p != NULL) {
		cout << p->item;
		cout << " ";
		p = p->next;
	}
	cout << "\n\t\t***************************\n";
	cout << "\t\tPress any key to continue...";
	getch();
	system("cls");
}

void create(int v) {
	p = new Node;
	p->item = v;
	p->next = NULL;
	if(head == 0) {
		head = p;
		tail = p;
		p = 0;
	} else {
		tail->next = p;
		tail = p;
	}
}

void insertend(int v) {
	p = new Node;
	p->item = v;
	p->next = NULL;
	tail->next = p;
	tail = p;
}

void insertstart(int v) {
	p = new Node;
	p->item = v;
	p->next = head;
	head = p;
}

void insertspeci(int k, int v) {
	p = head;
	while(k != p->item) {
		p = p->next;
	}
	t = new Node;
	t->item = v;
	t->next = p->next;
	p->next = t;
}

void search(int v) {
	while(v != p->item) {
		if(v == p->item) {
			break;
		} else {
			p = p->next;
		}
		if(p == NULL) {
			cout << "Data does not exist.";
		}
	}
	cout << "\t\tData " << p->item << " has been found. "
	     << " Memory Address: " << &p;
	cout << "\n\t\tPress any key to continue...";
	getch();
	system("cls");
}

void remove(int v) {
	p = head;
	while(v != p->item) {
		t = p;
		p = p->next;
	}
	t->next = p->next;
	delete p;
}


> it also takes "000" as an element, which I dont need.
1
2
3
4
5
	do {
		cout << "\t\tEnter data to be added: ";
		cin >> value;
		create(value);
	} while(value != 000);
you check for 0 too late
also, 000 is 0, it does not diferentiate between them.

>.When it reach search, it crashes
1
2
3
4
5
6
7
8
9
10
11
void search(int v) {
	while(v != p->item) {
		if(v == p->item) { //this could never be true
			break;
		} else {
			p = p->next;
		}
		if(p == NULL) { //too late, also, you should break the loop
			cout << "Data does not exist.";
		}
	}
¿what's the state of `p' at the start of the function?

> and im not sure how to get the memory address.
don't understand what you are asking
Last edited on
0) Please use code tags when posting code, to make it readable:

http://www.cplusplus.com/articles/z13hAqkS/

1) In your do... while loop, you read the value and then call create(value) immediately, without first checking whether it's 0. The "while" condition is only checked at the end of each iteration, to determine whether another iteration will be performed.

2) This is where a debugger will be invaluable in helping you see what's going on. In the meantime - what should the value of p be at the start of the function.

3) As a general question: why are you using so many global variables? What's the point of declaring a pointer as a global variable, when you're going to assign a new value ot it at the start of most functions? Do you really need them all to persist?

EDIT: Ninja'd on almost every point - I got called into a meeting before I could finish writing my post.
Last edited on
insertstart and insertend: if you insert into an empty list then must set head and tail.

insertspeci: if you insert at the end, you must adjust tail. The function will crash if you insert int an empty list (dereferencing a NULL pointer at line 117.

search: you start at p? Shouldn't you start at head?

Remove: this will crash if given an empty list. If you remove the first item, you have to adjust head. If you remove the last item you have to adjust tail. If you remove the only item you have to adjust both head and tail.

Basically, whenever you modify the list, you need to think about these cases:
- operating on the first item.
- operating on the last item.
- operating on the only item.
- operating on an empty list.
Topic archived. No new replies allowed.