Question

In: Computer Science

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 pop_Stack (struct linked_list* list, int number) //*This is the function to make and below is the explanation that should be written in given code.

This function removes some nodes in stack manner; the tail of the list will be removed, repeatedly. The parameter (variable number_of_nodes) means the number of nodes that will be removed. When parameter is bigger than 1, popping a node with n times, you do not remove node at one go. If there is only one node in the list, please make sure it frees (de-allocates) both the node and the list. If the list is not a stack type, print the error message “Function pop_Stack: The list type is not a stack” and exit the function. If the number_of_nodes parameter is less than 1 or more than the number of nodes in the stack, respectively print the error message “Function popStack: The number of nodes which will be removed is more than 1” and “Function popStack: The number of nodes which will be removed is more than that in the stack”, then exit the function. The removed nodes should be freed.

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 touch this function.

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

(Below code is about linked_list.h)

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


struct linked_node{
   int value;
   struct linked_node* next;
   struct linked_node* prev;
};

struct linked_list{
   int type_of_list; // normal = 0, stack = 1
   struct linked_node* head;
   struct linked_node* tail;
   int number_of_nodes;
};

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

#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 pop_Stack(struct linked_list* list, int number)//The function to be written!!
{
~~~~~~~~~~~~~ //your code starts from here
}


Solutions

Expert Solution

Summary : The function pop_Stack is implemented which you can see at the end. It will pop the stack when the list type is stack. The code in the problem is also pasted for getting a working code. Comments added in the function implemented pop_Stack for reference,

CPP program

#include <iostream>

using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "string.h"

struct linked_node{
   int value;
   struct linked_node* next;
   struct linked_node* prev;
};

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;
}
struct linked_list{
   int type_of_list; // normal = 0, stack = 1
   struct linked_node* head;
   struct linked_node* tail;
   int number_of_nodes;
};


int list_exist;
void push_Stack(struct linked_list *list,struct linked_node *node){

struct linked_list *temp=list;

if(list->tail == 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->prev = list->tail;
       list->tail->next = node;
       list->tail = node;
   }
   else if(list->head != list->tail)
   {
       node->prev = list->tail;
       list->tail->next = node;
       list->tail = node;
   }

list->number_of_nodes = (list->number_of_nodes )+1;

}
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)++;
}

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;
}

void pop_Stack(struct linked_list *list, int number)//The function to be written!!
{
    if (number != 1)
    { 
        printf("\nFunction pop_Stack: The list type is not a stack\n");
        return;
    }
    // create a temporary node pointer
    struct linked_node *temp;

    while(list->tail){
        // temp has the node to be poped
        temp=list->tail;
        printf("Value of the node deleted: %d\n", temp->value);
        // link the list properly
        list->tail=list->tail->prev;
        // Free the memory of the poped element
        free( temp);
        list->number_of_nodes--;
    }
    // Printing the validation error checking done
    if (list->number_of_nodes > 0)
        printf("Function popStack: The number of nodes which will be removed is more than 1");
    else if (list->number_of_nodes< 0)
        printf("Function popStack: The number of nodes which will be removed is more than that in the stack");
    
}
int main()
{
    // This is the driver program for testing the code, we create a sample list
    struct linked_list* linked_list_new = create_list(5,1);
    printf("Number of nodes created %d\n", linked_list_new->number_of_nodes);
    // Pop not from stack type
    pop_Stack(linked_list_new,0);
    // Pop from stack
    pop_Stack(linked_list_new,1);
    return 0;
}

Sample Output


Related Solutions

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 remove_node(struct linked_list* list, int rm_node_value) (the function to make) This function removes a node with specified value. If there is only one node in the list, remove the node and remove the list also since there is nothing left. While removing a node, the node should be perfectly freed. If the type of list is...
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 pop_Stack (struct linked_list* list, int number) //*This is the function to make and below is the explanation that should be written in given code. This function removes some nodes in stack manner; the tail of the list will be removed, repeatedly. The parameter (variable 'number') means the number of nodes that will be removed. When...
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 remove_list(struct linked_list* list) //*This is the function to make and below is the explanation that should be written in given code. "This function removes the list. When the list is removed, all the memory should be released to operating system (OS) so that OS lets other computer programs use this. While deleting the list, every...
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 push_Stack (struct linked_list* list, struct linked_node* node) //*This is the function to make and below is the explanation that should be written in given code. This function inserts a node in stack manner. If the type of list is not stack, print the error message “Function push_Stack: The list type is not a stack” The...
This is my C language code. I have some problems with the linked list. I can't...
This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL. #include #include #include #include struct node{int data; struct node *next;}; int main() {     struct node *head, *current, *temp, *trash;     srand(time(0));     int randNumber = rand()%51;     if(randNumber != 49)     {         temp = (struct node*)malloc(sizeof(struct node));         current = (struct node*)malloc(sizeof(struct node));...
For my assignment, I'm not sure how to start making a stack implemented via linked list...
For my assignment, I'm not sure how to start making a stack implemented via linked list of arrays. I'm stuck on how to approach the very first steps (Stack class members and constructor.) I'm familiar with allocating memory, arrays, and linked lists, but putting them together has me lost. How should I create a Stack with a dynamic linked list of arrays? Stack name (number of elements); Creates a stack of strings with name “name.” Internally, each unit of memory...
C++ Need to add the following functions to my templatized class linked list. (main is already...
C++ Need to add the following functions to my templatized class linked list. (main is already set to accommodate the functions below) --void destroy_list () deletes each node in the list, and resets the header to nullptr --bool search_list (key value) searches the list for a node with the given key. Returns true if found, false if not. --bool delete_node (key value) deletes the node which contains the given key. If there is more than one node with the same...
using C++. edit this code down below so that it will implement stack with linked list...
using C++. edit this code down below so that it will implement stack with linked list contains a default constructor, a copy constructor, and a destructor. #include <iostream> #include <vector> #include <string> #include <stack> #include <limits> using namespace std; class Stack { public: bool isEmpty(); int top(); int pop(); void push(int); void printList(); private: vector<int> elements; }; bool Stack::isEmpty() { return elements.empty(); } int Stack::top() { if(isEmpty()) { throw runtime_error("error: stack is empty"); } return elements.back(); } int Stack::pop() {...
# List at least three functions in the linked list toolbox, including function names and their...
# List at least three functions in the linked list toolbox, including function names and their functionality. # Assuming we have already implemented all functions in the linked list toolbox and we have a pointer head_ptr pointing to the front of a linked list. Write a few lines to insert the number 42 and make it the second item in the list. Note: if the list was originally empty, then it should be the first in the list.
Need this in C#. Below is my code for Problem 3 of Assignment 2. Just have...
Need this in C#. Below is my code for Problem 3 of Assignment 2. Just have to add the below requirement of calculating the expected winning probability of VCU. Revisit the program you developed for Problem 3 of Assignment 2. Now your program must calculate the expected winning probability of VCU through simulation. Run the simulation 10,000 times (i.e., play the games 10,000 times) and count the number of wins by VCU. And then, calculate the winning probability by using...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT