C language This is a study guide for an exam! The 3 important functions necessar
ID: 3852143 • Letter: C
Question
C language
This is a study guide for an exam!
The 3 important functions necessary to create a list of any ADT would be:
______ will allow you to return the memory to the system for use
______ is the value you should check for. If your request for memory fails, you will get this value.
______ will allow you to request memory to create a node
______ will allow you to determine how much memory you will need for a node
Assume I have the following linked list of nodes as we discussed in class:
B ---> C ---> E ---> G---> NULL
and each node has the following address: B=4000, C=2500, E=8120, G=3400, F=6000. If I have
successfully completed inserting a new node “F” in the proper location in this list ,
what would the printf() on line “aa “ display for each pointer value? (IGNORE COMPILE ERRORS)
Using the node structure we used in class to discuss this function how much space would a node
take assuming an int is 2 bytes, a char is 1 byte, a float is 4 bytes and a pointer is 4 bytes?
What should the AAAAAAAAAAAAAAAA in the printf() on line “dd” indicate in it’s message?
a. void insert ( ListNodePtr *sPtr, char value)
b. {
c. ListNodePtr newPtr, previousPtr, currentPtr;
d. newPtr = malloc (sizeof (ListNode) );
e.
f. if ( NewPtr != NULL) {
g. newPtr->data = value;
h. newPtr->nextPtr = NULL;
j. previousPtr = NULL;
k. currentPtr = *sPtr;
m.
n. while ( currentPtr != NULL && value > currentPtr->data) {
o. previousPtr = currentPtr;
p. currentPtr = currentPtr->nextPtr;
r. }
s. if (previousPtr == NULL) {
t. newPtr->nextPtr = *sPtr;
u. *sPtr = newPtr;
v. }
w. else {
x. previousPtr->nextPtr = newPtr;
y. newPtr->nextPtr = currentPtr;
z. }
aa. printf( “%p %p %p %p ”, *sPtr, currentPtr, previousPtr, newPtr);
bb. }
cc. else {
dd. printf(“ AAAAAAAAAAAAAAA ”);
ee. }
ff. }
Explanation / Answer
On successful completion of insertion a new node “F” in the proper location in this list , printf() on line “aa “ will display the follwing for each pointer value
4000 //sPtr B
3400 //currentPtr G
8120 //previousPtr E
9000 //newPtr F
Explanation : We are moving in the list until we get a value in the list greater than the value passed in the argument.
Initially currentPtr is assigned the sPtr, and on moving the currentPtr we are keeping previous address in previousPtr. And when F is inserted previousPtr's nextPtr is pointed to the newPtr and newPtr's nextPtr is pointed to the currentPtr . And in this whole process sPtr is unchanged.
Node will take a space of 5 bytes. 1 byte for the char and 4 bytes for the pointer, that means (1+4)=5 bytes.
the AAAAAAAAAAAAAAAA in the printf() on line “dd” indicates that NewPtr is not able to successfully allocate memory dynamically and is pointing to NULL,i.e., a memory location which a user is not allowed to access.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.