Question

In: Computer Science

Can you please do this in C asap. Promise to leave a awesome rating! Thank you...

Can you please do this in C asap. Promise to leave a awesome rating! Thank you in advance. Question and template below

Instructions-

LINKED LIST

In C
'objects' => structs
(struct - collection of data, not methods)
Linked List

Collection of objects which are all connected to each other

Each Object 'points' to the next item in the list


Node (struct) ' O '
- data
- nextNode

Our linked list will store data(structs containing integers) in order from smallest to largest

Generate one node for each data point
each node will point to the next node in the list
each node data point will be less than (or equal to) the 'next' node that it points to
Truths:
nothing points to HEAD
each node points to node of equal or larger value
HEAD smallest data point
last largest data point
last points to NULL


HEAD

'O'
3
-> 'O'
5
-> 'O'
8
-> 'O'
9
-> NULL


& = ADDRESS
// * creates Pointer variable
struct node* nodePtr;
// * also dereferences Pointer
*nodePtr
// look familiar?
obj.field

// put it all together
(*nodePtr).field


// allocating memory

malloc( sizeof(struct node) )
free( ptr )

TEMPLATE for assignment-

#include

#include

struct node

{

// data field

int data;

// nextNode field

struct node* nextNode;

};

/*

struct node myNode;

printf("%d", myNode.data);

printf("%p", myNode.nextNode);

*/

// FUNCTION - allocate and create a new Node

// Precondition - data exists

// Postcondition - Node allocated, and ptr returned

struct node* createNode(int newData)

{

// must allocate memory for each node

// because each nodes lifetime is longer than this function

// *note must manually deallocate (free) each node when program is complete

// memory for these nodes

// must outlive this function call

// myObj() => malloc()

// ~myObj() => free()

struct node* np = malloc(sizeof(struct node));

// set the fields

(*np).data = newData;

(*np).nextNode = NULL;

// return the pointer to this new Node

return np;

}

// FUNCTION - insert new Node into our linked List

// New Node inserted ahead of larger nodes

// Precondition - ptr to list containing 0 or more nodes

// Postcondition - after insertion list is always sorted

struct node* insertNode(struct node* list, int data)

{

// defining our reference data

// new node to be inserted

struct node* np;

// previous node

struct node* prev = NULL;

// current beginning of LL

struct node* head = list;

// current node

struct node* lp = list;

// create a new Node

...

//check if this is our first node

if(...)

{

//start the list with this node since it's empty

...

...

//return new head pointer, since list was empty now head is np

return ...;

}

// MOVE TO WHILE LOOP

// while list pointer isn't at the end

while( ... )

{

// if our current node data is larger than the new node data

// insert into list before current node

if( ... )

{

// insert new node into list by settings it's next node to current node

...

// check if this is NOT the beginning of the list

if(...)

{

// it's not, so remember to set last node to point to new node

...

}

else

{

// it is! cool, set head to new node

...

}

// send back the beginning of the list

return ...;

}

// what's the case for this 'else'?

// ...

else

{

// current node was smaller than new node

// lets check if we're at the end yet

if( ... )

{

// add new node to end of the list

...

// head didn't change, but return it anyway because we're done here

return ...;

}

// move the loop along becaue we aren't at the end of the list yet

...

...

}

}

// list ended for some reason...

// probably shouldn't ever reach this code...

// if we inserted the new node into the list, we would have returned head already...

// hmm...

fprintf(stderr, "ERROR: WE DIDN'T INSERT OUR NEW NODE FOR SOME REASON...");

return head;

}

// FUNCTION - call free on every node in our list from back to front

// Precondition - ptr to list containing 0 or more nodes

// Postcondition - return 0 on all nodes mem freed

// FREE EACH POINTER FROM END OF LIST TO FRONT

int freeListMemory(struct node* list)

{

// COMPLETE THIS ENTIRE FUNCTION

// *hint hint*

// free()

//

//

//on success

return 0;

}

//

// COMPLETE

// DO NOT EDIT

//

int main()

{

// insert create 7 data points

int data1 = 6;

int data2 = 4;

int data3 = 3;

int data4 = 1;

int data5 = 5;

int data6 = 7;

int data7 = 2;

// create POINTER to front of list

struct node *ptrHead;

ptrHead = NULL;

// insert each data point into the list

ptrHead = insertNode(ptrHead, data1);

ptrHead = insertNode(ptrHead, data2);

ptrHead = insertNode(ptrHead, data3);

ptrHead = insertNode(ptrHead, data4);

ptrHead = insertNode(ptrHead, data5);

ptrHead = insertNode(ptrHead, data6);

ptrHead = insertNode(ptrHead, data7);


// create list pointer for our loop

struct node *lp = ptrHead;

// Here we ASSUME list is non-empty

while( lp != NULL)

{

// print out the data for this node

printf("%d\n", (*lp).data);

// set the list pointer to the next node in the list

lp = (*lp).nextNode;

}

// REMEMBER TO FREE ALL LIST POINTERS

// FREE EACH POINTER FROM END OF LIST TO FRONT (reverse order)

// CALL freeListMemory()

int freed = freeListMemory(ptrHead);

return freed;

}

Solutions

Expert Solution

#include<stdio.h>
#include<malloc.h>

struct node

{

// data field

int data;

// nextNode field

struct node* nextNode;

};

/*

struct node myNode;

printf("%d", myNode.data);

printf("%p", myNode.nextNode);

*/

// FUNCTION - allocate and create a new Node

// Precondition - data exists

// Postcondition - Node allocated, and ptr returned

struct node* createNode(int newData)

{

// must allocate memory for each node

// because each nodes lifetime is longer than this function

// *note must manually deallocate (free) each node when program is complete

// memory for these nodes

// must outlive this function call

// myObj() => malloc()

// ~myObj() => free()

struct node* np = malloc(sizeof(struct node));

// set the fields

(*np).data = newData;

(*np).nextNode = NULL;

// return the pointer to this new Node

return np;

}

// FUNCTION - insert new Node into our linked List

// New Node inserted ahead of larger nodes

// Precondition - ptr to list containing 0 or more nodes

// Postcondition - after insertion list is always sorted

struct node* insertNode(struct node* list, int data)

{

// defining our reference data

// new node to be inserted

struct node* np;

// previous node

struct node* prev = NULL;

// current beginning of LL

struct node* head = list;

// current node

struct node* lp = list;

// create a new Node

np=createNode(data);

//check if this is our first node

if(head==NULL)

{

//start the list with this node since it's empty

head=np;

//return new head pointer, since list was empty now head is np

return head;

}

// MOVE TO WHILE LOOP

// while list pointer isn't at the end

while( lp!=NULL )

{

// if our current node data is larger than the new node data

// insert into list before current node

if( lp->data > np->data )

{

// insert new node into list by settings it's next node to current node

np->nextNode=lp;

// check if this is NOT the beginning of the list

if(head!=lp)

{

// it's not, so remember to set last node to point to new node

prev->nextNode=np;

}

else

{

// it is! cool, set head to new node

head=np;

}

// send back the beginning of the list

return head;

}

// what's the case for this 'else'?

//  ////if our current node data is smaller than the new node data

else

{

// current node was smaller than new node

// lets check if we're at the end yet

if( lp->nextNode==NULL )

{

// add new node to end of the list

lp->nextNode=np;

// head didn't change, but return it anyway because we're done here

return head;

}

// move the loop along becaue we aren't at the end of the list yet

prev=lp;

lp=lp->nextNode;

}

}

// list ended for some reason...

// probably shouldn't ever reach this code...

// if we inserted the new node into the list, we would have returned head already...

// hmm...

fprintf(stderr, "ERROR: WE DIDN'T INSERT OUR NEW NODE FOR SOME REASON...");

return head;

}

// FUNCTION - call free on every node in our list from back to front

// Precondition - ptr to list containing 0 or more nodes

// Postcondition - return 0 on all nodes mem freed

// FREE EACH POINTER FROM END OF LIST TO FRONT

int freeListMemory(struct node* list)

{

// COMPLETE THIS ENTIRE FUNCTION

// *hint hint*

// free()

//

//

//on success
free(list);

return 0;

}

//

// COMPLETE

// DO NOT EDIT

//

int main()

{

// insert create 7 data points

int data1 = 6;

int data2 = 4;

int data3 = 3;

int data4 = 1;

int data5 = 5;

int data6 = 7;

int data7 = 2;

// create POINTER to front of list

struct node *ptrHead;

ptrHead = NULL;

// insert each data point into the list

ptrHead = insertNode(ptrHead, data1);

ptrHead = insertNode(ptrHead, data2);

ptrHead = insertNode(ptrHead, data3);

ptrHead = insertNode(ptrHead, data4);

ptrHead = insertNode(ptrHead, data5);

ptrHead = insertNode(ptrHead, data6);

ptrHead = insertNode(ptrHead, data7);



// create list pointer for our loop

struct node *lp = ptrHead;

// Here we ASSUME list is non-empty

while( lp != NULL)

{

// print out the data for this node

printf("%d\n", (*lp).data);

// set the list pointer to the next node in the list

lp = (*lp).nextNode;

}

// REMEMBER TO FREE ALL LIST POINTERS

// FREE EACH POINTER FROM END OF LIST TO FRONT (reverse order)

// CALL freeListMemory()

int freed = freeListMemory(ptrHead);

return freed;

}

Related Solutions

HELLO CAN YOU PLEASE DO THIS JAVA PROGRAM I WILL LEAVE AWESOME RATING. THANK YOU IN...
HELLO CAN YOU PLEASE DO THIS JAVA PROGRAM I WILL LEAVE AWESOME RATING. THANK YOU IN ADVANCE. QUESTION Suppose you are designing a game called King of the Stacks. The rules of the game are as follows:  The game is played with two (2) players.  There are three (3) different Stacks in the game.  Each turn, a player pushes a disk on top of exactly one of the three Stacks. Players alternate turns throughout the game. Each...
Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers()...
Please solve questions in C++ ASAP!! thank you (a) Write a function in C++ called readNumbers() to read data into an array from a file. Function should have the following parameters: (1) a reference to an ifstream object (2) the number of rows in the file (3) a pointer to an array of integers The function returns the number of values read into the array. It stops reading if it encounters a negative number or if the number of rows...
Please answer ASAP, thank you! 1. Please present, analyze, and comment on a (specific or general)...
Please answer ASAP, thank you! 1. Please present, analyze, and comment on a (specific or general) monetary or fiscal policy enacted by the Federal Reserve Bank or by the Federal Government to help our economy by describing the particular context in which that policy was enacted.
can you please do this question? i need the result asap. show the output as well...
can you please do this question? i need the result asap. show the output as well Write a program that will simulate non - preemptive process scheduling algorithm: First Come – First Serve Your program should input the information necessary for the calculation of average turnaround time including: Time required for a job execution; Arrival time; The output of the program should include: starting and terminating time for each job, turnaround time for each job, average turnaround time. Step 1:...
*Please answer questions to case study ASAP. Thank you so much! When you think of the...
*Please answer questions to case study ASAP. Thank you so much! When you think of the most sustainable corporations in the world, Bell Canada may not jump to mind. Nonetheless, Corporate Knights listed Bell as one of only eight Canadian companies to make the Global 100 list of sustainable compa- nies for 2011. Bell Canada Enterprises (BCE) was also named byMaclean’s/Jantzi-Sustainalytics as one of Canada’s Top 50 Socially Responsible Corporations. It also made the prestigious FTSE4Good Global Index. It is...
2 MULTIPLE CHOICE. PLEASE ANSWER ASAP. THANK YOU! 28. Based on the following data for the...
2 MULTIPLE CHOICE. PLEASE ANSWER ASAP. THANK YOU! 28. Based on the following data for the current year, what is the inventory turnover (rounded to one decimal place)? Sales on account during year $489,126 Cost of goods sold during year 195,631 Accounts receivable, beginning of year 42,096 Accounts receivable, end of year 51,765 Inventory, beginning of year 33,834 Inventory, end of year 41,634 a.4.2 b.13 c.18.1 d.5.2 29. Use this information for Kellman Company to answer the question that follow....
use python3 language and need answer ASAP please thank you. The primary sources of calories in...
use python3 language and need answer ASAP please thank you. The primary sources of calories in food are fat, carbohydrate, and protein. Proteins and carbohydrates have four calories per gram, and fat has nine calories per gram. For example, on a box of Honey Nut Cheerios, it says that a serving has 1.5 grams of fat, 22 grams of carbohydrates, and 2 grams of protein. That works out to: 1.5 * 9 + 22 * 4 + 2 * 4...
MULTIPLE CHOICE PLEASE ANSWER ASAP. THANK YOU. 8. A company with working capital of $375,551 and...
MULTIPLE CHOICE PLEASE ANSWER ASAP. THANK YOU. 8. A company with working capital of $375,551 and a current ratio of 3.2 pays a $82,906 short-term liability. The amount of working capital immediately after payment is a.$551,149 b.$463,350 c.$375,551 d.$87,799 11. Assume the following sales data for a company: Current year $758,619 Preceding year 520,482 What is the percentage increase in sales from the preceding year to the current year (rounded to one decimal place)? a.31.4% b.14.4% c.77.1% d.45.8% 12. Cash...
PLEASE ANSWER WITH 200 WORDS OR MORE FOR A THUMBS UP RATING! THANK YOU! In 200...
PLEASE ANSWER WITH 200 WORDS OR MORE FOR A THUMBS UP RATING! THANK YOU! In 200 words or more, please explain why angel investments are important. *NOTICE* I do NOT need a definition of what angel investors are, I just need to know why angel investing is important
PLEASE ANSWER WITH 200 WORDS OR MORE FOR A THUMBS UP RATING! THANK YOU! In 200...
PLEASE ANSWER WITH 200 WORDS OR MORE FOR A THUMBS UP RATING! THANK YOU! In 200 words or more, list some examples of angel investment trends in the United States. *NOTICE* I do NOT need a definition of what angel investors are!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT