Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C program that removes the head of a linked list and prints out the contents of the node it deleted, for example if

Write a C program that removes the head of a linked list and prints out the contents of the node it deleted, for example if the head node contains an int of 432 a double of 3.4 and a char* john then you will remove that node then print out the content.

what I have so far (please add to this template or edit it)

typedef struct student_cell_T { int id; double gpa; char *name; // name is just a pointer here, you need to allocate space for name struct student_cell_T *next; } student_cell_T;

typedef struct linked_list_T { student_cell_T *head; student_cell_T *tail; } linked_list_T;

student_cell_T *NewStudentCell(int id, double gpa, char *name) { student_cell_T *element;

element = (student_cell_T *) malloc( sizeof(student_cell_T) ); if( !element){ fprintf(stderr,"NewStudentCell cannot allocate memory "); return NULL; } element->id = id; element->gpa = gpa; element->name = name;

return element; }

/* * Function: NewLinkedList * Usage: linked_list_T *list; * list = NewLinkedList(); * -------------------------- * This function allocates and returns an empty linked list. */ linked_list_T *NewLinkedList(void) { linked_list_T *list;

list = (linked_list_T *) malloc( sizeof(linked_list_T) ); if( !list){ fprintf(stderr,"NewLinkedList cannot allocate memory "); return NULL; }

list->head = NULL; list->tail = NULL;

return list; }

// adds cell to tail void Enlist(linked_list_T *list, student_cell_T *element) { // if null then the cell inserted will be the head and the tail since it is the only element there if (list -> tail == NULL) { list -> head = list -> tail = element; return; } (list -> tail) -> next = element; // points tail to next node (list -> tail) = (list -> tail) -> next; // asigning the tail to the last node }

// removes the node at the head, if empty return null student_cell_T *Delist(linked_list_T *list) { // if list is empty if (list -> head == NULL) { printf("ERROR: list is empty "); return NULL; } // if not empty remove from head else { list -> head = list -> head -> next; // removes first element and points to the node next to the head return list -> head; }

}

void printList (linked_list_T* list) { student_cell_T* element;

for (element = list -> head; element != NULL; element = element -> next) { printf("User id: %d, GPA: %lf, Name: %s ",element -> id,element -> gpa,element -> name); } printf(" "); }

int main (){

student_cell_T* element; linked_list_T* list;

list = NewLinkedList(); // creates the linked list

// write solution here by calling the above functions

return 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

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions