返回列表 发帖

[原创+整理]数据结构的一些笔记

//Header file:linkedStackType.h 链堆栈头文件 #ifndef H_linkedStackType #define H_linkedStackType #include using namespace std; template struct nodeType { Type info; nodeType *link; }; template class linkedStackType { public: const linkedStackType& operator=(const linkedStackType&); void initializeStack(); bool isEmptyStack(); bool isFullStack(); void push(const Type& newItem); void pop(Type& poppedElement); void destroyStack(); linkedStackType(); linkedStackType(const linkedStackType& otherstack); ~linkedStackType(); private: nodeType *top; }; template linkedStackType::linkedStackType() { top=NULL; } template void linkedStackType::destroyStack() { nodeType *temp; while(top!=NULL) { temp=top; top=top->link; delete temp; } } template void linkedStackType::initializeStack() { destroyStack(); } template bool linkedStackType::isEmptyStack() { return(top==NULL); } template bool linkedStackType::isFullStack() { return false; } template void linkedStackType::push(const Type& newElement) { nodeType *newNode; newNode=new nodeType; newNode->info=newElement; newNode->link=top; top=newNode; } template void linkedStackType::pop(Type& poppedElement) { nodeType *temp; poppedElement=top->info; temp=top; top=top->link; delete temp; } template linkedStackType::linkedStackType(const linkedStackType& otherStack) { nodeType *newNode,*current,*last; if(otherStack.top==NULL) top=NULL; else { current=otherStack.top; top=new nodeType; top->info=current->info; top->link=NULL; last=top; current=current->link; while(current!=NULL) { newNode=new nodeType; newNode->info=current->info; newNode->link=NULL; last->link=newNode; last=newNode; current=current->link; } } } template linkedStackType::~linkedStackType() { nodeType *temp; while(top!=NULL) { temp=top; top=top->link; delete temp; } } template const linkedStackType& linkedStackType::operator=(const linkedStackType& otherStack) { nodeType *newNode,*current,*last; if(this!=&otherStack) { if(top!=NULL) destroyStack(); if(otherStack.top==NULL) top=NULL; else { current=otherStack.top; top=new nodeType; top->info=current->info; top->link=NULL; last=top; current=current->link; while(current!=NULL) { newNode=new nodeType; newNode->info=current->info; newNode->link=NULL; last->link=newNode; last=newNode; current=current->link; } } } return *this; } #endif

TOP

[原创+整理]数据结构的一些笔记

//Header file:stackType #ifndef H_StackType #define H_StackType template class stackType { public: const stackType& operator=(const stackType&); void initializeStack(); bool isEmptyStack(); bool isFullStack(); void destroyStack(); void push(const Type& newItem); void pop(Type& poppedItem); stackType(int stackSize=100); stackType(const stackType& otherStack); ~stackType(); private: int maxStackSize; int top; Type *list; }; template void stackType::initializeStack() { top=0; } template bool stackType::isEmptyStack() { return(top==0); } template bool stackType::isFullStack() { return(top==maxStackSize); } template void stackType::destroyStack() { top=0; } template void stackType::push(const Type& newItem) { list[top]=newItem; top++; } template void stackType::pop(Type& poppedItem) { top--; poppedItem=list[top]; } template stackType::stackType(int stackSize) { if(stackSize <=0) { cout<<"The size of the array to hold the stack must be positive."< stackType::~stackType() { top=0; delete [] list; } template const stackType& stackType::operator=(const stackType& otherStack) { int j; if(this !=&otherStack) { if(maxStackSize != otherStack.maxStackSize) cout<<"Cannot copy the two stacks are of different sizes."< stackType::stackType(const stackType& otherStack) { int j; maxStackSize=otherStack.maxStackSize; top=otherStack.top; list=new Type[maxStackSize]; if(top!=0) for(j=0;j

TOP

[原创+整理]数据结构的一些笔记

//Header File:orderedList.h 有序链表的头文件 #ifndef H_orderedListType #define H_orderedListType #include #include"linkedList.h" using namespace std; template class orderedLinkedListType:public linkedListType { public: void search(const Type& item); void insertNode(const Type& newItem); void deleteNode(const Type& deleteItem); void printListReverse() const; private: void reversePrint(nodeType *current) const; }; template void orderedLinkedListType::search(const Type& item) { bool found; nodeType *current; found=false; current=first; if(first==NULL) cout<<"Cannot serach an empty list."<info >=item) found=true; else current=current->link; } if(current==NULL) cout<<"Item is not in the list"<info==item) cout<<"Item is found in the list"< void orderedLinkedListType::insertNode(const Type& newitem) { nodeType *current; nodeType *trailCurrent; nodeType *newNode; bool found; newNode=new nodeType; newNode->info=newitem; newNode->link=NULL; if (first==NULL) first=newNode; else { current=first; found=false; while(current!=NULL && !found) { if(current->info>=newitem) found=true; else { trailCurrent=current; current=current->link; } } if(current==first) { newNode->link=first; first=newNode; } else { trailCurrent->link=newNode; newNode->link=current; } } } template void orderedLinkedListType::deleteNode(const Type&deleteItem) { nodeType *current; nodeType *trailCurrent; bool found; if (first==NULL) cout<<"Cannot delete from an empty list."<info >=deleteItem) found=true; else { trailCurrent=current; current=current->link; } } if (current==NULL) cout<<"Item to be deleted is not in the list."<info==deleteItem) { if (first==current) { first=first->link; delete current; } else { trailCurrent->link=current->link; delete current; } } else cout<<"Item to be deleted is not in the list."< void orderedLinkedListType::reversePrint(nodeType *current) const { if (current!=NULL) { reversePrint(current->link); cout<info<<" "; } } template void orderedLinkedListType::printListReverse() const { reversePrint(first); cout<

TOP

返回列表 回复 发帖