Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and Implement a single linked list with the following operations. a- Crea

ID: 3869426 • Letter: D

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

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 <iostream>
#include <stdlib.h>
using namespace std;
//Structure to create node
struct LIST_NODE
{
// Data element of node
int data;
void creatlist(int n);
// Pointer element of node
struct LIST_NODE *next;
};
typedef struct LIST_NODE node;
//initialize start pointer to null
node *start = NULL;
//Creates a node
node *getnode()
{
node *newnode;
newnode = (node*)malloc(1000);
cout << " Enter Data: ";
cin >> newnode->data;
newnode->next = NULL;
}//End of function

//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<<temp->data<<" ";
temp = temp->next;
}
cout<<temp->data;
}//End of else
}//End of function

//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

Explanation / Answer

I have checked it, code is working fine! I have attached the sample output of the program, you can check it!

Sample Output:

A or a - Create "N" nodes
B or b - Delete from the middle
C or c - Insert in the middle
D or d - Display
Q or q - Quit
Enter your choice: A

Enter the size of Linked List: 4

Enter Data: 1

Enter Data: 2

Enter Data: 3

Enter Data: 4

A or a - Create "N" nodes
B or b - Delete from the middle
C or c - Insert in the middle
D or d - Display
Q or q - Quit
Enter your choice: C

Enter the data to insert:
Enter Data: 5

Enter he position where you want to insert the node: 3

A or a - Create "N" nodes
B or b - Delete from the middle
C or c - Insert in the middle
D or d - Display
Q or q - Quit
Enter your choice: D
1 2 5 3 4
A or a - Create "N" nodes
B or b - Delete from the middle
C or c - Insert in the middle
D or d - Display
Q or q - Quit
Enter your choice: B
Enter position where you want to delete node: 3

Node deleted

A or a - Create "N" nodes
B or b - Delete from the middle
C or c - Insert in the middle
D or d - Display
Q or q - Quit
Enter your choice: D
1 2 3 4
A or a - Create "N" nodes
B or b - Delete from the middle
C or c - Insert in the middle
D or d - Display
Q or q - Quit
Enter your choice: D
1 2 3 4
A or a - Create "N" nodes
B or b - Delete from the middle
C or c - Insert in the middle
D or d - Display
Q or q - Quit
Enter your choice: 2

Invalid choice
A or a - Create "N" nodes
B or b - Delete from the middle
C or c - Insert in the middle
D or d - Display
Q or q - Quit
Enter your choice: D
1 2 3 4
A or a - Create "N" nodes
B or b - Delete from the middle
C or c - Insert in the middle
D or d - Display
Q or q - Quit
Enter your choice: B
Enter position where you want to delete node: 2

Node deleted

A or a - Create "N" nodes
B or b - Delete from the middle
C or c - Insert in the middle
D or d - Display
Q or q - Quit
Enter your choice: D
1 3 4
A or a - Create "N" nodes
B or b - Delete from the middle
C or c - Insert in the middle
D or d - Display
Q or q - Quit
Enter your choice: Q

Code:

#include <iostream>
#include <stdlib.h>
using namespace std;
//Structure to create node
struct LIST_NODE
{
// Data element of node
int data;
void creatlist(int n);
// Pointer element of node
struct LIST_NODE *next;
};
typedef struct LIST_NODE node;
//initialize start pointer to null
node *start = NULL;
//Creates a node
node *getnode()
{
node *newnode;
newnode = (node*)malloc(1000);
cout << " Enter Data: ";
cin >> newnode->data;
newnode->next = NULL;
}//End of function
//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<<temp->data<<" ";
temp = temp->next;
}
cout<<temp->data;
}//End of else
}//End of function
//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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote