Sie sind auf Seite 1von 7

//

//
//
//
//
//
//

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;
}
}

void LList::insert(ElementType newData)


{
// Insert "bacon" into list
Node *newItem;//allocate new Node
newItem = new (nothrow) Node;//allocate memory and check
if(newItem == 0)
{
cout << "No more room for the list!\n";
exit(1);
}
newItem->data = newData;//what the new information is
newItem->next = NULL;//inserting at end of list
if(emptyList())
{
first = last = newItem;
}
else // already have items in the list
{
last->next = newItem;//add item after current last
last = newItem;//newItem becomes new last item
}
cout << "\nPrinting list in insert\n";
print();
cout << "Done printing list in insert\n";
size++;//increase size when this function is used
}
bool LList::search(ElementType d)
{
Node *current=first;
if(emptyList())
{
cout<<"Nice try, list is empty.\n";
}
else
{
while(current!=NULL)
{
if(current->data==d)
{
return true;
}

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()

cout<<"Destroy!\n";//will print out at end of program


if (!emptyList()) //if the list is not empty...
{
Node *current = first;

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;

Das könnte Ihnen auch gefallen