I have written the majority of a program that is required for lab, and just need
ID: 3747582 • Letter: I
Question
I have written the majority of a program that is required for lab, and just need some help in finishing it...
LINKEDLISTEXAMPLE.H
#ifndef LINKEDLISTEXAMPLE_H
#define LINKEDLISTEXAMPLE_H
#include "node.h"
using namespace std;
class LinkedList
{
public:
int length;
Node *head, *tail;
Node *curPtr;
LinkedList();
~LinkedList();
void add(string);
void forward();
void back();
void remove(int);
void print();
};
#endif
LINKEDLISTEXAMPLE.CPP
#include "LinkedListExample.h"
using namespace std;
LinkedList::LinkedList(){
this->length = 0;
this->head = NULL;
this->tail = NULL;
}
void LinkedList::add(string website){
Node *temp = new Node;
temp->data = website;
temp->prev = NULL;
temp->next = NULL;
if(head == NULL)
{
head = temp;
tail = temp;
temp = NULL;
curPtr = head;
}
else
{
tail->next = temp;
tail = temp;
curPtr = temp;
}
this->length++;
}
void LinkedList::forward(){
//Need to finish
}
void LinkedList::back(){
//Need to finish
}
void LinkedList::remove(int pos){
//Need to finish
}
void LinkedList::print(){
Node *temp = new Node;
temp = head;
cout << " Oldest" << endl;
cout << "=================" << endl;
while(temp != nullptr){
if(curPtr == temp){
cout << temp->data << " <== Current" << endl;
temp = temp->next;
}
else{
cout << temp->data << endl;
temp = temp->next;
}
}
cout << "=================" << endl;
cout << " Newest" << endl;
}
LinkedList::~LinkedList(){
std::cout << "LIST DELETED";
}
Everything is run from the main currently. The program reads in a file of commands and websites as follows...
and the output should match.....
I haven't been able to figure out how to write the forward, back, and remove functions, so my output currently is
Oldest
=================
http://google.com
http://reddit.com
http://facebook.com
http://myspace.com <== Current
=================
Newest
Oldest
=================
http://google.com
http://reddit.com
http://facebook.com
http://myspace.com
http://bing.com <== Current
=================
Newest
Explanation / Answer
here is the complete working code of kinked list with additional functions
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int data;
struct node *nxt;
}*start;
class single_LL
{
public:
node* creat(int);
void insertion_begin();
void insert_position();
void last_insert();
void del_pos();
void sort();
void search();
void update();
void reverse();
void display();
single_LL()
{
start = NULL;
}
};
main()
{
int ch, nodes, element, pos, i;
single_LL sl;
start = 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 pos"<<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 ch : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
sl.insertion_begin();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at Last: "<<endl;
sl.last_insert();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at a given pos:"<<endl;
sl.insert_position();
cout<<endl;
break;
case 4:
cout<<"Sort Link List: "<<endl;
sl.sort();
cout<<endl;
break;
case 5:
cout<<"Delete a particular node: "<<endl;
sl.del_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 ch"<<endl;
}
}
}
node *single_LL::creat(int val)
{
struct node *tem, *s;
tem = new(struct node);
if (tem == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
tem->data = val;
tem->nxt = NULL;
return tem;
}
}
void single_LL::insertion_begin()
{
int val;
cout<<"Enter the val to be inserted: ";
cin>>val;
struct node *tem, *p;
tem = creat(val);
if (start == NULL)
{
start = tem;
start->nxt = NULL;
}
else
{
p = start;
start = tem;
start->nxt = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
void single_LL::last_insert()
{
int val;
cout<<"Enter the val to be inserted: ";
cin>>val;
struct node *tem, *s;
tem = creat(val);
s = start;
while (s->nxt != NULL)
{
s = s->nxt;
}
tem->nxt = NULL;
s->nxt = tem;
cout<<"Element Inserted at last"<<endl;
}
void single_LL::insert_position()
{
int val, pos, counter = 0;
cout<<"Enter the val to be inserted: ";
cin>>val;
struct node *tem, *s, *ptr;
tem = creat(val);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->nxt;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = tem;
start->nxt = NULL;
}
else
{
pointr = start;
start = tem;
start->nxt = pointr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
pointr = s;
s = s->nxt;
}
pointr->nxt = tem;
tem->nxt = s;
}
else
{
cout<<"Positon out of range"<<endl;
}
}
void single_LL::sort()
{
struct node *pointr, *s;
int val;
if (start == NULL)
{
cout<<"The List is empty"<<endl;
return;
}
pointr = start;
while (pointr != NULL)
{
for (s = pointr->nxt;s !=NULL;s = s->nxt)
{
if (pointr->data > s->data)
{
val = pointr->data;
pointr->data = s->data;
s->data = val;
}
}
pointr = pointr->nxt;
}
}
void single_LL::del_pos()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the pos of val to be deleted: ";
cin>>pos;
struct node *s, *pointr;
s = start;
if (pos == 1)
{
start = s->nxt;
}
else
{
while (s != NULL)
{
s = s->nxt;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
pointr = s;
s = s->nxt;
}
pointr->nxt = s->nxt;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}
void single_LL::update()
{
int val, pos, i;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the node postion to be updated: ";
cin>>pos;
cout<<"Enter the new val: ";
cin>>val;
struct node *s, *pointr;
s = start;
if (pos == 1)
{
start->data = val;
}
else
{
for (i = 0;i < pos - 1;i++)
{
if (s == NULL)
{
cout<<"There are less than "<<pos<<" elements";
return;
}
s = s->nxt;
}
s->data = val;
}
cout<<"Node Updated"<<endl;
}
void single_LL::search()
{
int val, pos = 0;
bool flag = false;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the val to be searched: ";
cin>>val;
struct node *s;
s = start;
while (s != NULL)
{
pos++;
if (s->data == val)
{
flag = true;
cout<<"Element "<<val<<" is found at pos "<<pos<<endl;
}
s = s->nxt;
}
if (!flag)
cout<<"Element "<<val<<" not found in the list"<<endl;
}
void single_LL::reverse()
{
struct node *ptr1, *ptr2, *ptr3;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
if (start->nxt == NULL)
{
return;
}
ptr1 = start;
ptr2 = ptr1->nxt;
ptr3 = ptr2->nxt;
ptr1->nxt = NULL;
ptr2->nxt = ptr1;
while (ptr3 != NULL)
{
ptr1 = ptr2;
ptr2 = ptr3;
ptr3 = ptr3->nxt;
ptr2->nxt = ptr1;
}
start = ptr2;
}
void single_LL::display()
{
struct node *tem;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
tem = start;
cout<<"Elements of list are: "<<endl;
while (tem != NULL)
{
cout<<tem->data<<"->";
tem = tem->nxt;
}
cout<<"NULL"<<endl;
}