Empty Linked List
Hi, I've got an assignment where we have to start out with an empty linked list that is formatted as
IntNode* head = NULL;
Every time I attempt to run it, it goes to that line and then the program stops working.
Any ideas? I'll include the rest of the code below.
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#include "IntNode.h"
#include <iostream>
using namespace std;
bool run = true;
void start()
{
cout << "1. Add an integer to the front of the list" << endl;
cout << "2. Add an integer to the back of the list" << endl;
cout << "3. Remove a numer from the list" << endl;
cout << "4. Print the list" << endl;
cout << "5. Print the number of items from the list" << endl;
cout << "6. Quit" << endl;
cout << "Enter your choice." << endl;
}
void insertFront(IntNode* &head)
{
int num;
cout << "Enter the number:";
cin >> num;
IntNode* newFront = new IntNode;
newFront -> data = num;
newFront -> next = head;
head = newFront;
cout << "Added the number" << endl;
}
void print(IntNode* head)
{
IntNode* curr = head;
cout << "The List is :";
while(curr != NULL)
{
cout << curr -> data << " ";
curr = curr -> next;
}
cout << endl;
}
void insertAfter(IntNode* head)
{
int num;
cout << "Enter the number:";
cin >> num;
IntNode* back = new IntNode;
back->data = num;
back->next = NULL;
IntNode* curr = head;
while (curr->next != NULL)
{
curr = curr->next;
}
curr->next = back;
cout << "Added the number." << endl;
}
void remove(IntNode* &head)
{
int num;
cout << "Enter the number:";
cin >> num;
IntNode* curr = head;
IntNode* prev = NULL;
while ((curr != NULL) && (curr->data != num)) {
prev = curr;
curr = curr -> next;
}
if ((prev == NULL) && (curr != NULL)) {
head = head -> next;
delete curr;
curr = NULL;
}
else if (curr != NULL) {
prev -> next = curr -> next;
delete curr;
curr = NULL;
}
cout << "Removed." << endl;
cout << endl;
}
void printNumberOfElement(IntNode* head)
{
IntNode* curr = head;
int count = 0;
while (curr != NULL)
{
count++;
curr = curr -> next;
}
cout << "The Number of List :";
cout << count <<endl;
}
void quit()
{
run = false;
}
int main()
{
IntNode* head = NULL;
head -> data = NULL;
head -> next = NULL;
cout<<"Welcome to the linked list program."<<endl;
cout<<"What would you like to do?"<<endl;
start();
while(run == true)
{
int n;
int choice;
cout << endl;
cout<<"Enter your choice: ";
cin >> n;
choice = n;
if (choice == 1)
{
insertFront(head);
}
if (choice == 2)
{
insertAfter(head);
}
if (choice == 3)
{
remove(head);
}
if (choice == 4)
{
print(head);
}
if (choice == 5)
{
printNumberOfElement(head);
}
if (choice == 6)
{
quit();
}
}
}
|
The Header file contains two public fields, data that takes and int and next, which takes an IntNode pointer
Can anyone help me out?
Nevermind, I figured it out.
Topic archived. No new replies allowed.