Linked List . Design your own linked list class to hold a series of integers . –
ID: 3930991 • Letter: L
Question
Linked List.
Design your own linked list class to hold a series of integers.
– The class should have the following member functions:
• Append nodes
Add a new node to the end of the list
• Inserte nodes
Insert a new node before the node whose value is greater or equal to the value of the new node
• Delete nodes
Delete the node that has a specific value
• Search nodes
Return the position of the node that has a specific value in the linked list
• Print the list
Display all the values in the linked list
• Copy the list
Make a copy of the list
• Destroy the list
Delete the list
– Demonstrate the class with a C++ program
Explanation / Answer
#include<iostream>
#include <stdlib.h>
using namespace std;
struct node
{
int data;
struct node *next;
};
/*Class SinglyLinkedList Declaration*/
class SinglyLinkedList
{
/*creating start pointer to indicate the head position of linked list */
struct node * start;
public:
//Add a new node to the end of the list
void AppendNode(int);
/*Insert a new node before the node whose value is greater or equal to the value of the new node*/
void InsertBeforeGREValue(int);
//void DeleteAtNode();
void PrintList();
bool Search(int);
void CopyList();
void DestroyList();
SinglyLinkedList()
{
start = NULL;
}
};
void SinglyLinkedList::CopyList(struct node *newStart)
{
newStart=start;
}
void SinglyLinkedList::DestroyList()
{
struct node *temp,*t = start->next;
free(start);
while (t != NULL)
{
free(temp);
temp = temp->next;
t=temp;
}
cout<<"NULL"<<endl;
}
//Add a new node to the end of the list
void SinglyLinkedList::AppendNode(int value)
{
struct node *temp, *s;
/* creating a new node to hold value*/
temp = new(struct node);
temp->data = value;
temp->next = NULL;
/* If no start node till now, this node is made as start node*/
if (start == NULL)
{
start = temp;
start->next = NULL;
Display();
}
/* if start node exists, insert new node at end of list*/
else
{
s = start;
/* locating end of list*/
while (s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
/* attaching new node */
s->next = temp;
cout<<"Element Inserted"<<endl;
PrintList();
}
}
/*Insert a new node before the node whose value is greater or equal to the value of the new node*/
void SinglyLinkedList::InsertBeforeGREValue(int value)
{
struct node *temp, *s;
/* creating a new node to hold value*/
temp = new(struct node);
temp->data = value;
temp->next = NULL;
/* If no start node till now, this node is made as start node*/
if (start == NULL)
{
start = temp;
start->next = NULL;
Display();
}
/* if start node exists, insert new node at end of list*/
else
{
if(start->data>=value)
{
s=start;
start=temp;
start->next=s;
}
else
{
s = start;
/* locating end of list*/
while (s->data < value && s->next != NULL)
{
s = s->next;
}
temp->next = NULL;
/* attaching new node */
s->next = temp;
cout<<"Element Inserted"<<endl;
PrintList();
}
}
}
/*To Display Elements of the linked list*/
void SinglyLinkedList::PrintList()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
}
else
{
temp = start;
cout<<endl<<"Linked List"<<endl;
//traversing the list and prints data
while (temp != NULL)
{
cout<<temp->data<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
}
/*To search an element in linked list*/
bool SinglyLinkedList::Search(int value)
{
struct node *temp;
bool flag=false;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
}
else
{
temp = start;
//traverse the list
while (temp != NULL)
{
//if match occurs
if(temp->data==value)
{
flag=true;
break;
}
temp = temp->next;
}
return flag;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.