Beruflich Dokumente
Kultur Dokumente
//
//
//
//
//
//
LList.h
Link Lists ADT
Created by Tyler Nicholson on 9/27/12.
Copyright (c) 2012 __MyCompanyName__. All rights reserved.
#include <iostream>
#include <string>
using namespace std;
//#ifndef LList
//#define LList
typedef string ElementType;
class LList//LList will have access to the public member variables of Node but
nothing else will unless given access
{
private:
class Node
{
public:
ElementType data;//elementtype of data is string
Node *next;//next is pointer to next data in link list
};
Node *first;//first pointer
Node *last;//last pointer
int size;//will track size of link list
public:
LList();//default constructor
~LList();//destructor
int getSize();
bool emptyList();
void print();
void insert(ElementType newData);
bool search(ElementType d);//return true if item(d)is in the list return false if it is
not in the list
void removeItem(ElementType d); //delete function
};
//#endif
//
//
//
//
//
//
//
main.cpp
Link Lists ADT
Created by Tyler Nicholson on 9/27/12.
Copyright (c) 2012 __MyCompanyName__. All rights reserved.
#include <iostream>
#include <string>
using namespace std;
#include "LList.h"
LList::LList()
{
first=NULL;
last=NULL;
size=0;
}
int LList::getSize()
{
return size;
}
bool LList::emptyList()
{
if(first==NULL)
{
return true;
}
else
{
return false;
}
}
void LList::print()
{
Node *current = first;
while(current != NULL)
{
cout<<current->data<<endl;
current=current->next;
}
}
else
{
return false;
}
current=current->next;
void LList::removeItem(ElementType d)
{
Node *current=first;
if(emptyList())
{
cout<<"Nice try, list is empty.\n";
}
else
{
if(search(d)==true)
{
while(current!=NULL)
{
if(current->data==d)
{
cout<<"DELETED"<<endl;
delete current;
size--;
current=last;
}
current=current->next;
}
}
else
{
cout<<"Item is not in the list!?!?!?!?!?!?!?!?!?"<<endl;
}
}
LList::~LList()
while(current != NULL)
{
current=current->next;//current points to next the second item in the list
delete first;//first gets re-allocated
first = current;//first then points to current which points to the next with
repeat to end of list
}
}
//
//
//
//
//
//
//
LLdriver.cpp
Link Lists ADT
Created by Tyler Nicholson on 9/27/12.
Copyright (c) 2012 __MyCompanyName__. All rights reserved.
#include <iostream>
#include <string>
using namespace std;
#include "LList.h"
int main()
{
cout<<"\nCreating list #1\n";
LList list1;
cout<<"Checking size of list: "<<list1.getSize()<<endl;
cout<<"Is list empty? "<<boolalpha<<list1.emptyList()<<endl;
cout<<"Printing.\n";
list1.print();
cout<<"Finished printing.\n";
cout<<"Inserting.\n";
list1.insert("bacon");
cout<<"Checking size of list: "<<list1.getSize()<<endl;
cout<<"Is list empty? "<<boolalpha<<list1.emptyList()<<endl;
cout<<"Printing.\n";
list1.print();
cout<<"Finished printing.\n";
cout<<"Inserting into non-emtpy list.\n";
list1.insert("cheese");
list1.insert("eggs");
list1.insert("tofu");
cout<<"Checking size of list: "<<list1.getSize()<<endl;
cout<<"Is list empty? "<<boolalpha<<list1.emptyList()<<endl;
cout<<"Printing.\n";
list1.print();
cout<<"Finished printing.\n";
cout<<"What would you like to find in the list?\n";
string d;
cin>>d;
cout<<"The item in the list was found?: "<<boolalpha<<list1.search(d)<<endl;
cout<<"Specify an item to delete: ";
cin>>d;
list1.removeItem(d);
}
return 0;