Question

It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions...

It is about C++linked list code. my assignment is making 1 function, in below circumstance,(some functions are suggested for easier procedure of making function.)

void search_node(struct linked_list* list, int find_node_ value) (The function to make)

This function finds the node from the list that value is same with find_node_value and count the order of the node. This function should print message “The order of (node_value) is (order).” and error message “Function search_node : There is no such node to search.”.

<<This is the function and its explanation that should be written in given code.

Given code is written below,(There is a function to fill in last moment in this code)

linked_list.h: This is the header file of linkLQS.c that declares all the functions and values that are going to be used in linkLQS.c. You do not have to add any more functions or variables in order to complete the project and you are NOT ALLOWED to.

string.h : This header file defines several functions to manipulate C strings and arrays.

-----------------------------------------------------------------------

#include "linked_list.h"
#include "string.h"
extern int list_exist;

struct linked_list* create_list (int number_of_nodes, int list_type)
{
   int a[number_of_nodes];
   int i, j;
   int bFound;

   if (number_of_nodes < 1)
   {
       printf("Function create_list: the number of nodes is not specified correctly\n");
       return NULL;
   }
   if(list_exist == 1)
   {
       printf("Function create_list: a list already exists\nRestart a Program\n");
       exit(0);  
   }
   if(list_type != 0 && list_type != 1)
   {
       printf("Function create_list: the list type is wrong\n");
       exit(0);  
   }
   struct linked_list * new_list = (struct linked_list*)malloc(sizeof(struct linked_list));
   new_list->head = NULL;
   new_list->tail = NULL;
   new_list->number_of_nodes = 0;
   new_list->type_of_list = list_type;

   //now put nodes into the list with random numbers.
   srand((unsigned int)time(NULL));
   if(list_type == 0)
   {
       for ( i = 0; i < number_of_nodes; ++i )
       {
           while ( 1 )
           {
  
               a[i] = rand() % number_of_nodes + 1;
               bFound = 0;
               for ( j = 0; j < i; ++j )
               {
                   if ( a[j] == a[i] )
                   {
                       bFound = 1;
                       break;
                   }
               }
               if ( !bFound )
                   break;
           }
           struct linked_node* new_node = create_node(a[i]);
           insert_node(new_list, new_node);
       }
   }
   else if(list_type == 1)
   {
       for ( i = 0; i < number_of_nodes; ++i )
       {
           while ( 1 )
           {
  
               a[i] = rand() % number_of_nodes + 1;
               bFound = 0;
               for ( j = 0; j < i; ++j )
               {
                   if ( a[j] == a[i] )
                   {
                       bFound = 1;
                       break;
                   }
               }
               if ( !bFound )
                   break;
           }
           struct linked_node* new_node = create_node(a[i]);
           push_Stack(new_list, new_node);
       }
   }
   list_exist = 1;
   printf("List is created!\n");
   return new_list;
}

struct linked_node* create_node (int node_value)//This functon is the example for reference of the assignment function
{
   struct linked_node* node = (struct linked_node*)malloc(sizeof(struct linked_node));
   node->value = node_value;
   node->next = NULL;
   node->prev = NULL;
   return node;
}

void insert_node(struct linked_list* list, struct linked_node* node)//This functon is the example for reference of the assignment function
{
   node->next = NULL;
   node->prev = NULL;

   if(list->head == NULL)       //if head is NULL, tail is also NULL.
   {
       list->head = node;
       list->tail = node;
       list_exist = 1;
   }
   else if(list->head == list->tail)
   {
       node->next = list->head;
       list->head->prev = node;
       list->head = node;
   }
   else if(list->head != list->tail)
   {
       node->next = list->head;
       list->head->prev = node;
       list->head = node;
   }
   (list->number_of_nodes)++;
}

void search_node(struct linked_list* list, int find_node_value)//the function to be written!!
{
   struct linked_node * temp_Node = ~~~ //your code starts from here(please do not erase it!! you should cover this sentence.
}


}

Homework Answers

Answer #1

Method that ned to be implememted as follows:

void search_node(struct linked_list *list,int find_node_value)
{
   struct linked_node *temp_Node=list->head;
   int order=0;
   while(temp_Node)
   {
       if(temp_Node->value==find_node_value)
       {
           cout << "The order of " << find_node_value " is " << order << "." << endl;
           return;
       }
       order++;
       temp_Node=temp_Node->next;
   }
   cout << "Function search_node : There is no such node to search." << endl;
}

Mention in comments if any mistakes or errors are found. Thank you.

Know the answer?
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
Similar Questions
For the following code in C, I want a function that can find "america" from the...
For the following code in C, I want a function that can find "america" from the char array, and print "america is on the list" else "america is not on the list" (Is case sensitive). I also want a function to free the memory at the end of the program. #include <stdio.h> #include <stdlib.h> struct Node { void *data; struct Node *next; }; struct List { struct Node *head; }; static inline void initialize(struct List *list) { list->head = 0;...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is written TO DO List! #include<iostream> using namespace std; template<typename T> class DoubleList{​​​​​     class Node{​​​​​     public: T value; Node* next; Node* prev;         Node(T value = T(), Node* next = nullptr, Node* prev = nullptr){​​​​​             this->value = value;             this->next = next;             this->next = next;         }​​​​​     }​​​​​;     int size;     Node* head;     Node* tail; public:     DoubleList(){​​​​​         size = 0;         head = nullptr;     }​​​​​     int length(){​​​​​         return size;     }​​​​​...
SELECT ALL OPTIONS BELOW THAT ARE TRUE FOR THE GIVEN CODE #include <string.h> #include <stdlib.h> #include...
SELECT ALL OPTIONS BELOW THAT ARE TRUE FOR THE GIVEN CODE #include <string.h> #include <stdlib.h> #include <stdio.h> #define MAX_STR_LEN 100 struct Node { char *data; struct Node *left; struct Node *right; }; struct Node *new_node(char *d) { struct Node *r = malloc(sizeof(struct Node)); r->data = malloc(sizeof(char) * MAX_STR_LEN); strncpy(r->data, d, MAX_STR_LEN); free(d); r->left = NULL; r->right = NULL; return r; } void freeSubTree(struct Node *r) { free(r->data); if (r->left != NULL) freeSubTree(r->left); if (r->right != NULL) freeSubTree(r->right); free(r); } void...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels (integer values) that broadcast the show. For this problem you can ONLY use the following string library functions: strcpy, strlen, strcmp. You MAY not use memcpy, memset, memmove. You can assume memory allocations are successful (you do not need to check values returned by malloc nor calloc). typedef struct tv_show { char *name; int num_channels, *channels; } Tv_show; a. Implement the init_tv_show function that...
Using the C programming language implement Heapsort in the manner described in class. Here is some...
Using the C programming language implement Heapsort in the manner described in class. Here is some example code to use as a guideline. Remember, you need only implement the sort algorithm, both the comparison and main functions have been provided. /* * * after splitting this file into the five source files: * * srt.h, main.c, srtbubb.c, srtinsr.c, srtmerg.c * * compile using the command: * * gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR |...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if you try to add to a position greater than the number of nodes you get an error warning. /INSERT.C /*THIS PROGRAM READS A BINARY FILE (MUST ALREADY EXIST AND BE FILLED) */ /*AND PUTS IT INTO A LINKED LIST AND PRINTS THE LIST TO THE SCREEN) */ #include #include #include #include typedef struct ENTRY { char name[81]; }ENTRY; typedef struct LISTREC /* LISTREC is...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...