Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C Programming: Using lab.c, lab.h, main.c files lab.h file: #include <stdio.h

ID: 3591355 • Letter: I

Question

In C Programming:

Using lab.c, lab.h, main.c files

lab.h file:

#include <stdio.h>

#include <stdlib.h>

typedef struct nodestruct {

int item;

struct nodestruct *next;

} Node;

typedef struct {

int size; // Number of items on user’s list

Node *head, *tail;

} List;

lab.c file will have two functions:

List* insertSortedNode(List*, int); This function creates a node containing the provided item, then adds it to the list pointed by the provided list pointer and stores the node based on the value of its key item in ascending order. Finally, it should return an updated list pointer. Note: index is zero based.

void removeNode(List*, int); This function removes the node at the location provided by the index from the provided list pointer. Note: index is zero based. The function should be able to print out an error message if the index is beyond the size of the list. Also, make sure to free the memory of the removed node before you exit the function.


main.c file:
1. Using the provided two ADTs, create a list.
2. Generate an array with 5 random integer numbers between 1 and 10. Print out the 'generated' numbers on the console separated by space.
3. Insert each of the generated numbers in the list using the insertSortedNode function.
4. Print the contents of the list on the console separated by space.
5. Remove the node at the 4th index of the list. Next, remove the node at the 1st index of the list. Finally remove the 11th index of the list.
6. Print the contents of the list on the console separated by space.

Example output:

mwc-070138:~ $ gcc main.c lab8.c -Wall -Werror
mwc-070138:~ $ ./a.out
Randomly 'generated' numbers: 1 3 7 2 10

Items on the list: 1 2 3 7 10

Index 11 does not exist. Unable to remove!

Items on the list: 1 3 7

Have a Nice Day!!

Explanation / Answer

lab.c

List* insertSortedNode(List* list, int val)

{

Node* current;

Node* new_node =(Node*) malloc(sizeof(Node));

new_node->item=val;

new_node->next=NULL;

/* Special case for the head end */

if (list->head == NULL || list->head->item >= val)

{

new_node->next = list->head;

list->head = new_node;

}

else

{

/* Locate the node before the point of insertion */

current = list->head;

while (current->next!=NULL &&

current->next->item < new_node->item)

{

current = current->next;

}

new_node->next = current->next;

current->next = new_node;

}

list->size++;

return list;

}

void removeNode(List* list, int pos)

{

// If linked list is empty

if (list->head == NULL)

return;

// Store head node

Node* temp = list->head;

// If head needs to be removed

if (pos == 0)

{

list->head = temp->next; // Change head

free(temp); // free old head

list->size--;

return;

}

// Find previous node of the node to be deleted

for (int i=0; temp!=NULL && i<pos-1; i++)

temp = temp->next;

// If position is more than number of ndoes

if (temp == NULL || temp->next == NULL)

{ printf("Position beyond Linked List");

return;

}

Node *next = temp->next->next;

// Unlink the node from linked list

free(temp->next);  

temp->next = next;  

list->size--;

}

main.c

int main()

{

List *list=(List*)malloc(sizeof(list));

int tmp,i;

srand(time(NULL));

printf(" Randomly 'generated' numbers: ");

for (i=0; i< 5;i++)

{

tmp=rand()%10 + 1;

printf(" %d ", tmp);

insertSortedNode(list,tmp);

}

printf(" Items on the list: ");

Node *curr=list->head;

for (i=0; i< 5;i++)

printf(" %d ", curr->item);

curr=curr->next;

}

removeNode(list,4);

i--;

removeNode(list,1);

i--;

removeNode(list,11);

printf(" Items on the list: ");

while(i) {

printf(" %d ", curr->item);

curr=curr->next;

i--;

}

return 0;

}