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;
}
#include <conio.h>
#include <iostream>
#include <stdlib.h>
usingnamespace 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
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.
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.