Jigglychu
April 1st, 2003, 02:19 PM
this is my linked list program, when i try to use the sort function, i either get an error or it freezes (i think its an endless loop). Please help me!
// File lnklst.cpp
// This program builds and prints a linked list of integers.
#include <iostream.h>
#include "apqueue.h"
struct node
{
int data;
node *next;
};
apqueue <int> listrev;
void menu (node *);
void quit (void);
bool isEmpty(node *);
void initlist(node * &);
void createlist(node * &, apqueue <int> &);
void printlist(node *);
void insertAtFront(node * &, int);
void newinsert(node * &, apqueue <int> &);
void createNode(node * &, int);
void searchlist(node *);
void listlength(node *);
void del(node *);
node *find_previous(int, node *);
void displaylist(node *, apqueue <int>);
void sort (node * &);
int main(void)
{
node * head;
menu(head);
return 0;
}
void menu (node * head)
{
char choice;
cout << "Welcome to the uber-list program. Type:\n";
cout << "\t(C)reate a list\n";
cout << "\t(D)isplay the list\n";
cout << "\t(L)Print the length of the list\n";
cout << "\t(S)earch for an element in the list\n";
cout << "\t(X)Delete an item in the list\n";
cout << "\t(P)rint the list\n";
cout << "\t(Q)uit the Program\n";
cout << "\t(I)nsert a node at the front of the list\n";
cout << "\t(O)Sort the list\n";
cout << "\nPlease enter your selection:\t";
cin >> choice;
switch(choice)
{
case 'C': case 'c':
initlist(head);
createlist(head, listrev);
break;
case 'D': case 'd':
displaylist(head, listrev);
break;
case 'L': case 'l':
listlength(head);
break;
case 'S': case 's':
searchlist(head);
break;
case 'X': case 'x':
del(head);
break;
case 'P': case 'p':
printlist(head);
break;
case 'Q': case 'q':
quit();
break;
case 'I': case 'i':
newinsert(head, listrev);
break;
case 'O': case 'o':
sort(head);
break;
}
}
void initlist(node * & p)
//pre: pointer to list is declared
//post: pointer to list points to NULL
{
p = NULL;
}
void createlist(node * & p, apqueue <int> & listrev)
//pre: pointer to list has been initialized
//post: list has been created (length >= 0)
{
int number = 0;
cout << "Building list: \n\n";
while (number != -9999)
{
cout << "Enter an integer (-9999 to quit): ";
cin >> number;
if (number != -9999)
{
insertAtFront(p,number);
listrev.enqueue(number);
}
}//end while
menu(p);
}
void printlist(node * p)
//pre: list exists
//post: list is printed in reverse ordered of entry
{
int counter = 0;
node * temp = p;
cout << "\nPrint the list:\n\n";
while (temp != NULL)
{
counter++;
cout << "data item " << counter << " = " << temp -> data << endl;
temp = temp -> next;
}
menu(p);
}
void newinsert(node * & p, apqueue <int> & listrev)
//pre: head of list initialized
//post: item inserted at front of list
{
int item;
cout << "\nWhat would you like to enter at the front of the list?:\t";
cin >> item;
cout << endl;
node * temp;
if (isEmpty(p))
createNode(p,item);
else
{
createNode(temp,item);
temp -> next = p;
p = temp;
listrev.enqueue(item);
}
menu(p);
}
void insertAtFront(node * & p, int item)
//pre: head of list initialized
//post: item inserted at front of list
{
node * temp;
if (isEmpty(p))
createNode(p,item);
else
{
createNode(temp,item);
temp -> next = p;
p = temp;
}
}
void createNode(node * & p, int item)
//pre: pointer to node declared
//post: node created
{
p = new node;
p -> data = item;
p -> next = NULL;
}
bool isEmpty(node * p)
//pre: pointer to list declared
//post: list is determined to be empty or not
{
if (p == NULL)
return true;
else
return false;
}
void searchlist(node *p)
{
node * temp = p;
int found = 0, pos = 1;
int search;
cout << "\nWhat would you like to search for?";
cin >> search;
cout << "\nSearching...\n";
while (temp != NULL)
{
if (temp->data == search)
{
cout << "\n" << search << " was found in the list at position " << pos << ".\n\n";
found = 1;
menu(p);
}
else
{
temp = temp->next;
pos++;
}
}
if (found == 0)
{
cout << "\nThe item was not found.\n\n";
}
delete temp;
menu (p);
}
void listlength (node *p)
{
int counter = 0;
node * temp = p;
while (temp != NULL)
{
counter++;
temp = temp -> next;
}
cout << "There were " << counter << " items in the list." << endl;
menu(p);
}
void quit (void)
{
}
void displaylist (node * p, apqueue <int> listrev)
{
int output, count = 1;
while (!listrev.isEmpty())
{
listrev.dequeue(output);
cout << "Data Item " << count << ":\t" << output << endl;
count++;
}
menu(p);
}
node * find_previous(int i, node * p)
{
node *previous = NULL;
node *x;
for (x = p; x != NULL; x = x->next)
{
if (x->data == i)
return previous;
previous = x;
}
return NULL;
}
void del(node * p)
{
int i;
cout << "Which position would you like to delete?:\n";
cin >> i;
node *deleted = NULL;
node *previous = find_previous(i, p);
if (previous != NULL)
{
deleted = previous->next;
previous->next = previous->next->next;
}
else if (p != NULL && p->data == i)
{
deleted = p;
p = p->next;
}
if (deleted != NULL)
delete deleted;
menu(p);
}
void sort (node * & p)
{
int switchtemp, switchtemp2;
int headtemp;
bool exchanges;
do
{
exchanges = false;
while (p != NULL)
{
if (p->data < p->next->data)
{
switchtemp = p->data;
switchtemp2 = p->next->data;
p->data = switchtemp2;
p -> next->data = switchtemp;
exchanges = true;
sort(p->next);
}
}
}
while (exchanges = true);
menu(p);
}
// File lnklst.cpp
// This program builds and prints a linked list of integers.
#include <iostream.h>
#include "apqueue.h"
struct node
{
int data;
node *next;
};
apqueue <int> listrev;
void menu (node *);
void quit (void);
bool isEmpty(node *);
void initlist(node * &);
void createlist(node * &, apqueue <int> &);
void printlist(node *);
void insertAtFront(node * &, int);
void newinsert(node * &, apqueue <int> &);
void createNode(node * &, int);
void searchlist(node *);
void listlength(node *);
void del(node *);
node *find_previous(int, node *);
void displaylist(node *, apqueue <int>);
void sort (node * &);
int main(void)
{
node * head;
menu(head);
return 0;
}
void menu (node * head)
{
char choice;
cout << "Welcome to the uber-list program. Type:\n";
cout << "\t(C)reate a list\n";
cout << "\t(D)isplay the list\n";
cout << "\t(L)Print the length of the list\n";
cout << "\t(S)earch for an element in the list\n";
cout << "\t(X)Delete an item in the list\n";
cout << "\t(P)rint the list\n";
cout << "\t(Q)uit the Program\n";
cout << "\t(I)nsert a node at the front of the list\n";
cout << "\t(O)Sort the list\n";
cout << "\nPlease enter your selection:\t";
cin >> choice;
switch(choice)
{
case 'C': case 'c':
initlist(head);
createlist(head, listrev);
break;
case 'D': case 'd':
displaylist(head, listrev);
break;
case 'L': case 'l':
listlength(head);
break;
case 'S': case 's':
searchlist(head);
break;
case 'X': case 'x':
del(head);
break;
case 'P': case 'p':
printlist(head);
break;
case 'Q': case 'q':
quit();
break;
case 'I': case 'i':
newinsert(head, listrev);
break;
case 'O': case 'o':
sort(head);
break;
}
}
void initlist(node * & p)
//pre: pointer to list is declared
//post: pointer to list points to NULL
{
p = NULL;
}
void createlist(node * & p, apqueue <int> & listrev)
//pre: pointer to list has been initialized
//post: list has been created (length >= 0)
{
int number = 0;
cout << "Building list: \n\n";
while (number != -9999)
{
cout << "Enter an integer (-9999 to quit): ";
cin >> number;
if (number != -9999)
{
insertAtFront(p,number);
listrev.enqueue(number);
}
}//end while
menu(p);
}
void printlist(node * p)
//pre: list exists
//post: list is printed in reverse ordered of entry
{
int counter = 0;
node * temp = p;
cout << "\nPrint the list:\n\n";
while (temp != NULL)
{
counter++;
cout << "data item " << counter << " = " << temp -> data << endl;
temp = temp -> next;
}
menu(p);
}
void newinsert(node * & p, apqueue <int> & listrev)
//pre: head of list initialized
//post: item inserted at front of list
{
int item;
cout << "\nWhat would you like to enter at the front of the list?:\t";
cin >> item;
cout << endl;
node * temp;
if (isEmpty(p))
createNode(p,item);
else
{
createNode(temp,item);
temp -> next = p;
p = temp;
listrev.enqueue(item);
}
menu(p);
}
void insertAtFront(node * & p, int item)
//pre: head of list initialized
//post: item inserted at front of list
{
node * temp;
if (isEmpty(p))
createNode(p,item);
else
{
createNode(temp,item);
temp -> next = p;
p = temp;
}
}
void createNode(node * & p, int item)
//pre: pointer to node declared
//post: node created
{
p = new node;
p -> data = item;
p -> next = NULL;
}
bool isEmpty(node * p)
//pre: pointer to list declared
//post: list is determined to be empty or not
{
if (p == NULL)
return true;
else
return false;
}
void searchlist(node *p)
{
node * temp = p;
int found = 0, pos = 1;
int search;
cout << "\nWhat would you like to search for?";
cin >> search;
cout << "\nSearching...\n";
while (temp != NULL)
{
if (temp->data == search)
{
cout << "\n" << search << " was found in the list at position " << pos << ".\n\n";
found = 1;
menu(p);
}
else
{
temp = temp->next;
pos++;
}
}
if (found == 0)
{
cout << "\nThe item was not found.\n\n";
}
delete temp;
menu (p);
}
void listlength (node *p)
{
int counter = 0;
node * temp = p;
while (temp != NULL)
{
counter++;
temp = temp -> next;
}
cout << "There were " << counter << " items in the list." << endl;
menu(p);
}
void quit (void)
{
}
void displaylist (node * p, apqueue <int> listrev)
{
int output, count = 1;
while (!listrev.isEmpty())
{
listrev.dequeue(output);
cout << "Data Item " << count << ":\t" << output << endl;
count++;
}
menu(p);
}
node * find_previous(int i, node * p)
{
node *previous = NULL;
node *x;
for (x = p; x != NULL; x = x->next)
{
if (x->data == i)
return previous;
previous = x;
}
return NULL;
}
void del(node * p)
{
int i;
cout << "Which position would you like to delete?:\n";
cin >> i;
node *deleted = NULL;
node *previous = find_previous(i, p);
if (previous != NULL)
{
deleted = previous->next;
previous->next = previous->next->next;
}
else if (p != NULL && p->data == i)
{
deleted = p;
p = p->next;
}
if (deleted != NULL)
delete deleted;
menu(p);
}
void sort (node * & p)
{
int switchtemp, switchtemp2;
int headtemp;
bool exchanges;
do
{
exchanges = false;
while (p != NULL)
{
if (p->data < p->next->data)
{
switchtemp = p->data;
switchtemp2 = p->next->data;
p->data = switchtemp2;
p -> next->data = switchtemp;
exchanges = true;
sort(p->next);
}
}
}
while (exchanges = true);
menu(p);
}