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...
can you explain 5d and 5e, thank you, I will leave a good rating! 5d. In...
can you explain 5d and 5e, thank you, I will leave a good rating! 5d. In a comparative study of two new drugs, A and B, 300 patients were treated with drug A, and 275 patients were treated with drug B. (The two treatment groups were randomly and independently chosen.) It was found that 222 patients were cured using drug A and 217 patients were cured using drug B. Let p1 be the proportion of the population of all patients...
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...
Design a role-based access control system. Thank you. Will leave a great rating A course registration...
Design a role-based access control system. Thank you. Will leave a great rating A course registration system is a computer system used by the university's administration to plan courses, for students to register for courses, and for faculty to grade them. Students and faculty can only enroll or teach a class if they have been recorded in the registration system. Students are entered into the system by and Admissions representatives; faculty members by a Human Resources employee. For all persons,...
C PROGRAM STRING AND FILE PROCESSING LEAVE COMMENTS! I WILL LEAVE POSITIVE REVIEW! THANK YOU :)...
C PROGRAM STRING AND FILE PROCESSING LEAVE COMMENTS! I WILL LEAVE POSITIVE REVIEW! THANK YOU :) I need a program that 1) Count all words in a file. A word is any sequence of characters delimited by white space or the end of a sentence, whether or not it is an actual English word. 2)Count all syllables in each word. To make this simple, use the following rules: •Each group of adjacent vowels (a, e, i, o, u, y) counts...
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...
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