Design and Implement a single linked list with the following operations. a- Crea
ID: 3810415 • 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
I wrote functions for a,b,c but I don't know how to set up my main to print and execute everything.
#include <iostream>
#include <stdlib.h>
using namespace std;
struct LIST_NODE
{
int data; // data element of node
void creatlist(int n);
struct LIST_NODE *next; // pointer element of node
};
typedef struct LIST_NODE node;
node *start = NULL;//initialize start pointer to null
node *getnode()
{
node *newnode;
newnode = (node*)malloc(1000);
cout << " Enter Data";
cin >> newnode->data;
newnode->next = NULL;
}
int countnode()
{
node *temp;
int length = 0;
temp = start;
while (temp != NULL)
{
length++;
temp = temp->next;
}
cout << " Length of Linked List : ";
cin>>length;
}
void LIST_NODE::creatlist(int n)
{
int i;
node *newnode;
node * temp;
for (i = 0; i < n; i++)
{
newnode = getnode();
if (start == NULL)
{
start = newnode;
}
else
{
temp = start;
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;
}
}
}
void insert_in_the_middle()
{
node *newnode, *temp, *prev;
int pos, nodectr;
int ctr = 1;
newnode = getnode();
cout << " Enter he position where you want to delete the node ";
cin >> pos;
nodectr = countnode();
if (pos > 1 && pos < nodectr)
{
temp = prev = start;
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 ";
}
}
void delete_node_in_middle()
{
int ctr = 1;
int pos, nodectr;
node *temp, *prev;
if (start == NULL)
{
cout << " Empty list";
return;
}
else
{
cout << "Enter position where you want to delete node";
cin >> pos;
nodectr = countnode();
if (pos > 1 && pos < nodectr)
{
temp = prev = start;
while (ctr < pos)
{
prev = temp;
temp = temp->next;
ctr++;
}
prev->next = temp->next;
free(temp);
cout << " node deleted ";
}
else
{
cout << " invalid position";
}
}
}
Explanation / Answer
#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
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: 5
Enter Data: 10
Enter Data: 20
Enter Data: 30
Enter Data: 40
Enter Data: 50
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
10 20 30 40 50
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: c
Enter the data to insert:
Enter Data: 44
Enter he position where you want to insert the node: 2
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
10 44 20 30 40 50
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: 55
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
10 44 55 20 30 40 50
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: 4
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
10 44 55 30 40 50
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.