Question

You are given four files: linked-list.h linked-list. c find-ї ast-node.c , and a , , Makefile . The program is missing a defi

****find_last_node.c

#include <stdio.h>
#include "linked_list.h"


int main(){
  
   struct node *linked_list = NULL;

   linked_list = add_to_list(linked_list, 5, 'a');
   linked_list = add_to_list(linked_list, 10, 'b');
   linked_list = add_to_list(linked_list, 4, 'c');
   linked_list = add_to_list(linked_list, 10, 'd');
   linked_list = add_to_list(linked_list, 5, 'e');
   linked_list = add_to_list(linked_list, 7, 'f');
   linked_list = add_to_list(linked_list, 5, 'g');
   linked_list = add_to_list(linked_list, 3, 'h');


   int search_number;
   printf("Enter number you want to search for:");
   scanf("%d", &search_number);

   struct node *last_node = find_last(linked_list, search_number);
   if (last_node != NULL)
   {
       printf("Node found: value = %d and marker = %c\n", last_node->value, last_node->marker);
   }else{
       printf("Number not found!\n");
   }
  
   //running your program using valgrind will show you memory leaks. You need to make sure
//you free any allocated memory before you exit the program

****linked_list.c

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

struct node *add_to_list(struct node *list, int n, char marker){
   struct node *new_node;
   new_node = malloc(sizeof(struct node));

   if(new_node == NULL){
       printf("Error: malloc failed in add_to_list\n");
       exit(EXIT_FAILURE);
   }

   new_node->value = n;
   new_node->marker = marker;
   new_node->next = list;

   return new_node;
}

****linked_list.h

#ifndef __LINKED_LIST__
#define __LINKED_LIST__

struct node{
int value;
char marker;
struct node *next;
};


struct node *add_to_list(struct node *list, int n, char marker);
struct node *find_last(struct node *list, int n);
#endif

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

(Note: I added code to linked_list.c.The add to list function adds element at head.So I wrote find_last function which parses the linked list till the last element and returns last occurrence of value n. If there is no n value in linked list returns NULL.

In find_last_node.c add linked_list.c in header section. I added it so please copy my whole code and run it it will execute perfectly.

If you need any changes please comment I will modify the code.

If the program works fine Please up vote. Thank you.

linked_list.h:

#ifndef __LINKED_LIST__
#define __LINKED_LIST__

struct node{
int value;
char marker;
struct node *next;
};


struct node *add_to_list(struct node *list, int n, char marker);
struct node *find_last(struct node *list, int n);
#endif

linked_list.c:

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

struct node *add_to_list(struct node *list, int n, char marker){
   struct node *new_node;
   new_node = malloc(sizeof(struct node));

   if(new_node == NULL){
       printf("Error: malloc failed in add_to_list\n");
       //exit(EXIT_FAILURE);
   }
   new_node->value = n;
   new_node->marker = marker;
   new_node->next = list;
   return new_node;
}
struct node *find_last(struct node *list, int n){
   struct node *new_node;  
   while (list != NULL)
{
      if(list->value==n)
      {
      new_node = list;//new_node stores the last occurence of n value.
      }
     list = list->next;
}
   return new_node;
}

find_last_node.c:

#include<stdio.h>
#include<stdlib.h>
#include"linked_list.h"
#include"linked_list.c"

int main(){

   struct node *linked_list = NULL;

   linked_list = add_to_list(linked_list, 5, 'a');
   linked_list = add_to_list(linked_list, 10, 'b');
   linked_list = add_to_list(linked_list, 4, 'c');
   linked_list = add_to_list(linked_list, 10, 'd');
   linked_list = add_to_list(linked_list, 5, 'e');
   linked_list = add_to_list(linked_list, 7, 'f');
   linked_list = add_to_list(linked_list, 5, 'g');
   linked_list = add_to_list(linked_list, 3, 'h');
   int search_number;
   printf("Enter number you want to search for:");
   scanf("%d", &search_number);

   struct node *last_node = find_last(linked_list, search_number);
   if (last_node != NULL)
   {
       printf("Node found: value = %d and marker = %c\n", last_node->value, last_node->marker);
   }else{
       printf("Number not found!\n");
   }
}

Output:

File Edit View Search Termina Help shyam@shyam:~/Docunentss gcc find_last_node.c shyan@shyam: /Documents ./a.out Enter number

Add a comment
Know the answer?
Add Answer to:
****find_last_node.c #include <stdio.h> #include "linked_list.h" int main(){    ...
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
  • 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...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • Write a function to insert a name at the end of a linked list? (C) I...

    Write a function to insert a name at the end of a linked list? (C) I have a linked list: struct node { char person[100]; struct node *next; }; int main() { struct node *new_node; new_node=malloc(sizeof(struct node)); printf("Please enter a name:\n"); scanf("%s", ); } How do I get the name from the user and write a function to add it to the end of the linked list? I'm not supposed to use the insert() library function, which is why I'm...

  • 1. static int function(int n){ return n * function(n + 1); } If you call the...

    1. static int function(int n){ return n * function(n + 1); } If you call the method function(2), the expected output is 3. True False 2. public void push(int new_data) { Node new_Node = new Node(new_data); new_Node.next = head; new_Node.prev = null; if (head != null) head.prev = new_Node; head = new_Node; } The code snippet depicts stack implementation True False 3. A ________ list is a linked data structure that consists of a set of sequentially linked records called...

  • ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE...

    ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE ANSWERING CODING SECTIONS HW07 #Q1-Q5 HW08 #Q1-Q2 // READ BEFORE YOU START: // Please read the given Word document for the project description with an illustrartive diagram. // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, standard, and a linked list of absents. // Please read the instructions...

  • this is i have code for double linked list with insert at sorted list. i have...

    this is i have code for double linked list with insert at sorted list. i have some error that stdout: ------- Error: compilation stderr: ------- InsertDouble.c: In function ‘list* InsertDouble(LIST, int)’: InsertDouble.c:51:14: error: cannot convert ‘list’ to ‘list*’ in assignment Currentptr = *head; // set a pointer which is current one ^ it keep give me this error i am not sure how to fix is anyone possible to help me? #include <stdio.h> #include <stdlib.h> typedef struct list {   ...

  • Enum {FALSE=0, TRUE}; #define MAXBUFF 1024 #define SMALLBUFF 10 /* The LinkNode type is used as a...

    enum {FALSE=0, TRUE}; #define MAXBUFF 1024 #define SMALLBUFF 10 /* The LinkNode type is used as an element of a linked list ** implementation of a stack of tree nodes. */ typedef struct link_t LinkNode; typedef LinkNode *LinkNodePtr; /* The TreeNode type is used as an element of a binary "parse" tree. ** Tree nodes are created while parsing the RPN expression and ** stored on a stack until it's time to place them. */ typedef struct tree_t TreeNode; typedef...

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

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

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