Question

In: Computer Science

list.h file #ifndef LIST_H_ #define LIST_H_ struct ListNode { long value; struct ListNode *next; }; struct...

list.h file

#ifndef LIST_H_
#define LIST_H_

struct ListNode {
long value;
struct ListNode *next;
};

struct ListNode *list_prepend(struct ListNode *list, long value);

int list_length(struct ListNode *list);
struct ListNode *list_remove(struct ListNode *list, long value);

#endif /* LIST_H_ */

given.c file

#include

#include "list.h"

struct ListNode *list_prepend(struct ListNode *list, long value) {
struct ListNode *node = malloc(sizeof(struct ListNode));
node->value = value;
node->next = list;

return node;
}

list.c file

#include

#include "list.h"

/* Counts and returns the number of nodes in the given list. */
int list_length(struct ListNode *list) {
return -1;

}


/* Searches the given list for the first node containing the given
* value. If such a list element is found, it is removed from the list
* and freed
* Returns the resulting list after any adjustments are made. */
struct ListNode *list_remove(struct ListNode *list, long value) {
return NULL;
}

Solutions

Expert Solution

list.c

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

/* Counts and returns the number of nodes in the given list. */
int list_length(struct ListNode *list) {
   int count = 0;
   while(list != NULL){
       count++;
       list = list->next;
   }
   return count;
}


/* Searches the given list for the first node containing the given
* value. If such a list element is found, it is removed from the list
* and freed
* Returns the resulting list after any adjustments are made. */
struct ListNode *list_remove(struct ListNode *list, long value) {
   if(list == NULL) return NULL;
   struct ListNode* current = list,*prev = NULL;
   if(list->value == value){
       list = list->next;
       free(current);
       return list;
   }
   while(current != NULL && current->value != value){
       prev = current;
       current = current->next;
   }
   if(current == NULL) return list;
   prev->next = current->next;
   free(current);
   return list;
  
}


Related Solutions

Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define...
Use the functions.h header file with your program (please write in C code): #ifndef FUNCTIONS_H #define FUNCTIONS_H typedef struct MyStruct { int value; char name[ 100 ]; } MyStruct; void sortArray( MyStruct*, int ); void printArray( MyStruct*, int ); #endif Create a source file named functions.c with the following: A sorting function named sortArray. It takes an array of MyStruct's and the length of that array. It returns nothing. You can use any of the sorting algorithms, you would like...
I create a h file, and I wrote #ifndef MYSTACK_H #define MYSTACK_H #include <cstdlib> #include <cstddef>...
I create a h file, and I wrote #ifndef MYSTACK_H #define MYSTACK_H #include <cstdlib> #include <cstddef> #include <iostream> struct node { int value; node* next; node(int value, node* next = nullptr) { this->value = value; this->next = next; } }; class mystack { private: node* stack_top; size_t stack_size; public: mystack(); mystack(const mystack& x); ~mystack(); mystack& operator=(const mystack& x); size_t size() const; bool empty() const; void clear(); const int& top() const; void push(int value); void pop(); void clone(const mystack& x); };...
Use the provided BingoBall.h and Set.h files to implement the Set.cpp file. //File: BingoBall.h #ifndef BINGOBALL_H...
Use the provided BingoBall.h and Set.h files to implement the Set.cpp file. //File: BingoBall.h #ifndef BINGOBALL_H #define   BINGOBALL_H #include <iostream> class BingoBall { public:    BingoBall():letter{'a'}, number{0} {}    BingoBall(char let, int num) :        letter{ let }, number{ num } {}    char getChar() const { return letter; }    int getNumber() const { return number; }    //overload == operator    bool operator==(BingoBall &b) const { return (number == b.getNumber() && letter == b.getChar()); } private:   ...
Parse the dirent struct to see if an entry is a directory or a file. If...
Parse the dirent struct to see if an entry is a directory or a file. If it is a directory, prepend "./" to the filename before printing it out. Include a main that does this below. Please do the above code using C language.
Using the ListNode file. Write methods called min and max that return the smallest and largest...
Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value. Write a method called insertNode that inserts a new node...
Using the ListNode file. Write methods called min and max that return the smallest and largest...
Using the ListNode file. Write methods called min and max that return the smallest and largest values in the linked list. These methods will be added to your ListNode class. For example if a variable called list stores {11, -7, 3, 42, 0, 14], the call of list.min() should return -7 and the call of list.max() should return 42. If the list is empty, return -1. Print the returned value. Write a method called insertNode that inserts a new node...
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class....
1. (10 pts) Define the nodes in the LinkedList. Create the LinkedList using the ListNode class. Create a method to find a node with given value in a LinkedList. Return the value is this value exists in the LinkedList. Return null if not exists. Use these two examples to test your method. Example 1: Input: 1 -> 2 -> 3, and target value = 3 Output: 3 Example 2: Input: 1 -> 2 -> 3, and target value = 4...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef...
#include<stdlib.h> #include<stdio.h> typedef struct node {    void* dataPtr;    struct node* next; } QUEUE_NODE; typedef struct {    QUEUE_NODE* front;    QUEUE_NODE* rear;    int count; } QUEUE; //Prototype Declarations QUEUE* createQueue(void); QUEUE* destroyQueue(QUEUE* queue); bool dequeue(QUEUE* queue, void** itemPtr); bool enqueue(QUEUE* queue, void* itemPtr); bool queueFront(QUEUE* queue, void** itemPtr); bool queueRear(QUEUE* queue, void** itemPtr); int queueCount(QUEUE* queue); bool emptyQueue(QUEUE* queue); bool fullQueue(QUEUE* queue); /*================= createQueue ================ Allocates memory for a queue head node from dynamic memory and returns...
HW_6d - Write a struct object to a binary file. Create a new C++ project and...
HW_6d - Write a struct object to a binary file. Create a new C++ project and name it as:   Cats Create a Source.cpp file. Declare a struct named Cat. (in the Source.cpp file, or in a separate header file) Each Cat object has a name and age. The name data member is a c_string. The age data member is an integer. Ask the user to enter 3 cats. Use a while loop to read the information about one cat entered...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of...
Name your c++ file Word_LastNameFirstName.cpp. Create a struct that has a word and the length of the word. Next, use the struct to store the word read from a text file call “word.txt” and the length of the word. Print out the word x number of times based on the length of the word as shown below. Also, print a statement with the length and determine if the string length has an odd number or even number of characters. You...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT