Question: Design and Implement a single linked list with the following operations. a- Create n nodes b- Delete from the middle c- Insert in the middle
Design and Implement a single linked list with the following operations.
a- Create "n" nodes
b- Delete from the middle
c- Insert in the middle
When I did the code I keep getting a problem saying getnode() has to return a value. Can someone help fix the code for me please?
#include
//Counts and returns number of nodes available int countnode() { node *temp; int length = 0; //Initializes temporary node to start temp = start; //Loops till end while (temp != NULL) { //Increase the length by one length++; //Move next temp = temp->next; }//End of while //Returns length return length; }//End of function
//Creates a list with size N void LIST_NODE::creatlist(int n) { int i; node *newnode; node * temp; //Loops till size entered by the user for (i = 0; i < n; i++) { //Creates a node newnode = getnode(); //Checks for starting node if (start == NULL) { start = newnode; } else { //Initializes temporary node to start temp = start; //Loops till end while (temp->next != NULL) { temp = temp->next; } temp->next = newnode; }//End of else }//End of for loop }//End of function
//Insert data void insert_in_the_middle() { node *newnode, *temp, *prev; int pos, nodectr; int ctr = 1; cout<<" Enter the data to insert: "; newnode = getnode(); cout << " Enter he position where you want to insert the node: "; cin >> pos; nodectr = countnode(); //Validates position if (pos > 1 && pos < nodectr) { temp = prev = start; //Loops till counter is less than position while (ctr < pos) { prev = temp; temp = temp->next; ctr++; } prev->next = newnode; newnode->next = temp; } else { cout << " The position is not in the middle. try again "; } }//End of function
//Delete node void delete_node_in_middle() { int ctr = 1; int pos, nodectr; node *temp, *prev; //Validates for empty if (start == NULL) { cout << " Empty list: "; return; } else { cout << "Enter position where you want to delete node: "; cin >> pos; nodectr = countnode(); //Validates position if (pos > 1 && pos < nodectr) { temp = prev = start; //Loops till counter is less than position while (ctr < pos) { prev = temp; temp = temp->next; ctr++; } prev->next = temp->next; free(temp); cout << " Node deleted "; } else { cout << " Invalid position"; } }//End of for loop }//End of function
//Display void display() { node *temp; //Validates empty if (start == NULL) { cout << " Empty list"; return; } else { //Assigns start to temporary temp = start; //Loops till end while (temp->next != NULL) { cout<
//Main function int main() { node n; char ch; int no; //Loops till q or Q entered by the user do { //Displays menu cout<<" A or a - Create \"N\" nodes"; cout<<" B or b - Delete from the middle"; cout<<" C or c - Insert in the middle"; cout<<" D or d - Display"; cout<<" Q or q - Quit"; //Accepts user choice cout<<" Enter your choice: "; cin>>ch; switch(ch) { case 'A': case 'a': cout<<" Enter the size of Linked List: "; cin>>no; n.creatlist(no); break; case 'B': case 'b': delete_node_in_middle(); break; case 'C': case 'c': insert_in_the_middle(); break; case 'D': case 'd': display(); break; case 'Q': case 'q': exit(0); default: cout<<" Invalid choice"; }//End of switch }while(1);//End of while }//End of main
Step by Step Solution
There are 3 Steps involved in it
Your code is almost complete but there are a few issues that need to be addressed before it can function correctly Lets go through and fix these issue... View full answer
Get step-by-step solutions from verified subject matter experts
