C++ linked list help! 1. Create a single source file (.cpp or .cc), 2. Implement
ID: 3809724 • Letter: C
Question
C++ linked list help!
1. Create a single source file (.cpp or .cc),
2. Implement a struct called “listNode” before main() to represent each node in the singly-linked list. Use a typedef before main to set the type of the element stored in the struct (set to int for this lab),
3. Write three functions, tested using the instructions below: a. Insert node – takes a pointer to the first element in the list and the item to add as parameters (make sure to use the typedef for the type!). Adds the specified item to the end of the list. b. Delete node – takes as parameters a pointer to the first element in the list, and takes an element (i.e., type defined by the typedef) to delete from the list if found. For simplicity, assume there are no duplicate items in the list. c. Print list – takes a pointer to the first element of the list, prints the entire list from start to end.
Testing:
1.Create nodes with following values: 326 38 27 94 4 8 15
2. Delete node 94
3. Add new node 16(should be added to end)
4.Print the list.
Explanation / Answer
//cpp program for single linked list
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef struct listNode SLL;
struct listNode
{
int data; //integer Data to be stoed inside of node
listNode *next; //Next element of data
}*first;
class singleList
{
public :
void insert();
void delet(int);
void display();
singleList()
{
first=NULL;
}
};
int main()
{
int ch, nodes, data, i;
singleList sll;
first = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Functions on singly linked list"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert node at last"<<endl;
cout<<"2.Delete a Particular Node of found data"<<endl;
cout<<"3.Display Linked List"<<endl;
cout<<"4.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Inserting Node : "<<endl;
sll.insert();
cout<<endl;
break;
case 2:
cout<<"Enter data(int type) to delete that node ? :";
cin>>data;
cout<<"Deleting a particular node of data: "<<endl;
sll.delet(data);
break;
case 3:
cout<<"Display elements of link list"<<endl;
sll.display();
cout<<endl;
break;
case 4:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice Entered"<<endl;
}
}
}
/*
* Inserting Node at last
*/
void singleList::insert()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
listNode *temp, *s;
temp = new(listNode);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
}
else
{
temp->data = value;
temp->next = NULL;
}
s = first;
if(first==NULL)
{
first=temp;
}
else
{
while (s->next != NULL)
{
s = s->next;
}
s->next = temp;
}
cout<<"Element Inserted "<<endl;
}
/*
* Delete node has data entered by user
*/
void singleList::delet(int dt)
{
int i, counter = 0;
listNode *s, *ptr;
s =first;
if (s == NULL)
{
cout<<"List is empty.Can't delete any node."<<endl;
}
if (s->data==dt)
{
first = s->next;
free(s);
cout<<"Element Deleted"<<endl;
}
else
{
while (s != NULL&&(s->next->data!=dt))
{
s = s->next;
}
if (s->next->data==dt)
{
ptr=s;
s=s->next;
ptr->next = s->next;
free(s);
cout<<"Element Deleted"<<endl;
}
else
{
cout<<"Position out of range"<<endl;
}
}
}
/*
* Display Data of a link list
*/
void singleList::display()
{
listNode *temp;
temp = first;
if (temp == NULL)
{
cout<<"The List is Empty"<<endl;
}
else
{
cout<<"Data values in the list are: "<<endl;
while (temp != NULL)
{
cout<<temp->data<<" ";
temp = temp->next;
}
cout<<"NULL"<<endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.