Question: Hi, please help, please. I have finished the code but it does not run very well. The function of this program should be: This program
Hi, please help, please. I have finished the code but it does not run very well. The function of this program should be:
This program responds to commands the user enters to manipulate an ordered list of integers, which is initially empty. In the following commands, k1 is a position in the list, and v is an integer. e -- Re-initialize the list to be empty. i v -- Insert the value v into the list. r v -- Remove the value v from the list. m -- Is the list empty? l -- Report the length of the list. p v -- Is the value v present in the list? k k1 -- Report the k1th value in the list. w -- Write out the list. h -- See this menu. q -- Quit.
Example run
--> i 27 --> i 42 --> i 15 --> i 33 --> i 14 --> m The list is NOT empty. --> w List: --> r 33 --> w List: --> p 22 The value 22 is NOT present in the list. --> p 42 The value 42 is present in the list. --> k 3 The 3th element of the list is 27. --> k 9 The list does not contain 9 values. --> q
CODE:
#include #include
using namespace std; struct node { int data; node* next;
};
void initialize(node*p); void insert(int data,int n); void remove(struct node **head_ref, int key); bool empty(node*); int length(node*head); bool valuein(node*head,int x); int reportvalue(int value); void printlist(struct node* temp); int menu();
int main() {
node *head = NULL; char commands=' '; int value; int pos = 0;
cout
while (commands != 'q') { cout ";
cin >> commands;
switch (commands) { case 'e': { initialize(head->next); } case 'i': {cin >> value; insert(value, pos); pos++; } case 'm': { empty(head); if (empty(head) == true) { cout
} case'p': { cin >> value; if (valuein(head, value) == true) { cout
} case 'r': {cin >> value; remove(&head, value); } case'w': { printlist(head);
} case'h': {cout
}
} } system("pause"); return 0; } bool empty(node*) { node *root=NULL; if (root->next == root) return true; else return false; }
void initialize(node*p) { node *temp; if (p == NULL){ return; } temp = p; initialize(p->next); delete temp; } void insert(int data, int pos) { node*head = new node; node* prev = new node(); node* curr = new node(); node* newNode = new node(); newNode->data = data;
int tempPos = 0; // Traverses through the list
curr = head; // Initialize current to head; if (head != NULL) { while (curr->next != NULL && tempPos != pos) { prev = curr; curr = curr->next; tempPos++; }
} } bool valuein(node*head, int x) { bool judge=false; struct node* current = head; // Initialize current while (current != NULL) { if (current->data == x) judge=true; current = current->next; } return judge; } void remove(struct node **head_ref, int key) {
struct node* temp = *head_ref, *prev; prev = NULL;
if (temp != NULL && temp->data == key) { *head_ref = temp->next; free(temp); return; }
while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; }
if (temp == NULL) return;
prev->next = temp->next;
free(temp); } void printlist(node* temp) { if (empty(temp) == false) { cout next data next; } } } int length(struct node*head) { int count = 0; // Initialize count struct node* current = head; // Initialize current while (current != NULL) { count++; current = current->next; } return count; }
Also, I cannot find a way to achieve
k k1 -- Report the k1th value in the list.
can you also help with this ? Thank you!!
C:WINDOWSlsystem321cmd.exe This program responds to commands the user enters to manipulate an ordered list of integers, which is initially empty. In the following commands, kl is a position in the list, and v is an integer e Re-initialize the list to be empty -- Insert the value v into the list v -_ Remove the value v from the list mIs the list empty ? 1Report the 1ength of the list. v Is the value v present in the list? k1 Report the klth value in the list. w - Write out the list. See this menu. q-Quit. i 20 Press any key to continue
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
