Question

All the functions are written and work besides removeLinkedList and contains LinkedList. Please fill those out. 무#ifndef #define LINKEDLIST-H LINKEDLISTH define TYPE /*# define TYPE SIZE sizeof (TYPE) */ double typedef struct ListStack L# include <stdlib. h> param: 1 the linked List pre: 1 is not null post: the head of the linked list is initialised to null, i/ Stack Interface/ pushLinkedList param: 1 the linked list param: val the value to be pushed pre: l is not nul] post: 1 is novoid popLinkedList (LinkedList *l) struct Link *link l->head ; assert (!isEmptyLinkedList (l)) 1-)head link->next ; free (lin

무#ifndef #define LINKEDLIST-H LINKEDLISTH define TYPE /*# define TYPE SIZE sizeof (TYPE) */ double typedef struct ListStack LinkedList; void initLinkedLǐst(LinkedList *1); void freeLinkedList (LinkedList *1) int İsEmptyLinkedList (LinkedList*1); void pushLinkedList (LinkedList #1, TYPE val); TYPE topLinkedList (LinkedList 1) void popLinkedList (LinkedList *1); /* Bag / void addLinkedList (LinkedList *1, TYPE val) ; int containsLinkedList (LinkedList *1, TYPE val) ; void removeLinkedList (LinkedList l, TYPE val) L#endif
# include param: 1 the linked List pre: 1 is not null post: the head of the linked list is initialised to null, isEmpty returns true void initLinkedList (LinkedList 1) 1-> head = MULL; param: 1 the linked list pre: 1 is not null 《1-head HULL) ; teturn param: 1 the linked list pre: 1 is not null post: siseLinkedList returns true /1 Note: Free gets rid of 11 links but keep5 the head of the list around, so the list itself still exist5 and 15 initi lined. while 《'isEmptyLinkedList(1)) popLinkedList(l): Stac Interface param: 1 the linked list param: val the value to be pushed pre: 1 is not null post: 1 is not empty, 1 sie has increased by one void pushLinkedList (LinkedList *1, TY?E val) I struct Link *lin(struct Link *)malloc (sizeof (struct Lint)) 교5sert(Link !=NULL);
/ Stack Interface/ pushLinkedList param: 1 the linked list param: val the value to be pushed pre: l is not nul] post: 1 is not empty, l size has increased by one void pushLinkedList (LinkedList l, TYPE val) i struct Link *link (struct assert (link != NULL); Link *)malloc (sizeof (struct Link)); link->next -1->head; link->val -val 1->head1ink topLinkedList params: 1 the linked list pre: l is not null pre: 1 is not empty post: none TYPE topLinkedList (LinkedList *l) assert (isEmptyLinkedList (1)) return l->head->val popLinkedList param: 1 the linked list pre: l is not nul] pre: 1 is not empty post: 1 size has decremented by one void popLinkedList (LinkedList struct Link link-1->head;
void popLinkedList (LinkedList *l) struct Link *link l->head ; assert (!isEmptyLinkedList (l)) 1-)head link->next ; free (link) Bag */ void addLinkedList (LinkedList *l, TYPE val) pushLinkedlist (l,val) int containsLinkedList (LinkedList *l, TYPE val) /*write this function * void _removeLink (struct Link *prev, struct Link *cur) prev->nextcur-next: free (cur) void removeLinkedList (LinkedList *l, TYPE val) struct Link *cur: struct Link *prev: assert (containsLinkedList (l, val)) /special case for head/ if (1->head-alval) cur 1-head 1-2head 1->head->next ; free (cur) else /* write this part of the function
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// function to check if val is in linked list l
int containsLinkedList(LinkedList *l, TYPE val)
{
   if(!isEmptyLinkedList(l)) // if list is empty
   {
       struct Link *list = l->head; // let list point to head of linkedlist
       while(list != NULL) // loop over the list
       {
           if(list->val == val) //check if val is found
               return 1; // return true
           list = list->next;   // set list to next node
       }
  
   }

   return 0; //val not found
}

//end of containsLinkedList

// function to remove a value from linked list

void removeLinkedList(LinkedList *l, TYPE val)
{
   struct Link *curr ;
   struct Link *prev;
  
   assert(containsLinkedList(l),val);
  
   /*special case for head*/
   if(l->head->val == val)
   {
       curr = l->head;
       l->head = l->head->next;
       free(curr);
   }else
   {
       curr = l->head->next; // set curr to node next to head
       prev = l->head; // set prev to head
      
       // loop over the linked list
       while(curr != NULL)
       {
           if(curr->val == val) // if val is found
           {
               prev->next = curr->next; // set the next pointer to prev to point to node next to curr
               free(curr); // delete curr
               break; // break out of loop
           }
          
           prev = curr; // set prev to curr
           curr = curr->next; // set curr to node next to curr
       }
   }
}

//end of removeLinkedList

Add a comment
Know the answer?
Add Answer to:
All the functions are written and work besides removeLinkedList and contains LinkedList. Please fill those out. 무#ifndef #define LINKEDLIST-H LINKEDLISTH define TYPE /*# define TYPE SIZE sizeof (TYP...
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
  • Header file #ifndef DYNAMIC_ARRAY_INCLUDED #define DYNAMIC_ARRAY_INCLUDED 1 #ifndef __TYPE #define __TYPE # define TYPE int #...

    Header file #ifndef DYNAMIC_ARRAY_INCLUDED #define DYNAMIC_ARRAY_INCLUDED 1 #ifndef __TYPE #define __TYPE # define TYPE int # define TYPE_SIZE sizeof(int) # endif # ifndef LT # define LT(A, B) ((A) < (B)) # endif # ifndef EQ # define EQ(A, B) ((A) == (B)) # endif typedef struct DynArr DynArr; /* Dynamic Array Functions */ void initDynArr(DynArr *v, int capacity); DynArr *newDynArr(int cap); void freeDynArr(DynArr *v); void deleteDynArr(DynArr *v); int sizeDynArr(DynArr *v); void addDynArr(DynArr *v, TYPE val); TYPE getDynArr(DynArr *v, int...

  • You are now going to create a LinkedList class, that will work very similarly to the...

    You are now going to create a LinkedList class, that will work very similarly to the Stack class seen in the book (and used in the previous exercise). Then write new methods as follows: add ( LinkedList::Link* l, int n ): will insert in the linked list, after link l, a chain of n new links. Each link will store an integer that will be set to receive, in order, a value starting from 0 until n-1. In the last...

  • Use the header and other files listed below for problems 1 and 2. Each problem has...

    Use the header and other files listed below for problems 1 and 2. Each problem has 3 files that correspond to the problem. Problem 1, linked list deque and bag implementations. First, complete the linked list implementation of the deque and bag ADTs in C language. To do this, implement all functions with the // FIXME... comments in linkedList.c (File included below). These functions listed are the ones that need to be fixed. init addLinkBefore removeLink linkedListAddFront linkedListAddBack linkedListFront linkedListBack...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • 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 PROGRAMMING #include <stdio.h> #include <stdlib.h> struct nodet { int data; struct nodet *link; }; struct...

    C PROGRAMMING #include <stdio.h> #include <stdlib.h> struct nodet { int data; struct nodet *link; }; struct nodet *makeAnode(int val) { struct nodet *box; box = malloc(sizeof(struct nodet) ); box->data = val; box->link = NULL; return box; } void printList(struct nodet *L) { struct nodet = *mov; mov = L; while(mov != NULL) { printf("%d ", mov->data); mov = mov->link; } printf("\n"); } // THIS SHOULD COUNT HOW MANY ITEMS (NODES) ARE IN THE LIST. int listLen(struct nodet **L) { int...

  • This assignment is comprised of 3 parts: ​All files needed are located at the end of...

    This assignment is comprised of 3 parts: ​All files needed are located at the end of the directions. Part 1: Implementation of Dynamic Array, Stack, and Bag First, complete the Worksheets 14 (Dynamic Array), 15 (Dynamic Array Amortized Execution Time Analysis), 16 (Dynamic Array Stack), and 21 (Dynamic Array Bag). These worksheets will get you started on the implementations, but you will NOT turn them in. ​Do Not Worry about these, they are completed. Next, complete the dynamic array and...

  • need this updated so it will delete the list and then recreate it again /***********************************************************/ /*...

    need this updated so it will delete the list and then recreate it again /***********************************************************/ /* Header files. */ /***********************************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> /***********************************************************/ /* Structure definitions. */ /***********************************************************/ struct node { int data; struct node *right; struct node *left; }; struct trash { struct node *node; struct trash *next; }; /****************************************/ /* BUILD_LIST. */ /****************************************/ void BUILD_LIST(int number2Add, struct node *(*head), struct node *(*tail)) { int i; struct node *previous, *current; *head = NULL; *tail =...

  • C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the...

    C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...

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