Question

Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...

Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation of the Table ADT. Make sure that you apply the concepts of design by contract (DbC) to your implementation.

Once you have fully implemented the table, create a main.c file that implements a testing framework for your table.

Your table implementation must ensure that values inserted are unique, and internally sorted within a linked list.

table.h

#ifndef _TABLE_H
#define _TABLE_H

//-----------------------------------------------------------------------------
// CONSTANTS AND TYPES
//-----------------------------------------------------------------------------

typedef enum BOOL { false, true } Boolean;

//-----------------------------------------------------------------------------
// PROTOTYPES
//-----------------------------------------------------------------------------

// add an element to the table 
Boolean insertItem( int item );
// removes the int from the table
Boolean removeItem( int item );
// empty the table so that we clear all memory and can start a fresh table
void clearTable( );
// tells us whether or not the given item is in the table
Boolean search( int item );
// table iterators
Boolean firstItem( int * const item );
Boolean nextItem( int * const item );

#endif

linkedList.c

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

typedef enum BOOL { false, true } bool;

// Linked list node definition
typedef struct Node node;

struct Node
{
        int   number;
        node  *next;
};

static node *top = NULL;
 
// used to track where we are for the list traversal methods
static node *traverseNode = NULL;
    
// "destroy" will deallocate all nodes in a linked list object
// and will set "top" to NULL.
void destroy()
{
  node *curr = top;
  node *temp = NULL;

  while ( curr != NULL )
  {
    // flip order to see it blow up...
    temp = curr;
    curr = curr->next;

    free( temp );
  }

  top = NULL;
}


// "build" will create an ordered linked list consisting
// of the first "size" even integers.
void build( int size )
{
  node *newNode = NULL;
  int i = 0;

  // make sure we don't have a list yet
  destroy();

  for ( i=size ; i>0 ; i-- )
  {
    newNode = malloc( sizeof( node ) );
    newNode->number = i*2;
    newNode->next = top;

    top = newNode;
  }
}


// starts a list traversal by getting the data at top.
// returns false if top == NULL.
bool firstNode( int *item )
{
  bool result = false;
  
  if ( top )
  {
    *item = top->number;
    
    traverseNode = top->next;
  
    result = true;
  }  
  
  return result;
}


// gets the data at the current traversal node and increments the traversal.
// returns false if we're at the end of the list.
bool nextNode( int *item )
{
  bool result = false;
  
  if ( traverseNode )
  {
    *item = traverseNode->number;
    
    traverseNode = traverseNode->next;
    
    result = true;
  }
  
  return result;
}


// "print" will output an object's entire linked list 
// to the standard output device -- one "number" per line.
void print()
{
  int value;
  
  if ( firstNode( &value ) )
  {
    do
    {
      printf( "%d\n", value );
    } while ( nextNode( &value ) );
  }
}

int main( int argc, char *argv[] )
{
  build( 10 );
  print();
  destroy();
  build( 5 );
  build( 20 );
  print();
  destroy();
  
  return 0;
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

khsdg

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

typedef enum BOOL { false, true } bool;

// Linked list node definition

typedef struct Node node;

struct Node

{

int number;

node *next;

};

static node *top = NULL;

// used to track where we are for the list traversal methods

static node *traverseNode = NULL;

// "destroy" will deallocate all nodes in a linked list object

// and will set "top" to NULL.

void destroy()

{

node *curr = top;

node *temp = NULL;

while ( curr != NULL )

{

// flip order to see it blow up...

temp = curr;

curr = curr->next;

free( temp );

}

top = NULL;

}

// "build" will create an ordered linked list consisting

// of the first "size" even integers.

void build( int size )

{

node *newNode = NULL;

int i = 0;

// make sure we don't have a list yet

destroy();

for ( i=size ; i>0 ; i-- )

{

newNode = malloc( sizeof( node ) );

newNode->number = i*2;

newNode->next = top;

top = newNode;

}

}

// starts a list traversal by getting the data at top.

// returns false if top == NULL.

bool firstNode( int *item )

{

bool result = false;

if ( top )

{

*item = top->number;

traverseNode = top->next;

result = true;

}

return result;

}

// gets the data at the current traversal node and increments the traversal.

// returns false if we're at the end of the list.

bool nextNode( int *item )

{

bool result = false;

if ( traverseNode )

{

*item = traverseNode->number;

traverseNode = traverseNode->next;

result = true;

}

return result;

}

// "print" will output an object's entire linked list

// to the standard output device -- one "number" per line.

void print()

{

int value;

if ( firstNode( &value ) )

{

do

{
printf( "%d\n", value );

} while ( nextNode( &value ) );

}

}

int main( int argc, char *argv[] )

{

build( 10 );

print();

destroy();

build( 5 );

build( 20 );

print();

destroy();

return 0;

}

Add a comment
Know the answer?
Add Answer to:
Using the provided table interface table.h and the sample linked list code linkedList.c, complete an implementation...
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
  • Double linked list implementation of PutItem function. How to fix my code to get desired output b...

    Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void setValue(int newValue);     int getValue() const;     RelationType ComparedTo(ItemType newItem); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::setValue(int newValue) {     value = newValue; } int ItemType::getValue() const {     return value; } RelationType ItemType::ComparedTo(ItemType newItem)...

  • Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the...

    Problem 3 (List Implementation) (35 points): Write a method in the DoublyLList class that deletes the first item containing a given value from a doubly linked list. The header of the method is as follows: public boolean removeValue(T aValue) where T is the general type of the objects in the list and the methods returns true if such an item is found and deleted. Include testing of the method in a main method of the DoublyLList class. ------------------------------------------------------------------------------------- /** A...

  • LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete...

    LAB: Inserting an integer in descending order (doubly-linked list) Given main() and an IntNode class, complete the IntList class (a linked list of IntNodes) by writing the insertInDescendingOrder() method to insert new IntNodes into the IntList in descending order. Ex. If the input is: 3 4 2 5 1 6 7 9 8 -1 the output is: 9 8 7 6 5 4 3 2 1 ___________________________________________________________________________________________________________________________________________________ SortedList.java (READ ONLY!!!) import java.util.Scanner; public class SortedList { public static void main...

  • C++ - I have a doubly linked list, but I haven't been able to get the...

    C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...

  • I am trying to make a linked list queue and I am trying to use the...

    I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...

  • Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for ...

    Instructions Part 1 - Implementation of a Doubly Linked List Attached you will find the code for an implementation of a Singly Linked List. There are 3 files: Driver.java -- testing the List functions in a main() method. LinkNode.java -- Class definition for a Node, which is the underlying entity that stores the items for the linked list. SinglyLinkedList.java -- Class definition for the Singly Linked List. All the heavy lifting happens here. Task 1 - Review & Testing: Create...

  • This is a code for linked list, it is about adding a node in the middle...

    This is a code for linked list, it is about adding a node in the middle of a list, I am really confused why int i = 2? can't it be 0? Add to the Middle • Allocate memory and store data for new node Traverse to node just before the required position of new node Change next pointers to include new node in between struct node *newNode; newNode = malloc(sizeof(struct node)); newNode->data = 4; struct node *temp head; for(int...

  • Write a complete bag class implementation using linked implementation. The linked bag class name must be...

    Write a complete bag class implementation using linked implementation. The linked bag class name must be LinkedBag and name your test program as LinkedBagDemo. Your test program should include following test conditions: 1. Get the number of items currently in the bag 2. See whether the bag is full 3. See whether the bag is empty 4. Add a given object to the bag 5. Remove an unspecified (not random) object from the bag 6. Remove an occurrence of a...

  • ^^^ Q3. I am trying to implement double linked list but I was failed to write...

    ^^^ Q3. I am trying to implement double linked list but I was failed to write the code so anyone gives the main Code in the main function   THANK YOU FOR ADVANCE #include<stdio.h> #include<stdlib.h> #include<alloc.h> struct node {      int info;      struct node *lptr,*rptr; }; typedef struct node DL; DL *delete( ) , *insert ( ); void display(); DL *delete(DL *start,int x) {      DL *left,*right,*curr;      curr = start;      if( start == NULL)       {                 printf("\nDoubly...

  • C programming A linked list is a linear data structure that allows us to add and remove items fro...

    c programming A linked list is a linear data structure that allows us to add and remove items from the list very quickly, by simply changing a few pointers. There are many different variations of linked lists. We have studied the doubly-linked, circular, with a dummy-header-node version of a linked list. In the class notes we studied several functions to manipulate a Linked List. For this assignment you must write the code for the following additional linked list functions: addFirst,...

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