I can\'t add or delete to the back of a linked list. What do I need to do? [code
ID: 3859241 • Letter: I
Question
I can't add or delete to the back of a linked list. What do I need to do?
[code]
#include <iostream>
#include <string>
#include "StringList.h"
using namespace std;
void StringList::insertFront(string aStr) {
listNode *temp = new listNode;
temp->next = ptrHead;
temp->str = aStr; // <--
ptrHead = temp;
}
//Inserting Back
void StringList::insertBack(string aStr) {
listNode *temp = new listNode;
temp->next = ptrTail;
temp->str = aStr; // <--
}
//deleteFront
void StringList::deleteFront() {
if (ptrHead != NULL) {
listNode *temp = ptrHead->next;
delete ptrHead;
ptrHead = temp;
}
}
void StringList::deleteBack() {
if (ptrTail == NULL) {
listNode *temp = ptrTail->next;
delete ptrTail;
ptrTail = temp;
}
}
void StringList::displayList() {
listNode *nodePtr;
nodePtr = ptrHead;
while (nodePtr) {
cout << nodePtr->str << endl;
nodePtr = nodePtr->next;
}
}
header file
[code]
#ifndef STRINGLIST_H_
#define STRINGLIST_H_
#include <iostream>
#include <string>
using namespace std;
class StringList {
private:
struct listNode {
string str;
listNode *next;
};
listNode *ptrHead;
listNode *ptrTail;
string None;
public:
StringList() {
None = "";
ptrHead = ptrTail = NULL;
}
void displayList();
void deleteFront();
void deleteBack();
void insertFront(string aStr);
void insertBack(string aStr);
};
#endif
Explanation / Answer
Answer:
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int info;
struct node *later;
}*begin;
class single_llist
{
public:
node* create_node(int);
void insert_begin();
void insert_pos();
void insert_last();
void delete_pos();
void sort();
void search();
void update();
void reverse();
void display();
single_llist()
{
begin = NULL;
}
};
main()
{
int choice, nodes, element, position, i;
single_llist sl;
begin = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Operations on singly linked list"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert Node at beginning"<<endl;
cout<<"2.Insert node at last"<<endl;
cout<<"3.Insert node at position"<<endl;
cout<<"4.Sort Link List"<<endl;
cout<<"5.Delete a Particular Node"<<endl;
cout<<"6.Update Node Value"<<endl;
cout<<"7.Search Element"<<endl;
cout<<"8.Display Linked List"<<endl;
cout<<"9.Reverse Linked List "<<endl;
cout<<"10.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
sl.insert_begin();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at Last: "<<endl;
sl.insert_last();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at a given position:"<<endl;
sl.insert_pos();
cout<<endl;
break;
case 4:
cout<<"Sort Link List: "<<endl;
sl.sort();
cout<<endl;
break;
case 5:
cout<<"Delete a particular node: "<<endl;
sl.delete_pos();
break;
case 6:
cout<<"Update Node Value:"<<endl;
sl.update();
cout<<endl;
break;
case 7:
cout<<"Search element in Link List: "<<endl;
sl.search();
cout<<endl;
break;
case 8:
cout<<"Display elements of link list"<<endl;
sl.display();
cout<<endl;
break;
case 9:
cout<<"Reverse elements of Link List"<<endl;
sl.reverse();
cout<<endl;
break;
case 10:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}
node *single_llist::create_node(int value)
{
struct node *support, *s;
support = new(struct node);
if (support == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
support->data= value;
support->later = NULL;
return support;
}
}
void single_llist::insert_begin()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *support, *p;
support = create_node(value);
if (begin == NULL)
{
begin = support;
begin->later = NULL;
}
else
{
p = begin;
begin = support;
begin->later = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
void single_llist::insert_last()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *support, *s;
support = create_node(value);
s = begin;
while (s->later != NULL)
{
s = s->later;
}
support->later = NULL;
s->later = support;
cout<<"Element Inserted at last"<<endl;
}
void single_llist::insert_pos()
{
int value, pos, counter = 0;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *support, *s, *input;
support = create_node(value);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
s = begin;
while (s != NULL)
{
s = s->later;
counter++;
}
if (pos == 1)
{
if (begin == NULL)
{
begin = support;
begin->later = NULL;
}
else
{
input = begin;
begin = support;
begin->later = input;
}
}
else if (pos > 1 && pos <= counter)
{
s = begin;
for (i = 1; i < pos; i++)
{
input = s;
s = s->later;
}
input->later = support;
support->later = s;
}
else
{
cout<<"Positon out of range"<<endl;
}
}
void single_llist::sort()
{
struct node *input, *s;
int value;
if (begin == NULL)
{
cout<<"The List is empty"<<endl;
return;
}
input = begin;
while (input != NULL)
{
for (s = input->later;s !=NULL;s = s->later)
{
if (input->data> s->info)
{
value = input->info;
input->data= s->info;
s->data= value;
}
}
input = input->later;
}
}
void single_llist::delete_pos()
{
int pos, i, counter = 0;
if (begin == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
struct node *s, *input;
s = begin;
if (pos == 1)
{
begin = s->later;
}
else
{
while (s != NULL)
{
s = s->later;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = begin;
for (i = 1;i < pos;i++)
{
input = s;
s = s->later;
}
input->later = s->later;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}
void single_llist::update()
{
int value, pos, i;
if (begin == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the node postion to be updated: ";
cin>>pos;
cout<<"Enter the new value: ";
cin>>value;
struct node *s, *input;
s = begin;
if (pos == 1)
{
begin->data= value;
}
else
{
for (i = 0;i < pos - 1;i++)
{
if (s == NULL)
{
cout<<"There are less than "<<pos<<" elements";
return;
}
s = s->later;
}
s->data= value;
}
cout<<"Node Updated"<<endl;
}
void single_llist::search()
{
int value, pos = 0;
bool flag = false;
if (begin == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the value to be searched: ";
cin>>value;
struct node *s;
s = begin;
while (s != NULL)
{
pos++;
if (s->data== value)
{
flag = true;
cout<<"Element "<<value<<" is found at position "<<pos<<endl;
}
s = s->later;
}
if (!flag)
cout<<"Element "<<value<<" not found in the list"<<endl;
}
void single_llist::reverse()
{
struct node *input1, *input2, *input3;
if (begin == NULL)
{
cout<<"List is empty"<<endl;
return;
}
if (begin->later == NULL)
{
return;
}
input1 = begin;
input2 = input1->later;
input3 = input2->later;
input1->later = NULL;
input2->later = input1;
while (input3 != NULL)
{
input1 = input2;
input2 = input3;
input3 = input3->later;
input2->later = input1;
}
begin = input2;
}
void single_llist::display()
{
struct node *support;
if (begin == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
support = begin;
cout<<"Elements of list are: "<<endl;
while (support != NULL)
{
cout<<support->info<<"->";
support = support->later;
}
cout<<"NULL"<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.