belch
asked on
linked list
I have the following code for a linked list. This code has one list and a user can enter values into the list then print it in order or reverse order. How would I change this code to create 5 link lists and the user can add values to the different linked lists?
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
struct list_item
{
char string[50];
list_item* next;
list_item* previous;
};
class list
{
private:
list_item* head; // Ptr to head of list
list_item* tail; // Ptr to tail of list
public:
list(); // Constructor - initialise empty list
bool insert( char* ); // Add string to tail of list
void display(); // Display list
void display_reverse(); // Display list in reverse order
};
list::list() // list constructor
{
head = NULL;
tail = NULL;
}
bool list::insert( char* data )
{
list_item* new_item;
new_item = new list_item;
if ( new_item == NULL )
return false;
strcpy( new_item -> string, data );
if ( head == NULL )
{
tail = new_item;
head = new_item;
new_item -> next = NULL;
new_item -> previous = NULL;
}
else
{
tail -> next = new_item;
new_item -> previous = tail;
new_item -> next = NULL;
tail = new_item;
}
return true;
}
void list::display()
{
list_item* current;
current = head;
if ( current == NULL )
cout << "\n\n Empty list!\n";
else
cout << "\n\n List:\n";
while ( current != NULL )
{
cout << " " << current -> string << "\n";
current = current -> next;
}
}
void list::display_reverse()
{
list_item* current;
current = tail;
if ( current == NULL )
cout << "\n\n Empty list!\n";
else
cout << "\n\n List:\n";
while ( current != NULL )
{
cout << " " << current -> string << "\n";
current = current -> previous;
}
}
void main( void )
{
char string[50];
char choice;
list l;
bool result;
do {
cout << "\n\n Menu:\n\n 1. Add to list\n 2. Print list\n";
cout << " 3. Print list in reverse order\n 4. Quit\n\n Please enter your choice: ";
cin >> choice;
switch( choice )
{
case '1':
cout << "\n Type string to add to list: ";
cin >> string;
result = l.insert( string );
if ( !result )
cout << "\n Insufficient memory!";
break;
case '2':
l.display();
break;
case '3':
l.display_reverse();
break;
}
} while ( choice != '4' );
}
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
struct list_item
{
char string[50];
list_item* next;
list_item* previous;
};
class list
{
private:
list_item* head; // Ptr to head of list
list_item* tail; // Ptr to tail of list
public:
list(); // Constructor - initialise empty list
bool insert( char* ); // Add string to tail of list
void display(); // Display list
void display_reverse(); // Display list in reverse order
};
list::list() // list constructor
{
head = NULL;
tail = NULL;
}
bool list::insert( char* data )
{
list_item* new_item;
new_item = new list_item;
if ( new_item == NULL )
return false;
strcpy( new_item -> string, data );
if ( head == NULL )
{
tail = new_item;
head = new_item;
new_item -> next = NULL;
new_item -> previous = NULL;
}
else
{
tail -> next = new_item;
new_item -> previous = tail;
new_item -> next = NULL;
tail = new_item;
}
return true;
}
void list::display()
{
list_item* current;
current = head;
if ( current == NULL )
cout << "\n\n Empty list!\n";
else
cout << "\n\n List:\n";
while ( current != NULL )
{
cout << " " << current -> string << "\n";
current = current -> next;
}
}
void list::display_reverse()
{
list_item* current;
current = tail;
if ( current == NULL )
cout << "\n\n Empty list!\n";
else
cout << "\n\n List:\n";
while ( current != NULL )
{
cout << " " << current -> string << "\n";
current = current -> previous;
}
}
void main( void )
{
char string[50];
char choice;
list l;
bool result;
do {
cout << "\n\n Menu:\n\n 1. Add to list\n 2. Print list\n";
cout << " 3. Print list in reverse order\n 4. Quit\n\n Please enter your choice: ";
cin >> choice;
switch( choice )
{
case '1':
cout << "\n Type string to add to list: ";
cin >> string;
result = l.insert( string );
if ( !result )
cout << "\n Insufficient memory!";
break;
case '2':
l.display();
break;
case '3':
l.display_reverse();
break;
}
} while ( choice != '4' );
}
Why not use the std::list instead of a user defind list class?
This sounds a lot like homework. If it is not homework, you have big problems because your linked-list leaks memory.
I will give you a hint. You only have to make a few changes inside the main function.
I will give you a hint. You only have to make a few changes inside the main function.
ASKER
It is a small part of a homework assignment. Just looking for guidance, not someone to do the work for me.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
One thing is you need a destuctor which will delete all the nodes left after the program has terminated otherwise the memory that was allocated to the creation of the list nodes is never freed (memory leak).
I would recommend the book "Data Abstraction and Problem Solving with C++" by Carrano, Helman, & Veroff - ISBN 0-201-87402-4. It has really great examples of data structures in general including linked lists.
I would recommend the book "Data Abstraction and Problem Solving with C++" by Carrano, Helman, & Veroff - ISBN 0-201-87402-4. It has really great examples of data structures in general including linked lists.