Linked list Runtime error...
When i run my program and add a few nodes to my linked list and then when i try to delete nodes a runtime error occurs that states: Program received signal SIGSEGV, Segmentation fault.
here's the program: can anyone help please???
Code:
#include <iostream>
using namespace std;
struct node{
int a;
int id;
node *next;
};
node *X,*Y,*Z,*U;
node *createNode(int num,int i);
void addToList(node* z);
void display();
void deleteNode(int ID);
int main(){
char d;
do{
char c;
cout << "Choose:\n1)Create node\n2)Delete node\n3)Display list\n4)Exit\n\nWrite 1,2 or 3: ";
int uva;
cin >> uva;
cin.ignore();
if(uva == 1){
do{
cout << "Enter a number: ";
int a;
cin >> a;
cin.ignore();
cout << "Enter an id for your node: ";
int i;
cin >> i;
Z = createNode(a,i);
addToList(Z);
cout << "Do you want to create an new node?(y/n): ";
cin >> c;
cin.ignore();
}while(c == 'y' || c == 'Y');
}else if(uva == 2) {
cout << "Enter the id of the node to delete: ";
int uno;
cin >> uno;
deleteNode(uno);
}else if(uva == 3){
cout << "Do you want to display the whole list?(y/n): ";
cin >> c;
cin.ignore();
if(c == 'y' || c == 'Y')
display();
}else if(uva == 4){
cout << "\n\n===PRESS ENTER TO EXIT===";
cin.get();
return 0;
}else{
cout << "UNKNOW NUMBER!!";
}
cout << "\n\nDo you want to use the program again?(y/n): ";
cin >> d;
cin.ignore();
}while(d == 'y' || d == 'Y');
cout << "\n\n===PRESS ENTER TO EXIT===";
cin.get();
}
node *createNode(int num,int i){
node *oneobj = new node;
oneobj -> a = num;
oneobj->next = NULL;
oneobj->id = i;
return oneobj;
}
void addToList(node* z){
if(X == NULL){
X = Y = z;
}
else{
Y->next = z;
Y=z;
}
}
void display(){
while(X->next != NULL){
if(X != NULL){
cout<< X->a << " -> ";
X = X->next;
}
}
cout << X->a << " -> NULL";
}
void deleteNode(int ID){
while(X->id != ID){
X = X->next;
}
U = X;
X = X->next;
delete U;
}