Question

Using C, I need help debugging this program. I have a few error messages that I'm...

Using C, I need help debugging this program. I have a few error messages that I'm not sure how to fix.

Here is the code I have:

/*

* C Program to Print a Linked List in Reverse Order

*/

#include <stdio.h>

#include <stdlib.h>

struct node

{

int num;

struct node *next;

};

int main()

{

struct node *p = NULL;

struct node_occur *head = NULL;

int n;

printf("Enter data into the list\n");

create(&p);

printf("Displaying the nodes in the list:\n");

display(p);

printf("Displaying the list in reverse:\n");

reversedisplay(p);

release(&p);

return 0;

}

void reversedisplay(struct node *head)

{

if (head != NULL)

{

reversedisplay(head->next);

printf("%d\t", head->num);

}

}

void create(struct node **head)

{

int c, ch;

struct node temp, rear;

do

{

printf("Enter number: ");

scanf("%d", c);

temp = (struct node *)malloc(sizeof(struct node));

temp->num = c;

temp->next = NULL;

if (head == NULL)

{

head = temp;

}

else

{

rear->next = temp;

}

rear = temp;

printf("Do you wish to continue [1/0]: ");

scanf("%d", ch);

} while (ch != 0);

printf("\n");

}

void display(struct node *p)

{

while (p != NULL)

{

printf("%d\t", p->num);

p = p->next;

}

printf("\n");

}

void release(struct node **head)

{

struct node temp = head;

head = head->next;

while (head != NULL)

{

free(temp);

temp = head;

head = head->next;

}

}


Any help is appreciated. Thanks!!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include <stdio.h>

#include <stdlib.h>
void create(struct node **head);
void reversedisplay(struct node *head);
void release(struct node **head);
void display(struct node *p);
struct node

{

int num;

struct node *next;

};

int main()

{

struct node *p = NULL;

struct node_occur *head = NULL;

int n;

printf("Enter data into the list\n");

create(&p);

printf("Displaying the nodes in the list:\n");

display(p);

printf("Displaying the list in reverse:\n");

reversedisplay(p);

release(&p);

return 0;

}

void reversedisplay(struct node *head)

{

if (head != NULL)

{

reversedisplay(head->next);

printf("%d\t", head->num);

}

}

void create(struct node **head)

{

int c, ch;

struct node *temp, *rear;

do

{

printf("Enter number: ");

scanf("%d",&c);

temp = (struct node *)malloc(sizeof(struct node));

temp->num = c;

temp->next = NULL;

if (*head == NULL)

{

*head = temp;

}

else

{

rear->next = temp;

}

rear = temp;

printf("Do you wish to continue [1/0]: ");

scanf("%d",&ch);

} while (ch != 0);

printf("\n");

}

void display(struct node *p)

{

while (p != NULL)

{

printf("%d\t", p->num);

p = p->next;

}

printf("\n");

}

void release(struct node **head)

{

struct node *temp = *head;

*head = (*head)->next;

while (head != NULL)

{

free(temp);

temp = *head;

*head = (*head)->next;

}

}

Output:

Add a comment
Know the answer?
Add Answer to:
Using C, I need help debugging this program. I have a few error messages that I'm...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C LANGUAGE I just need the void push() function, which inserts new node to the front...

    C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • I need to make it so this program outputs to an output.txt, the program works fine,...

    I need to make it so this program outputs to an output.txt, the program works fine, just need it to fprintf to output.txt #include <stdio.h> #include <string.h> #include <malloc.h> #define MAX 30 struct treeNode { char names[MAX];    struct treeNode *right; struct treeNode *left; }*node; void searchName(char names[], struct treeNode ** parent, struct treeNode ** location) { struct treeNode * ptr, * tempPtr; if(node == NULL)    { *location = NULL; *parent = NULL; return; } if(strcmp(names, node->names) == 0)...

  • Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU...

    Need help for C program. Thx #include <stdio.h> #include <string.h> #include <ctype.h> // READ BEFORE YOU START: // This homework is built on homework 06. The given program is an updated version of hw06 solution. It begins by displaying a menu to the user // with the add() function from the last homework, as well as some new options: add an actor/actress to a movie, display a list of movies for // an actor/actress, delete all movies, display all movies,...

  • need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...

    need this updated so it will delete the list and then recreate it again /***********************************************************/ /* Header files. */ /***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /***********************************************************/ /* Structure definitions. */ /***********************************************************/ struct node { int data; struct node *right; struct node *left; }; struct trash { struct node *node; struct trash *next; }; /****************************************/ /* BUILD_LIST. */ /****************************************/ void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) { int i; struct node *previous, *current; *head = NULL; *tail =...

  • Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h>...

    Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list...

  • 1) Create a struct called CourseInfo to store info about a course (Include variables for courseNum,...

    1) Create a struct called CourseInfo to store info about a course (Include variables for courseNum, courseName, & grade) 2) Change the type for the 'data' member variable in the node struct to CourseInfo (see #1) and rename it to 'courseData' 3) Modify the createNode function to receive a CourseInfo struct as a parameter. It should also display the address of the new node that is created. Display the address in both hex and decimal form. 4) Modify the display...

  • (WRITE THIS PROGRAM IN C LANGUAGE).Before I ask the question, i'm just letting you know that...

    (WRITE THIS PROGRAM IN C LANGUAGE).Before I ask the question, i'm just letting you know that I asked this question before but I unfortunately the answer was wrong, in fact, it didn't even answer my questions. So please make an effort to answer this question. In this question, we will learn about a variant of linked list called \doubly Linked List". In addition to the \next" pointer pointing to the next node in the list, a node in a doubly...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT