Question

In: Computer Science

Example code of inserting nodes, deleting nodes, and deleting specific nodes in a Linked List in...

Example code of inserting nodes, deleting nodes, and deleting specific nodes in a Linked List in C++?

How do I create nodes? explain each line of code pleasee

Solutions

Expert Solution

#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;

// Class List definition
class List
{
public:
// Data member to store data
string name;
// Pointer to point to next node
List *next;

};// End of class

// Declares a start pointer and assigns null to it
List *start = NULL;

// Function to add a name
void addName()
{
string name;
// Creates a temporary pointer
List *temp = new List();

// Checks if temp is null then list is full
if(temp==NULL)
{
cout<<"\nOut of Memory Space:\n";
return;
}// End of if condition

cout<<"\n Enter a name to add: ";
cin>>temp->name;
// Assigns null to temp next
temp->next = NULL;
// Checks if the start is NULL then it is the first node to insert
if(start == NULL)
{
// temp address is assigned to start
start = temp;
}// End of if condition
// Otherwise
else
{
// start address is assigned to temp next
temp->next = start;
// temp address is assigned to start
start = temp;
}// End of else
}// End of function

// Function to display information
void displayNames()
{
// Creates a temporary pointer
List *ptr;
// Checks if start is null then list is empty
if(start == NULL)
{
cout<<"\nList is empty:\n";
return;
}// End of if condition
else
{
// Points to starting name
ptr = start;
cout<<"\n List of names \n";
// Loops till end of the list
while(ptr != NULL)
{
cout<<"\n Name: "<<ptr->name;
// Move to next name
ptr = ptr->next ;
}// End of while loop
}// End of else
}// End of function

// Function to search a name in the list
int searchName(string name)
{
int position = -1, counter = 0;
// Creates a temporary pointers
List *ptr;
// Checks if start is null then list is empty
if(start==NULL)
{
cout<<"\nThe List is Empty:\n";
return -1;
}// End of if condition

// Otherwise
else
{
// Points to starting name
ptr = start;
// Loops till end of the list
while(ptr != NULL)
{
// Checks if current name number is equals to parameter name
if(ptr->name.compare(name) == 0)
{
cout<<"\n Name "<<name<<" available.";
// Assigns the found position
position = counter;
// Returns the position
return counter;
}// End of if condition
// Move to next name
ptr=ptr->next ;
// Increase the record counter
counter++;
}// End of while loop
}// End of else

// Checks if found position is -1 name not found
if(position == -1)
cout<<"\n Name "<<name<<" not available.";

// returns the position
return position;
}// End of function

// Function to delete a node at specified position
void deleteName()
{
int position;
string name;
// Creates a temporary pointer
List *temp,*ptr;
// Checks if start is null then list is empty
if(start==NULL)
{
cout<<"\nThe List is Empty:\n";
return;
}// End of if condition
// Otherwise
else
{
// Accepts the name
cout<<"\nEnter the name: \t";
cin>>name;

// Calls the function to search the name
// Stores the returned found position
position = searchName(name);

// Checks if found position is -1 name not found
if(position == -1)
{
cout<<"\n No such "<<name<<" found to delete.";
return;
}// End of if condition

// Checks if the position is zero
if(position == 0)
{
// Temporary pointer points start
ptr = start;
// Start pointing to next node
start = start->next;
cout<<"\n "<<name<<" deleted from list.";
// Deletes the pointer
free(ptr);
}// End of if condition

// Otherwise
else
{
// Temporary pointer points start
ptr = start;

// Loops till record found position
for(int c = 0; c < position; c++)
{
// temporary pointer pointing to previous node ptr
temp = ptr;
// Move to next name
ptr = ptr->next;

// Checks if ptr is null node not found
if(ptr == NULL)
{
cout<<"\n Name: "<<name<<" not found in the list.\n";
return;
}// End of if condition
}// End of for loop
temp->next = ptr->next ;
cout<<"\n The deleted name: "<<name;

// Deletes the name node
free(ptr);
}// End of inner else
}// End of outer else
}// End of function

// Function to display user choice and return user choice
int menu()
{
// To store user choice
int choice;
// Displays menu

cout<<"\n LIST MENU \n";
cout<<"---------------------------------------\n";
cout<<"\n 1. Add Name.";
cout<<"\n 2. Delete name.";
cout<<"\n 3. Search name.";
cout<<"\n 4. Display all names.";
cout<<"\n 5. Quit";
cout<<"\n--------------------------------------\n";
// Accepts user choice
cout<<"Enter your choice:\t";
cin>>choice;
// returns user choice
return choice;
}// End of function

// main function definition
int main()
{
string name;
// Loops till user choice is not 5
while(1)
{
// Calls the function to display menu.
// Calls the function as per user choice returned by the function
switch(menu())
{
case 1:
addName();
break;
case 2:
deleteName();
break;
case 3:
cout<<"\n Enter a name to search: ";
cin>>name;
searchName(name);
break;
case 4:
displayNames();
break;
case 5:
exit(0);
default:
cout<<"\n Wrong Choice:\n";
}//end of switch - case
}// End of while loop
return 0;
}//end of main function

Sample Output:

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 2

The List is Empty:

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 3

Enter a name to search: 4

The List is Empty:

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 1

Enter a name to add: Pyari

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 4

List of names

Name: Pyari
LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 1

Enter a name to add: Ram

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 4

List of names

Name: Ram
Name: Pyari
LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 1

Enter a name to add: Manvi

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 4

List of names

Name: Manvi
Name: Ram
Name: Pyari
LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 1

Enter a name to add: Tanvi

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 4

List of names

Name: Tanvi
Name: Manvi
Name: Ram
Name: Pyari
LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 2

Enter the name: Sunita

Name Sunita not available.
No such Sunita found to delete.
LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 2

Enter the name: Ram

Name Ram available.
The deleted name: Ram
LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 4

List of names

Name: Tanvi
Name: Manvi
Name: Pyari
LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 3

Enter a name to search: Manvi

Name Manvi available.
LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 3

Enter a name to search: Tarun

Name Tarun not available.
LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 9

Wrong Choice:

LIST MENU
---------------------------------------

1. Add Name.
2. Delete name.
3. Search name.
4. Display all names.
5. Quit
--------------------------------------
Enter your choice: 5


Related Solutions

1. Considering singly linked list, be able to determine if inserting nodes at the end of...
1. Considering singly linked list, be able to determine if inserting nodes at the end of a LinkedList is less time-efficient than inserting them at the front of the list. 2. Be able to differentiate between the data structures Stack, Queue, and Linked List. 3. Determine between the acronyms LIFO and FIFO, and be able to give one real life example where each is applicable
The purpose of this lab is to become familiar with searching, inserting and deleting elements in...
The purpose of this lab is to become familiar with searching, inserting and deleting elements in a linked list.using java   public boolean contains(String name) // returns true if provided name exists in the list, otherwise returns false public void add(String name) // adds name to the list in its proper lexographical (alphabetical) ordering public void delete(String name) // removes name from list
Write the following algorithms for a Doubly Linked List Inserting an item                              
Write the following algorithms for a Doubly Linked List Inserting an item                                                                                                                              [7] Deleting an item                                                                                                                               [7] Question two Take a queue containing numbers 10, 15, 5, 25, 30 in which 30 has been inserted first. After performing the following operations, what would be the contents of the queue? Delete two elements                                                                                                                      [2] Insert 7 and then 20                                                                                                                        [2] Delete an element                                                                                                                          [2]
Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
Method in C language for inserting in descending order in a single linked list. It has...
Method in C language for inserting in descending order in a single linked list. It has a head and tail variable.
In C++, Write a function to reverse the nodes in a linked list. You should not...
In C++, Write a function to reverse the nodes in a linked list. You should not create new nodes when you reverse the the linked list. The function prototype:          void reverse(Node*& head); Use the following Node definition: struct Node {    int data;    Node *next; }
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse...
/* *fix the below C Program to Display the Nodes of a Linked List in Reverse */ #include <stdio.h> #include <stdlib.h> struct node { int visited; int a; struct node *next; }; int main() { struct node *head = NULL; generate(head); printf("\nPrinting the list in linear order\n"); linear(head); printf("\nPrinting the list in reverse order\n"); display(head); delete(head); return 0; } void display(struct node *head) { struct node *temp = head, *prev = head; while (temp->visited == 0) { while (temp->next !=...
Write down a C program which will create a list (simple linear linked list) of nodes....
Write down a C program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a grade-point average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function you wrote...
1.Please write a C++ program that counts the nodes in a linked list with the first...
1.Please write a C++ program that counts the nodes in a linked list with the first node pointed to by first. Also please explain. 2. Write a program to determine the average of a linked list of real numbers with the first node pointed to by first. 3. Determine the computing times of the algorithms in question 1 and 4. Write a program to insert a new node into a linked list with the first node pointed to by first...
PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.
PYTHON: Describe a recursive algorithm that counts the number of nodes in a singly linked list.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT