Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2 Step 2: allocate free nodes (20 marks) In this section, you will make the program allocate extra free space. Free nodes will be added

image text in transcribed

image text in transcribed

2 Step 2: allocate free nodes (20 marks) In this section, you will make the program allocate extra free space. Free nodes will be added to the list in blocks of BLOCKSIZE nodes, where BLOCKSIZE is a macro defined as #define BLOCKSIZE 5 Your task is to write a function declared as void allocateBlock (struct list plist, int *counter) that adds BLOCKSIZE nodes on the right of the node pointed by pList->left. The function should consist of a loop of BLOCKSIZE iterations where, at each iteration, you allocate a new object of type struct node with allocator link the new node to the existing ones so that the doubly-linked structure of the list is preserved set i of the new node to - 1 increase pList->length by one To preserve the doubly-linked structure of the list at every step, you need to . let prev of the new node be a pointer to the node pointed by pList->left let next of the new node be a pointer to the node pointed by pList->left->next . let prev of the node pointed by plist->left->next be a pointer to the new node let next of the node pointed by pList->left be a pointer to the new node To get an intuition of what the function is supposed to do, draw a simple cartoon of the node-insertion process. Copy step1.c into a new file, call it step2.c, and replace main with int main() { int counter = 0; struct list myList; printf("initialising the list "); initialiseList(&myList, &counter); printList (EmyList, &counter); printf ("allocating $d free nodes ", BLOCKSIZE); allocateBlock (EmyList, Ecounter); printList (&myList, 6counter); printf("freeing $d free nodes ", BLOCKSIZE); deAllocateBlock (EmyList, scounter); printList (EmyList, &counter); print("Ereeing head and tail ... "); removeList (&myList. counter); printList (AmyList, &counter); } Code 3: Free node allocation 1 2 3 4 6 ... 7 5 10 11 12 13 11 15 IG 17 6 Check that the file contains all required headers, macros, structure definitions and function declarations before main and all functions definitions after it. The code of de AllocateBlock, printList, and removeList are given in Appendix A. Example If you set BLOCKSIZE to 5, the output should be initialising the list ... | 0 | 10 | counter - 2 allocating 5 free nodes ... 101 1-1 1-1 | 1-1 1-1 1-1 1011 counter - 7 freeing 5 free nodes ... 101 101 counter - 2 freeing head and tail ... counter - 0 2 Step 2: allocate free nodes (20 marks) In this section, you will make the program allocate extra free space. Free nodes will be added to the list in blocks of BLOCKSIZE nodes, where BLOCKSIZE is a macro defined as #define BLOCKSIZE 5 Your task is to write a function declared as void allocateBlock (struct list plist, int *counter) that adds BLOCKSIZE nodes on the right of the node pointed by pList->left. The function should consist of a loop of BLOCKSIZE iterations where, at each iteration, you allocate a new object of type struct node with allocator link the new node to the existing ones so that the doubly-linked structure of the list is preserved set i of the new node to - 1 increase pList->length by one To preserve the doubly-linked structure of the list at every step, you need to . let prev of the new node be a pointer to the node pointed by pList->left let next of the new node be a pointer to the node pointed by pList->left->next . let prev of the node pointed by plist->left->next be a pointer to the new node let next of the node pointed by pList->left be a pointer to the new node To get an intuition of what the function is supposed to do, draw a simple cartoon of the node-insertion process. Copy step1.c into a new file, call it step2.c, and replace main with int main() { int counter = 0; struct list myList; printf("initialising the list "); initialiseList(&myList, &counter); printList (EmyList, &counter); printf ("allocating $d free nodes ", BLOCKSIZE); allocateBlock (EmyList, Ecounter); printList (&myList, 6counter); printf("freeing $d free nodes ", BLOCKSIZE); deAllocateBlock (EmyList, scounter); printList (EmyList, &counter); print("Ereeing head and tail ... "); removeList (&myList. counter); printList (AmyList, &counter); } Code 3: Free node allocation 1 2 3 4 6 ... 7 5 10 11 12 13 11 15 IG 17 6 Check that the file contains all required headers, macros, structure definitions and function declarations before main and all functions definitions after it. The code of de AllocateBlock, printList, and removeList are given in Appendix A. Example If you set BLOCKSIZE to 5, the output should be initialising the list ... | 0 | 10 | counter - 2 allocating 5 free nodes ... 101 1-1 1-1 | 1-1 1-1 1-1 1011 counter - 7 freeing 5 free nodes ... 101 101 counter - 2 freeing head and tail ... counter - 0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions