Question: Please write c program, Give input of -1 to exit the program. 1. Enter value and index: 3 0 Array: 3 2. Enter value and

Please write c program,

Give input of -1 to exit the program. 1. Enter value and index: 3 0 Array: 3 2. Enter value and index: 5 1 Array: 3 5 3. Enter value and index: 7 0 Array: 7 5 4. Enter value and index: 3 2 Array: 7 5 3 5. Enter value and index: 2 3 *** Growing array from size 3 to 6

Array: 7 5 3 2 6. Enter value and index: 13 6 *** Growing array from size 6 to 9 Array: 7 5 3 2 0 0 13 7. Enter value and index: -1 Done.

main.c

#include #include

#define true 1 // Make true a synonym for 1 const int Delta = 3; // Array growth increment

typedef struct Node *NodePtr; // NodePtr is a synonym for: struct Node * struct Node { int data; // The data stored at the node int index; // Index position of this node, when viewed as array NodePtr pNext; // Pointer to the next node };

//------------------------------------------------- void displayArray( NodePtr pCurrent) // Pointer to the first list Node { // Display the index values stored on the array nodes. printf("Array indices: "); NodePtr pTemp = pCurrent; // make a copy, so original can still be used below. while( pTemp != NULL) { // Don't print the sentinel Node value if( pTemp->data != -1) { printf("%3d", pTemp->index); } pTemp = pTemp->pNext; } printf(" "); // Display the numbers stored on the array nodes. printf(" values: "); while( pCurrent != NULL) { // Don't print the sentinel Node value if( pCurrent->data != -1) { printf("%3d", pCurrent->data); } pCurrent = pCurrent->pNext; } printf(" "); } //end displayArray(..)

//---------------------------------------------------- // Find and return the pointer to the nth Node on the list. // Return NULL if the index value we are searching for is // not found. NodePtr findNthNode( NodePtr pCurrent, // Starts at head of list int indexToFind) // Index value we are looking for { // Advance past the sentinel Node to the first Node, if there is one. if( pCurrent != NULL && pCurrent->index == -1) { pCurrent = pCurrent->pNext; } // Iterate down the list looking for the indexToFind. // Make sure we stop at the end of the list, in case indexToFind // is larger than the index at the end of the list. while( pCurrent != NULL && pCurrent->index < indexToFind) { // Advance pCurrent to point to next node pCurrent = pCurrent->pNext; } // Return pointer to the Node where index was found, or NULL if not found. if( pCurrent != NULL && indexToFind == pCurrent->index) { return pCurrent; } else { return NULL; } } //end findNthNode(..)

//---------------------------------------------------- // Add newNumber into array. If index is larger than // the current array size, then grow the array in groups // of 3 nodes at a time until index is now in range. // // Once this is done, add the new element and update // the new current size. // void addValueAt( //... parameters here ... ) { // If newNumberIndex is larger than the current number of Nodes on the list, // then keep growing the list three nodes at a time until it is in range. // ...

// List is now large enough to accomodate the newNumberIndex. // Find the node where it should go using findNthNode(..); // ... // Add the new element // ... } //end addValueAt(...)

//----------------------------------------------- int main() { int moveNumber = 1; // Numbers user inputs. int maxSize = 0; // Number of Nodes in storage, not counting the sentinel Node. int newNumber = 0; // User input of new number to be added. int newNumberIndex = 0; // User input of index position where new number should go. // Allocate space for the initial sentinel Node and initialize its values. NodePtr pHead = (NodePtr) malloc( sizeof( struct Node)); pHead->index = -1; pHead->data = -1; pHead->pNext = NULL; NodePtr pTail = pHead; // Tail initially points to sentinel node // Allocate the first set of 3 nodes // ... // Prompt for, get and store new number printf("Give input of -1 to exit the program. "); do { printf("%2d. Enter value and index: ", moveNumber); scanf("%d", &newNumber); if( newNumber == -1) { break; // Exit when -1 is entered } else { // Also scan the index scanf("%d", &newNumberIndex); } // Add the new value, growing storage if needed. addValueAt(...);

// Display the array with this new element displayArray( pHead); // Increment the moveNumber moveNumber++; } while( true);

printf("Done. "); return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!