Anúncio

coding in C- Create a function called reverseList that takes the head.docx

9 de Feb de 2023
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
coding in C- Create a function called reverseList that takes the head.docx
Próximos SlideShares
Lab Week 2 Game Programming.docxLab Week 2 Game Programming.docx
Carregando em ... 3
1 de 3
Anúncio

Mais conteúdo relacionado

Similar a coding in C- Create a function called reverseList that takes the head.docx(20)

Mais de tienlivick(20)

Anúncio

coding in C- Create a function called reverseList that takes the head.docx

  1. coding in C; Create a function called reverseList that takes the head of a linked list, reverses the order of all the nodes. For example, if the list contained 1, 2, 3, 4 in its nodes, the list will now contain 4, 3, 2, 1. please add a main test. Solution Please find the required program below: #include<stdio.h> #include<stdlib.h> struct node { int data; struct node* next; }; void push(struct node** head, int new_element) { struct node* new_node = (struct node*) malloc(sizeof(struct node)); new_node->data = new_element; new_node->next = (*head); (*head) = new_node; } void printList(struct node *head) { struct node *temp = head;
  2. while(temp != NULL) { printf("%d ", temp->data); temp = temp->next; } } void reverseList(struct node** head) { struct node* prev = NULL; struct node* current = *head; struct node* next; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } *head = prev; } int main() { struct node* head = NULL; push(&head, 12); push(&head, 14); push(&head, 5); push(&head, 8); printf(" Linked list Before Reversing : "); printList(head); reverseList(&head); printf(" Linked list After Reversing : "); printList(head); } -------------------------------------------------------------------------------- OUTPUT: Linked list Before Reversing : 8Â Â 5Â Â 14Â Â 12 Linked list After Reversing :
  3. 12Â Â 14Â Â 5Â Â 8
Anúncio