Write a C program that creates the dynamically alocated list holding strings with different size. Problem 1: To warm up, declare a double pointer to char variable and set it to NULL. Then, declare an integer el Nr that holds the number of element in the list and set it to 3. Using malloc function allocate a memory block your list to hold 3 items. Now, you need to add the first item (a string) to the list. Check the length of the string you want to add, and allocte a memory block to hold this string, using the malloc function. Remember, to include the space for the end-of-line character. Copy your string to the allocated memory block using strcpy function. Perform the same steps to add two more items. Problem 2: Create a function print_list which prints all elements in the list. Problem 3: Now, you will add automatically the elements to your list. Declare an integer el Nr that holds the number of element in the list, and initiate it with 0. Using malloc function allocate a memory block your list to hold a single item. Create a function add item which adds a new string, passed as an argument, to your list. This function should increase el Nr by one, each time it is invoked, and reallocate the memory block for your list with a new size, using realloc function. Then, allocate the memory block for the string you want to add (the last element of the expanded list), using malloc function. Finally, copy the string you want to add to the allocated memory block, using strcpy function.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Write a C program that creates a dynamically allocated list holding strings with different sizes.

Write a C program that creates the dynamically alocated list holding strings with different size.
Problem 1:
To warm up, declare a double pointer to char variable and set it to NULL. Then, declare an integer
el Nr that holds the number of element in the list and set it to 3. Using malloc function allocate a
memory block your
list to hold 3 items. Now, you need to add the first item (a string) to the list.
Check the length of the string you want to add, and allocte a memory block to hold this string,
using the malloc function. Remember, to include the space for the end-of-line character. Copy
your string to the allocated memory block using strcpy function. Perform the same steps to add
two more items.
Problem 2:
Create a function print_list which prints all elements in the list.
Problem 3:
Now, you will add automatically the elements to your list. Declare an integer el Nr that holds the
number of element in the list, and initiate it with 0. Using malloc function allocate a memory
block your list to hold a single item. Create a function add_item which adds a new string, passed as
an argument, to your list. This function should increase el Nr by one, each time it is invoked, and
reallocate the memory block for your list with a new size, using realloc function. Then, allocate
the memory block for the string you want to add (the last element of the expanded list), using
malloc function. Finally, copy the string you want to add to the allocated memory block, using
strcpy function.
Problem 4:
Create a function remove_item which removes a string you pass as an argument. This function
should check, if the string you want to remove is in the list, and if it is so, then record the index
of the item you want to remove. You may assume, that there are no double items. To remove the
item, at first free the memory block occupied by the item. Then, shift all subsequent items by one
to the left (for these items, you assign to each pointer the subsequent pointer). For the pointer of
last item in the list, assign NULL. Then, decrease the el Nr variable and reduce memory block for
the list using realloc function.
Problem 5:
Test your code, adding and removing few items, and print the list. Free all the memory before
finishing the program.
Transcribed Image Text:Write a C program that creates the dynamically alocated list holding strings with different size. Problem 1: To warm up, declare a double pointer to char variable and set it to NULL. Then, declare an integer el Nr that holds the number of element in the list and set it to 3. Using malloc function allocate a memory block your list to hold 3 items. Now, you need to add the first item (a string) to the list. Check the length of the string you want to add, and allocte a memory block to hold this string, using the malloc function. Remember, to include the space for the end-of-line character. Copy your string to the allocated memory block using strcpy function. Perform the same steps to add two more items. Problem 2: Create a function print_list which prints all elements in the list. Problem 3: Now, you will add automatically the elements to your list. Declare an integer el Nr that holds the number of element in the list, and initiate it with 0. Using malloc function allocate a memory block your list to hold a single item. Create a function add_item which adds a new string, passed as an argument, to your list. This function should increase el Nr by one, each time it is invoked, and reallocate the memory block for your list with a new size, using realloc function. Then, allocate the memory block for the string you want to add (the last element of the expanded list), using malloc function. Finally, copy the string you want to add to the allocated memory block, using strcpy function. Problem 4: Create a function remove_item which removes a string you pass as an argument. This function should check, if the string you want to remove is in the list, and if it is so, then record the index of the item you want to remove. You may assume, that there are no double items. To remove the item, at first free the memory block occupied by the item. Then, shift all subsequent items by one to the left (for these items, you assign to each pointer the subsequent pointer). For the pointer of last item in the list, assign NULL. Then, decrease the el Nr variable and reduce memory block for the list using realloc function. Problem 5: Test your code, adding and removing few items, and print the list. Free all the memory before finishing the program.
Expert Solution
steps

Step by step

Solved in 7 steps with 4 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY