CS2401 Lab Assignment 6


Category: You will Instantly receive a download link for .zip solution file upon Payment


5/5 - (2 votes)

Begin by making a new directory for this lab, and then open a file called, list1.h. Into this file type the following code:

struct Node{
std::string data;
Node *link;

class Lilist{
Lilist(){head = NULL;}
void add(std::string item);
void show();
Node *head;

void Lilist::add(std::string item){
Node * tmp;
if(head == NULL){
head = new Node;
head -> data = item;
head -> link = NULL;
for(tmp = head; tmp->link != NULL; tmp = tmp -> link)
; // this loop simply advances the pointer to last node in
//the list
tmp ->link = new Node;
tmp = tmp->link;
tmp->data = item;
tmp->link = NULL;

void Lilist::show(){
for(Node *tmp = head; tmp != NULL; tmp = tmp->link)
std::cout<data<<" "; } I know that you could just copy-paste, but I’m hoping that you will learn how this works by typing in the code yourself. Now write a main that looks like this in a file called main.cc #include
#include “list1.h”
using namespace std;

int main(){
Lilist L1, L2;
string target;
cout<<"Now showing list One:\n"; L1.show(); // END OF PART ONE // The code from here down requires that you add two functions to //the class /* cout<<”Enter a name to search:”; cin>>target;
if(L1.search(target) != NULL)
cout<<”That name is stored at address: “<data == target) return cursor;
Then write a move_front_to_back function. The key here is to use an extra pointer to hold the first node in the list, then move the head to the second node of the list, and then with still another pointer find the last node in the list and hook the node that used to be at the front to the back. (Look at the code you’ve written for adding nodes.) Uncomment the parts of main

When you are done run the program with a script file:
script myresults
This file will show the list printed both in its original order and in the order with the first two names moved to the back of the list. Submit this script file along with your two source code files to Blackboard.