Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please i just need you to change the printlist from normal order to reveerse order so it should become s-->f-->c-->a-->B-- please i need it quickly
please i just need you to change the printlist from normal order to reveerse order so it should become s-->f-->c-->a-->B-- please i need it quickly
- Change the function PrintList properly so that it prints the characters of the linked list in reverse order. (15 points)
#include#include struct Node //structure node with character and next data values { char data; struct Node* next; }; struct Node* head = NULL; //head reference of linked list int isEmpty(){ // checking length of linked list if(head==NULL){ //if head is null(there is no nodes in linked list ) return 1; //so return 1 } else{ //else return 0 return 0; } } void printList() // to print linked list { if(isEmpty()){ // if no nodes in linked list print empty list printf("Empty List"); } else{ // else iterate the linked list struct Node* ptr = head; //temperory pointer for iteratioon while (ptr) // iterate until reached last last { printf("%c -> ", ptr->data); //print data ptr = ptr->next; //go to next node } printf("null"); //at last print null } } void countNodes() //number of nodes in linked list { struct Node* ptr = head; //temperory reference node pointed to head for iterating the linked list int count=0; while (ptr) //iterate until reach last element { count++; //increment the count ptr = ptr->next; //goto next node } printf("The number of nodes are: %d",count); } void insert(char data) //inserting new node to linked list { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); //create new node reference newNode->data = data; //insert data if (head == NULL || (head)->data >= newNode->data) //check if data is greater than the previous value { newNode->next = head; //if yes insert data at starting of the linked list head = newNode; return; } struct Node* current = head; //temperory reference node while (current->next != NULL && current->next->data < newNode->data) //check where the data fits in previous linked list current = current->next;//go to next node newNode->next = current->next; //update address value current->next = newNode; } void instructions(){ int ch=1; while(ch!=2){ printf(" Enter your choice: 1 to insert an element into the list 2 to end ?"); scanf("%d",&ch); if(ch==1){ char character; printf(" Enter a character: "); scanf("%s",&character); insert(character); printf(" The list is "); printList(); } else if(ch==2){ countNodes(); printf("End of run"); } } } int main(void) { instructions(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started