my first time here and a beginner, so bear with me.
I got an assignment dealing with a doubly linked list and we were given two print functions that prints the linked list forward and in reverse. We are not allowed to alter those functions in anyways.
I am unable to pass the linked list through while(1) passing to it. I made a test function where it iterates the list with while(pointer != NULL) and that works fine.
Any suggestions?
This is my main function.
FILE *fp;
struct node{
int data;
struct node* prev;
struct node* next;
};
typedef struct node* listPointer;
listPointer headNode = NULL;
listPointer tailNode = NULL;
void delete(listPointer *head, listPointer removalNode);
//int traverseList(listPointer head, int data);
listPointer insertBefore(listPointer *head, listPointer nextNode, listPointer insertionNode);
listPointer findPosition(listPointer *head, int data);
listPointer findNode(listPointer *head, int data);
listPointer insertAtHead(listPointer *head, listPointer insertionNode);
listPointer insertAtBack(listPointer *head, listPointer insertionNode);
listPointer createNode(int data);
void print_forward(listPointer list);
void print_reverse(listPointer list);
void sortedInsert(listPointer *head,listPointer *tail,int data)
{
listPointer p = malloc(sizeof(listPointer));
listPointer temp = malloc(sizeof(listPointer));
p->data = data;
p->next = NULL;
if ((*head) == NULL)
{
(*head) = p;
(*tail) = p;
(*head)->prev = NULL;
return;
}
if ((p->data) < ((*head)->data))
{
p->prev = NULL;
(*head)->prev = p;
p->next = (*head);
(*head) = p;
return;
}
if ((p->data) > ((*tail)->data))
{
p->prev = (*tail);
(*tail)->next = p;
(*tail) = p;
return;
}
temp = (*head)->next;
while ((temp->data) < (p->data))
temp = temp->next;
(temp->prev)->next = p;
p->prev = temp->prev;
temp->prev = p;
p->next = temp;
}
///test print function
void printlist(listPointer head) // this is a test function
{
listPointer temp = head;
while(temp != NULL) // works, but seg fault when is set to while(1) like line 282
{
printf("%d - ", temp->data);
temp = temp->next;
}
printf("\n");
}
test print function reverse
void printlist2(listPointer head)
{
listPointer temp = head;
while( temp != NULL)
{
printf("%d - ", temp->data);
temp = temp->prev;
}
printf("\n");
}
void main(int argc, char** argv)
{
if(argc != 2)
{
fprintf(stderr, "usage: ./mp1 input_filename\n");
exit(1);
}
FILE *fp = fopen(argv[1], "r");
if(fp == NULL)
{
fprintf(stderr, "The input file does not exist.\n");
exit(1);
}
char *op;
listPointer temp;
listPointer newnode;
int data=0;
int flag=0;
char c;
int num;
///////////////////////////////////////////////////////// TESTING
data = 10;
op = "INSERT";
sortedInsert(&headNode,&tailNode,data);
sortedInsert(&headNode,&tailNode,6);
sortedInsert(&headNode,&tailNode,7);
printlist(headNode);
printf("\nhead %d\n",headNode->data);
printf("tail %d\n",tailNode->data);
printlist2(tailNode);
print_forward(headNode); // seg fault
///////////////////////////////////////////////////////// TESTING
/*while(!feof(fp)){
fscanf(fp,"%s",op);
fscanf(fp,"%d",&data);
//printf("%s ",op);
//printf("%d ",data);
if(strcmp(op,"INSERT")==0){flag=1;}
else if(strcmp(op,"DELETE")==0){flag=2;}
else if(strcmp(op,"ASCEND")==0) {flag=3;}
else if(strcmp(op,"DESCEND")==0) {flag=4;}
switch(flag)
{
case 1:
newnode = createNode(data);
if(headNode == NULL) insertAtHead(&headNode,newnode);
else{
temp = findPosition(&headNode,data);
insertBefore(&headNode,temp,newnode);
}
break;
case 2:
temp = findNode(&headNode,data);
delete(&headNode,temp);
break;
case 3:
print_forward(headNode);
break;
case 4:
print_reverse(headNode);
break;
default: break;
}
}*/
fclose(fp);
}
And this are the given print functions.
void print_forward(listPointer list) {
listPointer curr;
FILE *outfile;
outfile = fopen("mp1_result.txt", "a");
if(list) {
curr = list;
while(1) {
fprintf(outfile, "%d ", curr->data);
printf("%d ", curr->data);
curr = curr->next;
if(curr == list) break;
}
}
fprintf(outfile, "\n");
printf("\n");
fclose(outfile);
}
void print_reverse(listPointer list) {
listPointer curr;
FILE *outfile;
outfile = fopen("mp1_result.txt", "a");
if(list) {
curr = list->prev;
while(curr != list) {
fprintf(outfile, "%d ", curr->data);
printf("%d ", curr->data);
curr = curr->prev;
}
fprintf(outfile, "%d ", curr->data);
printf("%d ", curr->data);
}
fprintf(outfile, "\n");
printf("\n");
fclose(outfile);
Any suggestions welcomed and appreciated!
fopenfails. There is no excuse for not doing so.if(fp == NULL) { fprintf(stderr, "The input file does not exist.\n"); exit(1); }Is this wrong? Thanks for the input!fopens in your code.cse20172184@cspro:~/datastructures/mp1$ ./mp1 input.txt 6 - 7 - 10 - head 6 tail 10 10 - 7 - 6 - Segmentation fault (core dumped)