-
link list
I need to create a circular linklist so that the last node in the list points at the head instead of null. But I am a nubee and really have a hard time with the creation of link list. I tried to create a link list last week that ended with null but I could not get it to function properly so now I have a program to do that uses a circular link list to pull 10 names out of a txt file and put them into order alphabetically so im trying to convert my program from last week to help me with the circular list program. I probally should start from scratch but I thought I would ask someone on here before I started over. So if you know why this is not working properly please throw me a hint. please disreguard the cout statements I was using them just to trouble shoot.
Thanks
Fastlane 29
Code:
#include <stdio.h>
#include <iostream.h>
#include <fstream.h>
#include <string.h>
const int ItemLen = 12;
//Node definition
struct ListNode
{
char name[ItemLen];
char lastname[ItemLen];
ListNode *link;
};
typedef ListNode* ListNodePtr;
ListNodePtr Head;
int main(int argc, char *argv[])
{
char filename[ItemLen];
char filelastname[ItemLen];
char transcode[2];
char transname[ItemLen];
char translastname[ItemLen];
char changedname[ItemLen],changedlastname[ItemLen];
int i,j;
ListNodePtr p,q,temp;
Head= new ListNode;
q=new ListNode;
temp=new ListNode;
ifstream inFile;
ofstream output;
ofstream out_stream;
Head=NULL;
inFile.open("a:GOLFNAME.txt",ios::in);
inFile>> filename>> filelastname;
cout<<filename<<" ? "<<filelastname<<endl;
while(!inFile.eof())
{
p=new ListNode;
strcpy(p->name,filename);
strcpy(p->lastname,filelastname);
if(Head==NULL)
{
Head=p;
p->link=NULL;
}
else
{
q=Head;
if(q->link==NULL)
{
p->link=q;
Head=p;
}
else
{
q=Head;
while((strcmp(p->name , q->name)>0)&& (strcmp(p->lastname , q->lastname)!=0))
{
cout<< "here I am";
temp=q;
q=q->link;
}
p->link=q;
temp->link=p;
}}
inFile>>filename>>filelastname;
cout<<"2nd"<<filename<<" ? "<<filelastname<<endl;
cout<<"plink= "<<p->name<<" "<<p->lastname<<endl;
}
inFile.close();
//out_stream.open("a:GOLFNAME.alp");
p=Head;
while(j>10)
{
//out_stream<< p->name << " " << p->lastname << endl;
cout<<"print "<<endl;
cout<< p->name << " " << p->lastname << endl;
p= p->link;
j++;
cin>>i;
}
cout<<"press any number it is the end of program"<<endl;
cin>>i;
return 0;
}
-
Use Standard C++ if possible. That means do not use .h in your header files.
Have you compiled your posted code. It may help to post your possible errors here as well.
mr. c
-
Use a function! This will make your life easier.
Code:
//...This function requires no preparation.
AttachNew(ListNode *head, ListNode *node) {
node = new ListNode;
node->link = NULL;
if(head == NULL)
head = node;
else {
ListNode *p = head;
while(p->link != NULL) {
p = p->link;
}
p->link = node;
}
}
Using this type of list is now easy:
Code:
char name[100];
ListNode
*head = NULL, //..this flags the function...
*node;
do{
cout << "enter a name or type 'bye' to exit" << endl;
cin >> name;
if(strcmp(name, "bye"))
break;
else {
AttachNew(head, node);
strcpy(node->name, name);
}while(1);
You can easily restructure this to turn out circular lists, too.
-
to have a singly linked circular list of three nodes, p, q , and r, you would do something like this:
struct Node
{
//data members
Node * link;
};
Node * p;
Node * q;
Node * r;
p->link = q;
q->link = r;
r->link = p;
Note now how p and r are linked together to create the circle. There must be one such link in your circular list but I can't find it in your code. Once you have that, you then need some way to tell where to start and where to stop searching the list since the usually stopping point if condition not met earlier, is the NULL pointer at the end of a straight list, but it isn't there anymore.
It looks as if you are trying to list names alphabetically by one name, and then subsort alphabetically by another name. If so, then I think you need many more cases. It will probably get pretty messy by the time you're done setting up such a sorting routine, so if you don't have to do it, don't.
-
Not exactly. A circular list starts at the head, and the last node points to the head. As long as you keep track of this head, you will always know when to stop. Also, you can easily "rotate" the list thus allowing the head to be any node you desire. This is actually easier than it sounds, all that is needed is a pointer swap.