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
  • 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...

  • 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,...

  • 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)...

  • ****Using C and only C**** I have some C code that has the function addRecord, to...

    ****Using C and only C**** I have some C code that has the function addRecord, to add a record to a linked list of records. However, when I run it, the program exits after asking the user to input the address. See picture below: Here is my code: #include<stdio.h> #include<stdlib.h> struct record { int accountno; char name[25]; char address[80]; struct record* next; }; void addRecord(struct record* newRecord) //Function For Adding Record at last in a SinglyLinkedList { struct record *current,*start,*temp;...

  • 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 =...

  • 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...

  • 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...

  • #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ...

    #include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...

  • I need help debugging this Java program. I am getting this error message: Exception in thread...

    I need help debugging this Java program. I am getting this error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at population.Population.main(Population.java:85) I am not able to run this program. ------------------------------------------------------------------- import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Scanner; /* Linked list node*/ class node { long data; long year; String country; node next; node(String c,long y,long d) { country=c; year=y; data = d; next = null; } } public class Population { private static node head; public static void push(String...

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
ADVERTISEMENT