Question

this is part of my code im not sure how to do last 2 function. i...

this is part of my code im not sure how to do last 2 function. i think above last 2 functions need for making function please help me how to do it.

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


typedef struct node {
ElemType val;
struct node *next;
} NODE;


struct list_struct {
NODE *front;
NODE *back;
};

int lst_remove_first(LIST *l, ElemType x) {

NODE *p;
NODE *tmp;

if(l->front == NULL) return 0;
if(l->front->val == x) {
   lst_pop_front(l);
   return 1;
}
// lst non-empty; no match on 1st elem
p = l->front;

while(p->next != NULL) {
if(x == p->next->val) {
   tmp = p->next;
   p->next = tmp->next;
   if(tmp == l->back)
   l->back = p;
   free(tmp);
   return 1;
}
p = p->next;
}
return 0;
}


int lst_remove_all_slow(LIST *l, ElemType x) {
int n=0;
while(lst_remove_first(l, x))
   n++;
return n;
}

/**
* function: lst_sra_bad_case (sra: slow_remove_all)
*
* description: constructs a list of length n such that
* the above function takes quadratic time to remove
* all occurrences of a specified value.
*
* By convention, the specified value will be 0
*/
LIST *lst_sra_bad_case(int n) {
LIST *lst;
int i;

   // idea: first n/2 elements are non-zero
   // the last n/2 are zeros
   // ever call to lst_remove_first will
   // have to walk through all of the 1's
   // [ 1 1 1 1 1 1 1 0 0 0 0 0 0 0 ]

   lst = lst_create();
   for(i=0; i<n/2; i++)
       lst_push_front(lst, 0);
   for(i=0; i<(n-n/2); i++)
       lst_push_front(lst, 1);
   return lst;
}

/** TODO

* function: lst_remove_all_fast
* description: same behavior as lst_remove_all_slow but has
*    faster execution time even on "bad cases" as generated by
*    the function above. Number of operations proportional to the
*    length of the list regardless of number of matches and distribution
*    of matches.
*
* Approach: all occurrences of x removed in a single pass
*
* returns: number of elements removed
*/
int lst_remove_all_fast(LIST *l, ElemType x) {
return 0;
}

// TODO
int lst_is_sorted(LIST *l){

return 0;

}

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

Hopefully, this code should work. If not, please provide the header file list.h, so that we can debug the code, and solve the problems for you.

/** TODO
* function: lst_remove_all_fast
* description: same behavior as lst_remove_all_slow but has
* faster execution time even on "bad cases" as generated by
* the function above. Number of operations proportional to the
* length of the list regardless of number of matches and distribution
* of matches.
*
* Approach: all occurrences of x removed in a single pass
*
* returns: number of elements removed
*/
int lst_remove_all_fast(LIST *l, ElemType x) {
NODE *p;
NODE *tmp;
int n = 0;
if(l->front == NULL) return n;
if(l->front->val == x) {
lst_pop_front(l);
n++;
}
// lst non-empty; no match on 1st elem
p = l->front;
while(p->next != NULL) {
if(x == p->next->val) {
tmp = p->next;
p->next = tmp->next;
if(tmp == l->back)
l->back = p;
free(tmp);
n++;
}
p = p->next;
}
return n;
}
/** TODO
* function: lst_is_sorted
*
* description: returns 1 if the list is sorted in
* non-decreasing order; 0 otherwise
*/
int lst_is_sorted(LIST *l){
NODE *p;
NODE *tmp;
if(l->front == NULL) return 1;
  
// lst non-empty;
p = l->front;
while(p->next != NULL) {
x = p->val;
if(x > p->next->val) {
  
return 0;
}
p = p->next;
}
return 1;
}

Add a comment
Know the answer?
Add Answer to:
this is part of my code im not sure how to do last 2 function. i...
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
  • 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 {   ...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • I don't know how to do the clear function, which mean delete the nodes between n1 and n2 # include<ios-ream>...

    I don't know how to do the clear function, which mean delete the nodes between n1 and n2 # include<ios-ream> using namespace sta; General instructions: Please modify this file and submit the modified one via svn and then websubmission (under the entry "pracExam"). No design documents are needed. struct Node int data; Node* next; /Task 1: Implement the following function for adding a front of the given list. new node to the input Node head: a head pointer to a...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function...

    Follow the TODOs and complete the insertItem(), searchItem() and printTable() functions in hash.cpp. The hash function has already been implemented for you. // hash.CPP program to implement hashing with chaining #include<iostream> #include "hash.hpp" using namespace std; node* HashTable::createNode(int key, node* next) { node* nw = new node; nw->key = key; nw->next = next; return nw; } HashTable::HashTable(int bsize) { this->tableSize= bsize; table = new node*[tableSize]; for(int i=0;i<bsize;i++) table[i] = nullptr; } //function to calculate hash function unsigned int HashTable::hashFunction(int key)...

  • Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how...

    Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...

  • 14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order....

    14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order. Consider running the following line of code: p = question4(p); where question4 is the function defined below. Show the contents of p after the function call. struct node* question4(struct node *list) { struct node* a = list; struct node* b = list; struct node* c; if (a == NULL) return NULL; while ( a->next != NULL) a = a ->next; a->next = b; c...

  • 14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order....

    14.   p contains the elements 66, 9, 14, 52, 87, 14 and 17, in that order. Consider running the following line of code: p = question4(p); where question4 is the function defined below. Show the contents of p after the function call. struct node* question4(struct node *list) { struct node* a = list; struct node* b = list; struct node* c; if (a == NULL) return NULL; while ( a->next != NULL) a = a ->next; a->next = b; c...

  • program in C - Starter code below //In this assignment, we practice call by reference. //Below...

    program in C - Starter code below //In this assignment, we practice call by reference. //Below description of call by reference is from the following link //https://www.tutorialspoint.com/cprogramming/c_function_call_by_reference.htm //The call by reference method of passing arguments to a function copies //the address of an argument into the formal parameter. Inside the function, //the address is used to access the actual argument used in the call. //It means the changes made to the parameter affect the passed argument. //We use an example...

  • C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses...

    C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses the linked list and prints the reverse text to the standard output, without changing the linked list. ( Pass the linked list by value, you have the freedom to create a doubly linked list that is a copy of the original list in your program before you call the function) #include "pch.h" #include <iostream> #include <string.h> #include <string> using namespace std; #define MAX 512...

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