circular linked->list help
Here is my attempt at writing a circularly linked list. Everything compiles fine (using Borland) but when I try to run this program.. it just locks up. Maybe an extra pair of eyes can help. :cool:
push_node( ) uses head node insertion.. and will connect tail_ptr to head_ptr after the 6th node. Once this code is working, I will later add a "spin the wheel" function.. the will move all nodes clockwise by one node
Code:
#include<iostream>
#include<cctype>
#include<string>
#include<iomanip>
#include<conio.h>
using namespace std;
struct node
{
string name;
node *next;
node *prev;
};
class Linkedlist
{
public:
Linkedlist();
void display_menu();
void display_graph();
string get_name(const int&);
void push_node();
void pop_node();
void exit_message();
~Linkedlist();
private:
int node_counter;
string entry;
string name;
node *head_ptr;
node *tail_ptr;
};
int main()
{
Linkedlist link;
char option;
do
{
clrscr();
link.display_menu();
link.display_graph();
option = toupper(getch());
switch(option)
{
case 'A': link.push_node();
break;
case 'R': link.pop_node();
break;
case 'E': link.exit_message();
break;
default: cout << "\n\nInvalid Entry, Try again.\n\n";
}
}while (option != 'E');
return 0;
}
////////////////////////
//Function Definitions//
////////////////////////
//i would like to use something like this to initialize my struct
//node::name(test) : next(NULL) : prev(NULL) { }
Linkedlist::Linkedlist()
{
node_counter = 0;
node *head_ptr = NULL;
node *tail_ptr = NULL;
}
void Linkedlist::display_menu()
{
cout << "\n\n\t\t*************************************"
<< "\n\t\t* Press 'A' to Add a name *"
<< "\n\t\t* Press 'R' to Remove from the list *"
<< "\n\t\t* Press 'E' to Exit *"
<< "\n\t\t*************************************"
<< endl << endl << endl << endl;
}
void Linkedlist::display_graph()
{
cout << setw(35) << get_name(1) << endl << endl;
cout << setw(15) << get_name(6) << setw(50) << get_name(2) << endl << endl;
cout << setw(15) << get_name(5) << setw(50) << get_name(3) << endl << endl;
cout << setw(35) << get_name(4) << endl << endl << endl;
}
string Linkedlist::get_name(const int& iter)
{
node *cursor;
string answer;
cursor = head_ptr;
for (int counter = 0; counter < iter && cursor->next != NULL; counter++)
{
cursor = cursor->next;
answer = cursor->name;
}
return answer;
}
void Linkedlist::push_node()
{
++node_counter;
node *temp_ptr;
temp_ptr = new node;
cout << "\n\n\tEnter Name: ";
getline(cin, entry);
temp_ptr->name = entry;
if (head_ptr != NULL) //This block will be the most frequent occurrance
{ //Perform the double-link.. the old head node will
head_ptr->prev = temp_ptr; //point to the new temp node.. and the new temp
temp_ptr->next = head_ptr; //node will point to the old head node.
}
else //(head_ptr == NULL) //This block is for the first node to be added to the
{ //linked list. This is a "special case" that will
temp_ptr->next = NULL; //facilitate the "empty list" scenario. When adding the first
tail_ptr = temp_ptr; //node to the list, *next should be set to NULL.. also assign
} //this node as the tail.
if (node_counter > 6) //This block is designed to perform the circular closure
{ //of the linked list (this will occur after the 6th node)
node *new_tail;
new_tail = tail_ptr->prev; //create a new tail (so we can delete the old one)
delete tail_ptr; //delete the extra 7th node
new_tail->next = temp_ptr; //perform the circular closure by having the last node
//point to the new temporary head node
temp_ptr->prev = new_tail; //and also have the new temp node point to the tail
tail_ptr = new_tail; //tail_ptr will now be the 6th node.
}
head_ptr = temp_ptr; //after all is done, assign the new temp node
//as the head node.
if (node_counter < 6) //if there are less than 6 nodes.. then the circle
//will not exist.. so the head node->prev should not
head_ptr->prev = NULL; //point to anything
delete temp_ptr;
}
void Linkedlist::pop_node()
{
if (head_ptr == NULL)
cout << "\n\n\tNo more nodes..!!\a ";
else
{
--node_counter;
node *new_head;
new_head = head_ptr->next;
delete head_ptr;
head_ptr = new_head;
tail_ptr->next = NULL; //Break the Circle
head_ptr->prev = NULL; //
}
if (node_counter == 0) //Make sure head_ptr and tail_ptr are NULL
{ //if all nodes have been popped.
head_ptr = NULL;
tail_ptr = NULL;
}
}
void Linkedlist::exit_message()
{
clrscr();
cout << "\n\n\tProgram written in c++ by David Weirich"
<< "\[email protected]\n\n\n";
}
Linkedlist::~Linkedlist()
{
while (node_counter)
pop_node();
}