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;
}