S
sugaray
hi, I wrote a simple program which merge two single linked lists into one
for practice, but it always freezes after the creation of the first list,
hope someone might help me out with this. thanx in advance.
#include <cstdio>
#include <cstdlib>
#include <ctime>
struct LinkList {
int data;
LinkList *next;
};
LinkList *CreateList(int size) {
LinkList *head=new LinkList;
LinkList *node,*current=head;
bool *f=new bool[size]; // sentinel for distribution
int i;
for(i=0;i<size;++i) f=false;
for(i=0;i<size;++i) {
LinkList *node=new LinkList;
// to distribute random numbers equally
do node->data=rand()%size+1;
while(f[node->data]);
f[node->data]=true;
current->next=node;
current=node;
}
current->next=NULL;
delete[] f;
return head;
}
LinkList *ListTail(LinkList *head) {
LinkList *prev,*ptr=head;
while(ptr) {
prev=ptr;
ptr=ptr->next;
}
return prev;
}
void DisplayList(LinkList *head,int column) {
LinkList *ptr=head->next;
int count=0;
while(ptr) {
if(count%column==0)
printf("\n");
cout<<ptr->data;
ptr=ptr->next;
count++;
}
cout<<endl<<"NUll"<<endl;
}
int main(int argc,char **argv)
{
if(argc!=3) exit(1); // expect two arguments
srand(time(NULL));
int size=atoi(argv[1]); // size of the list
int column=atoi(argv[2]); // column for each row when displayed on the screen
LinkList *head1=CreateList(size);
DisplayList(head1); // the program cease execution after this line
LinkList *head2=CreateList(size);
DisplayList(head2);
LinkList *tail1=ListTail(head1); // get the tail of the first list
tail1->next=head2; // merge by pointing to the head of the second list
DisplayList(head1,column);
return 0;
}
for practice, but it always freezes after the creation of the first list,
hope someone might help me out with this. thanx in advance.
#include <cstdio>
#include <cstdlib>
#include <ctime>
struct LinkList {
int data;
LinkList *next;
};
LinkList *CreateList(int size) {
LinkList *head=new LinkList;
LinkList *node,*current=head;
bool *f=new bool[size]; // sentinel for distribution
int i;
for(i=0;i<size;++i) f=false;
for(i=0;i<size;++i) {
LinkList *node=new LinkList;
// to distribute random numbers equally
do node->data=rand()%size+1;
while(f[node->data]);
f[node->data]=true;
current->next=node;
current=node;
}
current->next=NULL;
delete[] f;
return head;
}
LinkList *ListTail(LinkList *head) {
LinkList *prev,*ptr=head;
while(ptr) {
prev=ptr;
ptr=ptr->next;
}
return prev;
}
void DisplayList(LinkList *head,int column) {
LinkList *ptr=head->next;
int count=0;
while(ptr) {
if(count%column==0)
printf("\n");
cout<<ptr->data;
ptr=ptr->next;
count++;
}
cout<<endl<<"NUll"<<endl;
}
int main(int argc,char **argv)
{
if(argc!=3) exit(1); // expect two arguments
srand(time(NULL));
int size=atoi(argv[1]); // size of the list
int column=atoi(argv[2]); // column for each row when displayed on the screen
LinkList *head1=CreateList(size);
DisplayList(head1); // the program cease execution after this line
LinkList *head2=CreateList(size);
DisplayList(head2);
LinkList *tail1=ListTail(head1); // get the tail of the first list
tail1->next=head2; // merge by pointing to the head of the second list
DisplayList(head1,column);
return 0;
}