Question & Answer: PART 1: Create a List ADT based on the following structures. Functions to be supported include: creating an initial empty list; r…..

IN C Programming

PART 1: Create a List ADT based on the following structures. Functions to be supported include: creating an initial empty list; retrieving the kth item on a list; checking whether a given key is in a list; returning the size of a list; and deleting (freeing) a list.

PART 2: Re-implement using dummy nodes.

typedef struct nodestruct {

Key item;

struct nodestruct *next;

} Node;

typedef struct {

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

Node *head, *tail;

} List; t

ypedef struct linkedlist {

Node *head, *tail;

} List;

Expert Answer

 

Hi Friend, I have answered PART 1 completely.

Please repost other part in separate post.

Please let me know in case of any issue in PART 1.

#include <stdio.h>
#include <stdlib.h>

struct Node
{
int data;
struct Node *next;
};

struct Node *empty_list(){
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
return new_node;
}

int size(struct Node *head) {

int count = 0;
while(head != NULL) {
count++;
head = head->next;
}

return count;
}

int kthItem(struct Node *head, int k) {

int N = size(head);

if(N < k) {
printf(“Less number of noden”);
return 0;
}

int i = 0;
while(i < k) {
head = head->next;
}

return head->data;
}

int checkItem(struct Node *head, int item) {

while(head != NULL){

if(head->data == item)
return 1;
}

return 0;
}

void deleteList(struct Node **head) {

struct Node *t = NULL;
struct Node *temp = *head;

while(temp != NULL) {
t = temp;
temp = temp->next;
free(t);
}
}

Still stressed from student homework?
Get quality assistance from academic writers!