#ifndef LISTND_H
#define LISTND_H
template <class NODETYPE>
class ListNode
{
friend class List<NODETYPE>;
public:
ListNode (const NODETYPE &);
NODETYPE getData() const;
private:
NODETYPE data;
ListNode *nextPtr;
};
template <class NODETYPE>
ListNode<NODETYPE>::ListNode (const NODETYPE &info)
{
data = info;
nextPtr = 0;
}
template <class NODETYPE>
NODETYPE ListNode<NODETYPE>::getData() const {return data;}
#endif
Связные списки
Вот код трёх файлов:
1) listnd.h
Код:
2) list.h
Код:
#ifndef LIST1_H
#define LIST1_H
#include <iostream.h>
#include <assert.h>
#include "listnd.h"
template <class NODETYPE>
class List
{
public:
List();
~List();
void insertAtFront(const NODETYPE &);
void insertAtBack(const NODETYPE &);
int removeFromFront(NODETYPE &);
int removeFromBack(NODETYPE &);
int isEmpty() const;
void print() const;
private:
ListNode<NODETYPE> *firstPtr;
ListNode<NODETYPE> *lastPtr;
ListNode<NODETYPE> *getNewNode(const NODETYPE &);
};
template <class NODETYPE>
List<NODETYPE>::List() {firstPtr = lastPtr = 0;}
template <class NODETYPE>
List<NODETYPE>::~List()
{
if (!isEmpty())
{
cout<<"Udalenie uzlov..."<<endl;
ListNode<NODETYPE> *currentPtr = firstPtr, *tempPtr;
while (currentPtr != 0)
{
tempPtr = currentPtr;
cout<<tempPtr->data<<endl;
currentPtr = currentPtr->nextPtr;
delete tempPtr;
}
}
cout<<"Vse uzly udaleny"<<endl<<endl;
}
template <class NODETYPE>
void List<NODETYPE>::insertAtFront(const NODETYPE &value)
{
ListNode<NODETYPE> *newPtr = getNewNode(value);
if (isEmpty())
firstPtr = lastPtr = newPtr;
else
{
newPtr->nextPtr = firstPtr;
firstPtr = newPtr;
}
}
template <class NODETYPE>
void List<NODETYPE>::insertAtBack(const NODETYPE &value)
{
ListNode<NODETYPE> *newPtr = getNewNode(value);
if (isEmpty())
firstPtr = lastPtr = newPtr;
else
{
lastPtr->nextPtr = newPtr;
lastPtr = newPtr;
}
}
template <class NODETYPE>
int List<NODETYPE>::removeFromFront(NODETYPE &value)
{
if (isEmpty())
return 0;
else
{
ListNode<NODETYPE> *tempPtr = firstPtr;
if (firstPtr == lastPtr)
firstPtr = lastPtr = 0;
else
firstPtr = firstPtr->nextPtr;
value = tempPtr->data;
delete tempPtr;
return 1;
}
}
template <class NODETYPE>
int List<NODETYPE>::removeFromBack(NODETYPE &value)
{
if (isEmpty())
return 0;
else
{
ListNode<NODETYPE> *tempPtr = lastPtr;
if (firstPtr == lastPtr)
firstPtr = lastPtr = 0;
else
{
ListNode<NODETYPE> *currentPtr = firstPtr;
while (currentPtr->nextPtr != lastPtr)
currentPtr = currentPtr->nextPtr;
lastPtr = currentPtr;
currentPtr->nextPtr = 0;
}
value = tempPtr->data;
delete tempPtr;
return 1;
}
}
template <class NODETYPE>
int List<NODETYPE>::isEmpty() const {return firstPtr == 0;}
template <class NODETYPE>
ListNode<NODETYPE> * List<NODETYPE>::getNewNode(const NODETYPE &value)
{
ListNode<NODETYPE> *ptr = new ListNode<NODETYPE>(value);
assert (ptr != 0);
return ptr;
}
template <class NODETYPE>
void List<NODETYPE>::print() const
{
if (isEmpty())
{
cout<<"Spisok pust"<<endl<<endl;
return;
}
ListNode<NODETYPE> currentPtr = firstPtr;
cout<<"Spisok sostoit iz: ";
while(currentPtr != 0)
{
cout<<currentPtr->data<<' ';
currentPtr = currentPtr->nextPtr;
}
cout<<endl<<endl;
}
#endif
#define LIST1_H
#include <iostream.h>
#include <assert.h>
#include "listnd.h"
template <class NODETYPE>
class List
{
public:
List();
~List();
void insertAtFront(const NODETYPE &);
void insertAtBack(const NODETYPE &);
int removeFromFront(NODETYPE &);
int removeFromBack(NODETYPE &);
int isEmpty() const;
void print() const;
private:
ListNode<NODETYPE> *firstPtr;
ListNode<NODETYPE> *lastPtr;
ListNode<NODETYPE> *getNewNode(const NODETYPE &);
};
template <class NODETYPE>
List<NODETYPE>::List() {firstPtr = lastPtr = 0;}
template <class NODETYPE>
List<NODETYPE>::~List()
{
if (!isEmpty())
{
cout<<"Udalenie uzlov..."<<endl;
ListNode<NODETYPE> *currentPtr = firstPtr, *tempPtr;
while (currentPtr != 0)
{
tempPtr = currentPtr;
cout<<tempPtr->data<<endl;
currentPtr = currentPtr->nextPtr;
delete tempPtr;
}
}
cout<<"Vse uzly udaleny"<<endl<<endl;
}
template <class NODETYPE>
void List<NODETYPE>::insertAtFront(const NODETYPE &value)
{
ListNode<NODETYPE> *newPtr = getNewNode(value);
if (isEmpty())
firstPtr = lastPtr = newPtr;
else
{
newPtr->nextPtr = firstPtr;
firstPtr = newPtr;
}
}
template <class NODETYPE>
void List<NODETYPE>::insertAtBack(const NODETYPE &value)
{
ListNode<NODETYPE> *newPtr = getNewNode(value);
if (isEmpty())
firstPtr = lastPtr = newPtr;
else
{
lastPtr->nextPtr = newPtr;
lastPtr = newPtr;
}
}
template <class NODETYPE>
int List<NODETYPE>::removeFromFront(NODETYPE &value)
{
if (isEmpty())
return 0;
else
{
ListNode<NODETYPE> *tempPtr = firstPtr;
if (firstPtr == lastPtr)
firstPtr = lastPtr = 0;
else
firstPtr = firstPtr->nextPtr;
value = tempPtr->data;
delete tempPtr;
return 1;
}
}
template <class NODETYPE>
int List<NODETYPE>::removeFromBack(NODETYPE &value)
{
if (isEmpty())
return 0;
else
{
ListNode<NODETYPE> *tempPtr = lastPtr;
if (firstPtr == lastPtr)
firstPtr = lastPtr = 0;
else
{
ListNode<NODETYPE> *currentPtr = firstPtr;
while (currentPtr->nextPtr != lastPtr)
currentPtr = currentPtr->nextPtr;
lastPtr = currentPtr;
currentPtr->nextPtr = 0;
}
value = tempPtr->data;
delete tempPtr;
return 1;
}
}
template <class NODETYPE>
int List<NODETYPE>::isEmpty() const {return firstPtr == 0;}
template <class NODETYPE>
ListNode<NODETYPE> * List<NODETYPE>::getNewNode(const NODETYPE &value)
{
ListNode<NODETYPE> *ptr = new ListNode<NODETYPE>(value);
assert (ptr != 0);
return ptr;
}
template <class NODETYPE>
void List<NODETYPE>::print() const
{
if (isEmpty())
{
cout<<"Spisok pust"<<endl<<endl;
return;
}
ListNode<NODETYPE> currentPtr = firstPtr;
cout<<"Spisok sostoit iz: ";
while(currentPtr != 0)
{
cout<<currentPtr->data<<' ';
currentPtr = currentPtr->nextPtr;
}
cout<<endl<<endl;
}
#endif
3) driver.cpp
Код:
#include <iostream.h>
#include "list.h"
void instructions();
void testIntegers();
void main()
{
testIntegers();
}
void instructions()
{
cout<<"Choose:"<<endl
<<"1 - vstavit' v na4alo spiska"<<endl
<<"2 - vstavit' v konec spiska"<<endl
<<"3 - udalit' iz na4ala spiska"<<endl
<<"4 - udalit' iz konca spiska"<<endl
<<"5 - zaver6it' obrabotku spiska"<<endl;
}
void testIntegers()
{
cout<<"Proverka spiska celyh 4isel"<<endl;
List<int> integerList;
instructions();
int choice, value;
do
{
cout<<"?";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Vvedite 4islo: ";
cin>>value;
integerList.insertAtFront(value);
integerList.print();
break;
case 2:
cout<<"Vvedite 4islo: ";
cin>>value;
integerList.insertAtBack(value);
integerList.print();
break;
case 3:
if (integerList.removeFromFront(value))
cout<<value<<" udalyaetsya iz spiska"<<endl;
else cout<<"Spisok pust"<<endl;
integerList.print();
break;
case 4:
if (integerList.removeFromBack(value))
cout<<value<<" udalyaetsya iz spiska"<<endl;
else cout<<"Spisok pust"<<endl;
integerList.print();
break;
}
} while (choice != 5);
cout<<"Konec proverki spiska celyh 4isel"<<endl;
}
#include "list.h"
void instructions();
void testIntegers();
void main()
{
testIntegers();
}
void instructions()
{
cout<<"Choose:"<<endl
<<"1 - vstavit' v na4alo spiska"<<endl
<<"2 - vstavit' v konec spiska"<<endl
<<"3 - udalit' iz na4ala spiska"<<endl
<<"4 - udalit' iz konca spiska"<<endl
<<"5 - zaver6it' obrabotku spiska"<<endl;
}
void testIntegers()
{
cout<<"Proverka spiska celyh 4isel"<<endl;
List<int> integerList;
instructions();
int choice, value;
do
{
cout<<"?";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Vvedite 4islo: ";
cin>>value;
integerList.insertAtFront(value);
integerList.print();
break;
case 2:
cout<<"Vvedite 4islo: ";
cin>>value;
integerList.insertAtBack(value);
integerList.print();
break;
case 3:
if (integerList.removeFromFront(value))
cout<<value<<" udalyaetsya iz spiska"<<endl;
else cout<<"Spisok pust"<<endl;
integerList.print();
break;
case 4:
if (integerList.removeFromBack(value))
cout<<value<<" udalyaetsya iz spiska"<<endl;
else cout<<"Spisok pust"<<endl;
integerList.print();
break;
}
} while (choice != 5);
cout<<"Konec proverki spiska celyh 4isel"<<endl;
}