Urgent help! Please modify the following codes to reflect the assignment. It is
ID: 3859551 • Letter: U
Question
Urgent help! Please modify the following codes to reflect the assignment. It is mostly missing comments, menu-function, and a few other things that are detailed in the assignment.
The following are the code files:
1.LinkedList.h
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include<iostream>
struct node {
int item;
struct node *next;
};
class LinkedList{
private:
struct node *head;
public:
LinkedList(void);
void append(int data);
int insert_into_list(int data,int pos);
int delete_node(int pos);
void reverse_list();
int search_list(int data);
void print();
~LinkedList();
};
#endif
2. LinkedList.cpp
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include "linked_list.h"
using namespace std;
LinkedList:: LinkedList(void){
head=NULL;
}
void LinkedList:: append(int data){
struct node *tmp, *p;
tmp = (struct node *)malloc(sizeof(struct node));
tmp->item = data;
tmp->next = NULL;
if(head==NULL){
head=tmp;
return;
}
p=head;
while(p->next!=NULL)
p=p->next;
p->next=tmp;
return;
}
int LinkedList:: insert_into_list(int data,int pos){
struct node *tmp, *p, *tmp1;
int i;
tmp = (struct node *)malloc(sizeof(struct node));
tmp->item = data;
tmp->next = NULL;
if(pos==0){
tmp->next=head;
head=tmp;
return 0;
}
p=head;
for(i=1;i<pos&&p!=NULL;i++)
p=p->next;
if(p==NULL)
return -1;
tmp1=p->next;
p->next=tmp;
tmp->next=tmp1;
return 0;
}
int LinkedList::delete_node(int pos){
int i;
struct node *tmp = (struct node *)malloc(sizeof(struct node)), *p;
if(head==NULL)
return -1;
if(pos==0&&head!=NULL){
tmp=head;
head=head->next;
}
p=head;
for(i=1;i<pos&&p!=NULL;i++)
p=p->next;
if(p==NULL||p->next==NULL)
return -1;
tmp=p->next;
p->next=tmp->next;
free(tmp);
return 0;
}
void LinkedList:: reverse_list(){
struct node *prev, *p, *n,*tmp;
if(head==NULL)
return;
prev=NULL;
p=head;
n=p->next;
while(n!=NULL){
p->next=prev;
prev=p;
p=n;
n=n->next;
}
p->next=prev;
head=p;
return;
}
int LinkedList:: search_list(int data){
struct node *p=head;
int i=0;
while(p!=NULL){
if((p->item)==data)
return i;
p=p->next;
i++;
}
return -1;
}
void LinkedList:: print(){
struct node *p=head;
while(p!=NULL){
cout<<p->item<<" ";
p=p->next;
}
cout<<endl;
}
LinkedList:: ~LinkedList(){
struct node *tmp;
while(head!=NULL){
tmp=head;
head=head->next;
free(tmp);
}
free(head);
}
3. main()
#include<iostream>
#include "LinkedList.cpp"
int main(){
LinkedList l1;
l1.append(1);
l1.append(2);
l1.append(3);
l1.append(4);
l1.append(5);
l1.print();
l1.insert_into_list(8,0);
l1.print();
l1.delete_node(3);
l1.print();
l1.reverse_list();
l1.print();
cout<<l1.search_list(8)<<endl;
}
The output should be similar to that in the link: https://www.screencast.com/t/w4OeB05A
THE REQUIRED ASSIGNMENT WAS:
Design a linked list class to hold a series of integers. The class should have the following member functions:
append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges the nodes in the list so that their order is reversed), and search (which returns the position of a specific value in the list. Assume the first node is position 0. If the items is not found, then return a -1 which will tell the calling function that that value does not exist)
High level validation: Numerical data input from the user should be validated before sending to the mutator function. While a non-integer is entered, display error and get data.
Low level validation: Mutator functions which are told to access a node that doesn't exist should return a -1 so that the calling function can handle the problem.
Create a menu-driven program to demonstrate the linked list capabilities. Be sure to have proper constructors and destructors (a destructor must delete every node). Make your input and output professional. Break your code down into functions so as to maintain modular design. No global variables.
Please please please separate all files and FOLLOW the assignment requirement. Submit .cpp and .h files.
Demo your program as follows:
append node
append node
append node
append node
append node
print list
insert at position
print list
delete at position
print list
reverse
print list
search list
Explanation / Answer
1.LinkedList.h
#ifndef LINKED_LIST_H
#define LINKED_LIST_H
#include<iostream>
struct node {
int item;
struct node *next;
};
class LinkedList{
private:
struct node *head;
public:
LinkedList(void);
void append(int data);
int insert_into_list(int data,int pos);
int delete_node(int pos);
void reverse_list();
int search_list(int data);
void print();
~LinkedList();
};
#endif
2. LinkedList.cpp
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include "linked_list.h"
using namespace std;
LinkedList:: LinkedList(void){
head=NULL;
}
void LinkedList:: append(int data){
struct node *tmp, *p;
tmp = (struct node *)malloc(sizeof(struct node));
tmp->item = data;
tmp->next = NULL;
if(head==NULL){
head=tmp;
return;
}
p=head;
while(p->next!=NULL)
p=p->next;
p->next=tmp;
return;
}
int LinkedList:: insert_into_list(int data,int pos){
struct node *tmp, *p, *tmp1;
int i;
tmp = (struct node *)malloc(sizeof(struct node));
tmp->item = data;
tmp->next = NULL;
if(pos==0){
tmp->next=head;
head=tmp;
return 0;
}
p=head;
for(i=1;i<pos&&p!=NULL;i++)
p=p->next;
if(p==NULL)
return -1;
tmp1=p->next;
p->next=tmp;
tmp->next=tmp1;
return 0;
}
int LinkedList::delete_node(int pos){
int i;
struct node *tmp = (struct node *)malloc(sizeof(struct node)), *p;
if(head==NULL)
return -1;
if(pos==0&&head!=NULL){
tmp=head;
head=head->next;
}
p=head;
for(i=1;i<pos&&p!=NULL;i++)
p=p->next;
if(p==NULL||p->next==NULL)
return -1;
tmp=p->next;
p->next=tmp->next;
free(tmp);
return 0;
}
void LinkedList:: reverse_list(){
struct node *prev, *p, *n,*tmp;
if(head==NULL)
return;
prev=NULL;
p=head;
n=p->next;
while(n!=NULL){
p->next=prev;
prev=p;
p=n;
n=n->next;
}
p->next=prev;
head=p;
return;
}
int LinkedList:: search_list(int data){
struct node *p=head;
int i=0;
while(p!=NULL){
if((p->item)==data)
return i;
p=p->next;
i++;
}
return -1;
}
void LinkedList:: print(){
struct node *p=head;
while(p!=NULL){
cout<<p->item<<" ";
p=p->next;
}
cout<<endl;
}
LinkedList:: ~LinkedList(){
struct node *tmp;
while(head!=NULL){
tmp=head;
head=head->next;
free(tmp);
}
free(head);
}
3. main()
#include<iostream>
#include "LinkedList.cpp"
int main(){
LinkedList l1;
l1.append(1);
l1.append(2);
l1.append(3);
l1.append(4);
l1.append(5);
l1.print();
l1.insert_into_list(8,0);
l1.print();
l1.delete_node(3);
l1.print();
l1.reverse_list();
l1.print();
cout<<l1.search_list(8)<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.