Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C Programming: Given ADTs: typedef struct Node{ int item; struct Node* next; }Node; typedef struct List{ Node* head,*tail; int size; }List; Functions to create:

In C Programming:

Given ADTs:

typedef struct Node{

int item;

struct Node* next;

}Node;

typedef struct List{

Node* head,*tail;

int size;

}List;

Functions to create:

List* insertNode(int index, int item, List* uList); //Implement an insertNode() function which inserts a new node containing a given input item into a linked list at a given index and returns an updated list. If input index is 0, put the new node before head node and update head; if input index i is larger than 0 and smaller than the number of nodes in the list, insert the new node after the ith node. Otherwise, insert the node after the tail node, and update tail.

List* removeNode(int key, List* uList); //Next, implement a removeNode() function which deletes the first occurrence of an input key and returns an updated list. If there is only one node in the list and it needs to be deleted, you need to delete this note and return NULL. If there is no such key in the list, returns the original list. You should update *head or *tail if necessary.

Given functions:

void printList(List* mylist){

if(mylist == NULL) return;

Node* p = mylist->head;

for(int i = 0;isize;i++){

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

p = p->next;

}

printf(" ");

printf("The size of the input linked list is: %d ",mylist->size);

}

void freeList(List* mylist)

{

if(mylist == NULL) return;

Node* tmp;

for(int i = 0;isize;i++){

tmp = mylist->head;

mylist->head = tmp->next;

free(tmp);

}

free(mylist);

}

In main:

1. Create a linked list with 5 nodes. The items in the list are 1 2 3 2 1. 2. Use the given print list function to print the list onto the console. 3. Insert a new node containing item 4 at index 0, and print the updated list, the output should be like 4 1 2 3 2 1. 4. Insert a new node containing item 5 at index 2, and print the update list, the output should be like 4 1 5 2 3 2 1. 5. Insert a new node containing item 6 at index 8, and print the update list, the output should be like 4 1 5 2 3 2 1 6. 6. Call removeNode to remove a node containing key 1. 7. Call removeNode to remove a node containing key 10. 8. Free all the allocated memory.

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

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions