Question

C PROGRAMMING #include <stdio.h> #include <stdlib.h> struct nodet { int data; struct nodet *link; }; struct...

C PROGRAMMING

#include <stdio.h>
#include <stdlib.h>

struct nodet
{
int data;
struct nodet *link;
};

struct nodet *makeAnode(int val)
{
struct nodet *box;
box = malloc(sizeof(struct nodet) );
box->data = val;
box->link = NULL;
return box;
}

void printList(struct nodet *L)
{
struct nodet = *mov;
mov = L;
while(mov != NULL)
{
printf("%d ", mov->data);
mov = mov->link;
}
printf("\n");
}

// THIS SHOULD COUNT HOW MANY ITEMS (NODES) ARE IN THE LIST.

int listLen(struct nodet **L)
{
int c = 0;
while(*L != NULL)
{
L = L->link;
c++;
}
return c;
}

// find the highest & lowest value in the list. Return 1 if things went as expected. Return error code -1 if the list was empty and you were unable to perform the task. Recall this list //can contain any integer value. If the list has a single value, that is both the highest & lowest. If the high number is repeated in the list, that is still the biggest.

int listMaxAndMin(struct nodet **L)
{
int max, min;
mov = *L;
if(*L == NULL)
return -1;
while(*L != NULL)
{
max = 0;
if(max < box->data)
max = box->data;
mov = mov-link;

min = 0;
if(min > box->data)
min = box->data;
mov = mov->link;
}
return 1;
}

// add the integer value X to each item in the list. The list is changed when you are done. So if you were to print the list the values would be modified.

void addXto( struct nodet **L, int val)
{
struct nodet *mov;
mov = *L;
while(*L != NULL)
{
box->data = box->data + val;
mov = mov->link;
}
}

int main()
{
}

I'm not sure if the code I have so far is right because I'm not sure how to write the main. Any pointers are helpful.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
  • Firstly, we have to clear all the compilation errors in the code.
  • In three of the functions, struct nodet** variable is being passed. This is unnecessary. stuct nodet* will be enough, it's already a pointer.
  • Then, in listMinAndMax(), min and max variables must be intialized outside the while loop. And it is better to intialize them to data in the first node of the list, rather than 0.
  • Also, the variable box is used in the functions. There is no vaiable named box declated anywhere. mov pointer must be used in its place.
  • In main(), we must create a sample linked list and test the written functions on it.

modified program: (modifications are given in bold letters)

#include <stdio.h>
#include <stdlib.h>

struct nodet
{
int data;
struct nodet *link;
};

struct nodet *makeAnode(int val)
{
struct nodet *box;
box = malloc(sizeof(struct nodet) );
box->data = val;
box->link = NULL;
return box;
}

void printList(struct nodet *L)
{
struct nodet *mov;
mov = L;
while(mov != NULL)
{
printf("%d ", mov->data);
mov = mov->link;
}
printf("\n");
}

// THIS SHOULD COUNT HOW MANY ITEMS (NODES) ARE IN THE LIST.

int listLen(struct nodet *L)
{
int c = 0;
while(L!= NULL)
{
L = L->link;
c++;
}
return c;
}

// find the highest & lowest value in the list. Return 1 if things went as expected. Return error code -1 if the list was empty and you were unable to perform the task. Recall this list //can contain any integer value. If the list has a single value, that is both the highest & lowest. If the high number is repeated in the list, that is still the biggest.

int listMaxAndMin(struct nodet *L)
{
int max, min;
struct nodet* mov;
mov = L;
if(L == NULL)
return -1;
max = mov->data;
min = mov->data;

while(mov != NULL)
{
if(max < mov->data)
max = mov->data;

if(min > mov->data)
min = mov->data;
mov = mov->link;
}

printf("Minimum value in list: %d\n", min);
printf("Maximum value in list: %d\n", max);
return 1;
}

// add the integer value X to each item in the list. The list is changed when you are done. So if you were to print the list the values would be modified.

void addXto( struct nodet *L, int val)
{
struct nodet *mov;
mov = L;
while(mov != NULL)

{
mov->data = mov->data + val;
mov = mov->link;
}
}

int main()
{
struct nodet* n1 = makeAnode(1);
struct nodet* n2 = makeAnode(2);
struct nodet* n3 = makeAnode(3);
n1->link = n2;
n2->link = n3;
printList(n1);
int len = listLen(n1);
printf("Lenght of list = %d\n", len);
listMaxAndMin(n1);
printf("Adding 5 to list elements\n");
addXto(n1, 5);
printList(n1);

}

output:

1 2 3 Lenght of list = 3 Minimum value in list: 1 Maximum value in list: 3 Adding 5 to list elements 6 7 8

Add a comment
Know the answer?
Add Answer to:
C PROGRAMMING #include <stdio.h> #include <stdlib.h> struct nodet { int data; struct nodet *link; }; struct...
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
  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

  • Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h>...

    Deleting multiples of a given integer from a linked list: #include <stdio.h> #include <stdlib.h> #include <assert.h> #define MAX 10000 typedef struct node_tag { int v; // data struct node_tag * next; // A pointer to this type of struct } node; // Define a type. Easier to use. node * create_node(int v) { node * p = malloc(sizeof(node)); // Allocate memory assert(p != NULL); // you can be nicer // Set the value in the node. p->v = v; p->next...

  • Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */...

    Please explain how these code run for each line #include<stdio.h> #include<string.h> /** * Part A */ struct myWord{    char Word[21];    int Length; }; int tokenizeLine(char line[], struct myWord wordList[]); void printList(struct myWord wordList[], int size); void sortList(struct myWord wordList[], int size); /** * main function */ int main() {    struct myWord wordList[20];    char line[100];    printf("Enter an English Sentence:\n");    gets(line);    int size = tokenizeLine(line, wordList);    printf("\n");    printf("Unsorted word list.\n");    printList(wordList, size);...

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

  • #include<stdio.h> #include <stdlib.h> void read(struct Employee *e); struct Employee { int id; int age; }; int...

    #include<stdio.h> #include <stdlib.h> void read(struct Employee *e); struct Employee { int id; int age; }; int main(){ struct Employee e; read(&e); } void read(struct Employee *e){ int a,b; printf("Enter the id employee\n"); scanf("%d",&a); printf("Enter the age employee\n"); scanf("%d",&b); e->id=a; e->age=b; } Question: Declare a pointer variable of type Employee and place the address of the variable created in the above problem in that pointer variable.

  • Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h>...

    Question 1 Consider the following program fragment that defines a self-referential class: #include <stdlib.h> #include <stdio.h> struct node_int; typedef struct node int *node; struct node_int void *data; node next; typedef struct list_int (node first;} *list; void main(int argc, char *argv[]) list shopping, t; node n; int x=25; shopping=(list) malloc(sizeof(struct list_int)); n= (node) malloc(sizeof(struct node_int)); n->data=shopping; n->next=NULL; shopping->first=n; t=(list) (shopping->first->data); // ***** t->first=NULL; // ***** n->data=&x; printf("%d\n", *((int *) (shopping->first->data))); a What will be displayed on the screen if the above...

  • #include <stdlib.h> int main() { struct fruit Z; food X = (struct fruit *)malloc(sizeof(struct fruit)); X->taste[4]...

    #include <stdlib.h> int main() { struct fruit Z; food X = (struct fruit *)malloc(sizeof(struct fruit)); X->taste[4] = (struct flavour *)malloc(sizeof(struct flavour)); X->taste[4]->score = 5; X->link = &Z; return 0; } Given the code above, reverse engineer the necessary data structure and other definitions not shown above. Give your answer as C code.

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

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

  • IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node;...

    IN C Programming #include<stdio.h> #include<stdlib.h> typedef struct nodestruct { int item; struct nodestruct *next; } Node; typedef struct { int size; // Number of items on user’s list Node *head, *tail; } List; //In addition to creating an empty list, this function will also create two dummy nodes; one for the head, and the other for the tail. List* createList(); //This function creates a node containing the provided item, then inserts it into the list pointed by the provided list...

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