hey guys...
you people told me to put nice names, so i did..
you also told that a few vars. were uninitialized , so i initialized them...
but i do get an error....
(note: this is an entirely new program so kindly carefully go through it):
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
struct node{
int data;
node *link;
};
node *head = NULL;
node *traverser = NULL;
node* create(int data){
node *tempNode = new node;
tempNode -> data = data;
tempNode -> link = NULL;
return tempNode;
}
void insertFirst(node *nodeToBeInserted){
head = traverser = nodeToBeInserted;
}
void insertCustom(int insertAfter,node* nodeToBeInserted){
node *current = head;
node *tempNode,*tempNode2;
while(current != NULL){
if(current -> data == insertAfter){
tempNode2 = current -> link;
current -> link = tempNode = new node;
tempNode = nodeToBeInserted;
tempNode -> link = tempNode2;
break;
}else{
current = current -> link;
}
}
if(current == NULL){
cout << "Sorry user, the entire list was searched but a node with the value " << insertAfter << " was not found!!!" << endl;
}
}
void deleteFirst(){
node *tempVar = head;
head = traverser = head -> link;
delete tempVar;
}
void deleteCustom(int deletableNodesData){
node *tempVar = head,*tempVar2;
node *trail = NULL;
while(tempVar != NULL){
if(tempVar -> data == deletableNodesData){
tempVar2 = tempVar;
tempVar = tempVar -> link;
delete tempVar2;
trail = tempVar;
}else{
tempVar = tempVar -> link;
trail = tempVar;
}
}
if(tempVar == NULL){
cout << "Sorry user, the entire list was searched but there wasn't any node that contained " << deletableNodesData << ")....";
}
}
void display(){
node *tempVar = head;
while(tempVar != NULL){
if(tempVar -> link != NULL){
cout << tempVar -> data << " =>";
}else{
cout << tempVar -> data << " => NULL";
}
tempVar = tempVar -> link;
}
}
int main(){
char x = 'y';
while(x == 'y' || x == 'Y'){
cout << "Welcome to the linked list program...";
cout << "Here's our menu:\n1)Create node...\n2)Delete node...\n3)Display node...\n4)Exit...";
cout << "\n\nEnter choice (1,2,3 or 4): ";
int a;
cin >> a;
cin.ignore();
if(a == 1){
cout << "Enter a number, to be stored in the node to be created: ";
int b;
cin >> b;
cin.ignore();
node *temp = create(b);
if(head == NULL){
insertFirst(temp);
cout << "Successfully created and added the node...\n(The node was added in the beginning of the list since there wasn't any other node)...";
}else{
cout << "After which node do you wanna insert this node?(Enter the data stored in that node): ";
int c;
cin >> c;
cin.ignore();
insertCustom(c,temp);
cout << "Successfully created and added the node...\n(After the node containing " << c << " in it)...";
}
}else if(a == 2){
cout << "Enter the data stored in the node to be deleted: ";
int d;
cin >> d;
cin.ignore();
if(head -> data == d){
deleteFirst();
cout << "Successfully deleted the node containing " << d << " in it...";
}else{
deleteCustom(d);
cout << "Successfully deleted the node containing " << d << " in it...";
}
}else if(a == 3){
display();
}else if(a == 4){
cout << "===PRESS ENTER TO EXIT===";
cin.get();
return 0;
}else{
cout << "Sorry user the program doesn't recognise " << a << " as a command..\nPlease try again...";
}
cout << "Do you wanna use the program again?(y/n): ";
cin >> x;
system("cls");
}
}