顺序结构
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#include<stdlib.h>
#include<stdio.h>
#define MAXsize 100
typedef int ElemType
typedef int Status
typedef struct SqList{
int length;
ElemType * elem;
}OrderList;
//init
void InitList(OrderList* l){
//allocate memory and initiate length
l->elem=(OrderList*)malloc(sizeof(ElemType)*MAXsize);
if(l->elem==NULL){
exit(OVERFLOW);
}
l->length=0;
return OK;
}
int isfull(OrderList* l){
return l->length==MAXsize;
}
//get_value by number(index+1) in O(1)
Status GetElem(OrderList* L,int i,ElemType *e){
//i in sense
if(i>L->length||i<1)
return ERROR;
//get
*e=L->elem[i-1];
return OK;
}
//search in O(N)
int LocateElem(OrderList* l,ElemType e){
for(int i=0;i<l->length;i++){
if(l->elem[i]==e){
return i+1;
}
return ERROR;
}
}
//insert in O(N)
Status ListInsert(OrderList* l,ElemType e,int i){
//i in sense
if(i<1||i>l->length+1){
return ERROR;
}
//array is not full
if(isfull(l)){
return ERROR;
}
//move
for(int j=l->length-1;j>=i-1;j--){
l->elem[j+1]=l->elem[j];
}
//insert
l->elem[i-1]=e;
l->length++;
return OK;
}
//delete in O(N)
Status ListDelete(OrderList* l,ElemType* e,int i){
//i in sense
if(i<1||i>l->length)
return ERROR;
//elem is not empty
if(l->length==0)
return ERROR;
//move and delete
for(int j=i;j<=l->length-1;j++){
l->elem[j-1]=l->elem[j];
}
//length -1
l->length--;
return OK;
}
链式结构
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#include<stdlib.h>
#include<stdio.h>
#define MAXsize 100
typedef int ElemType
typedef int Status
typedef struct LNode{
ElemType elem;
struct LNode* next;
} LinkList;
//init
Status InitList(LinkList* l){
//allocate memory
l=(LinkList*)malloc(sizeof(LinkList));
//init member
l->next=NULL;
return OK;
}
//get in O(N)
Status GetElem(LinkList* l,int i,ElemType *e){
//first node and counter
LinkList* p=l->next;int ctr=1;
//move p and count
while(p!=NULL&&ctr<i){
p=p->next;
ctr++;
}
//i in sense or have no result
if(p==NULL || ctr>i)
return ERROR;
//get
*e=p->elem;
return OK;
}
//insert in O(N)
Status ListInsert(LinkList* l,ElemType e,int i){
LinkList* p=l->next;
int ctr=1;
//find location
while(p!=NULL&&ctr<i){
p=p->next;
ctr++;
}
//i in sense
if(p==NULL||ctr>=i){
return ERROR;
}
//insert
LinkList* new;
new=(LinkList*)malloc(sizeof(LinkList));
new->next=p->next;
new->data=e;
p->next=new;
return OK;
}
//search in O(N)
LinkList* LocateElem(LinkList* l,ElemType e){
LinkList* p=l->next;
while(p!=NULL&&p->elem!=e){
p=p->next;
}
return p;
}
//delete in O(N)
Status ListDelete(LinkList* l,ElemType* e,int i){
int ctr=1;
LinkList* p=l->next;
//find location
while(p!=NULL&&ctr<i){
p=p->next;
ctr++;
}
if(p==NULL||ctr>=i){
return ERROR;
}
//get elem and pointer
LinkList* t=p->next;
*e=p->next->elem;
//replace
p->next=p->next->next;
free(t);
return OK;
}
c++(顺序结构.h和顺序结构.cpp)
#ifndef List_H
#define List_H
enum bool{false,true};
template<class ElemType>
class List{
public:
List();
~List();
virtual int Size() const=0;//memory size
virtual int Length() const=0;//length
virtual int Search(ElemType &x) const=0;//serach by elem
virtual int Locate(int i)const=0;//search by index+1
virtual bool GetData(int i,ElemType &x)const=0;//get elem by index+1
virtual bool SetData(int i,ElemType &x)=0;//change elem by index+1
virtual bool Insert(int i,ElemType &x)=0;//insert one elem after the index+1 location
virtual bool Remove(int i,ElemType &x)=0;//remove one elem by index+1
virtual bool IsEmpty()const=0;//additive function
virtual bool IsFull()const=0;
virtual void Sort()=0;
virtual void input()=0;
virtual void output()=0;
virtual List<ElemType> operator=(List<ElemType> &l)=0;
};
#endif
//--------------------------------------//
#include<stdlib.h>
#include<iostream>
#include"List.h"
#define MAXsize 100
using namespace std;
template<class T>
class OrderList:public List<T>{
protected:
T *elem;
int capacity;
int rear;
void resize(int newsize);
public:
OrderList(int size=MAXsize);
OrderList(OrderList<T> &l);
~OrderList(){delete []elem;}
int Size()const{return capacity;}
int Length()const{return rear+1;}
int Search(T &x)const;
int Locate(int i)const;
bool GetData(int i,T &x)const{
if(i>0&&i<=rear+1){
x=data[i-1];
return true;
}else{
return false;
}
}
void SetData(int i,T &x){
if(i>0&&i<=rear+1){
data[i-1]=x;
}
}
bool Insert(int i,T &x);
bool Remove(int i,T &x);
bool IsEmpty(){return (rear==-1);}
bool IsFull(){return (rear==MAXsize-1);}
void input();
void output();
OrderList<T> operator = (OrderList<T> &l);
};
template<class T>
OrderList<T>::OrderList(int size){
if(size>0){
capacity=size;
rear=-1;
elem=new T[capacity];
}
if(elem==NULL){
cerr<<"allocate memory error"<<endl;
exit(1);
}
}
template<class T>
OrderList<T>::OrderList(OrderList<T> &l){
capacity=l.capacity;
rear=l.Length()-1;
data=new T[MAXsize];
//
T value;
if(elem==NULL){
cerr<<"allocate memory error"<<endl;
exit(1);
}
for(int i=0;i<=rear;i++){
l.GetData(i,value);elem[i]=value;
}
}
template<class T>
void OrderList<T>::resize(int newsize){
//newsize in sense
if(newsize<=0){
cerr<<"new size not in sense"<<endl;
return;
}
//modify size
if(newsize!=MAXsize){
T *newarray=new T[newsize];
if(newarray==NULL){
cerr<<"allocate memory error"<<endl;
exit(1);
}
//copy elem
int n=rear+1;
T *src=elem;
T *dest=newarray;
while(n != 0){
*dest=*src;
dest++;
src++;
n--;
}
delete []elem;
elem=newarray;MAXsize=newsize;
}
}
template<class T>
int OrderList<T>::Search(T &X)const{
for(int i=0;i<=rear;i++){
if(elem[i]=x){
return i+1;
}
}
return 0;
}
//int Locate(int i)const;
template<class T>
int OrderList<T>::Locate(int i)const{
//i in sense
if(i>=1 && i<=rear+1)
return i;
else
{
return 0;
}
//find location
}
/*
bool Remove(int i,T &x);
bool IsEmpty(){return (rear==-1);}
bool IsFull(){return (rear==MAXsize-1);}
void input();
void output();
OrderList<T> operator = (OrderList<T> &l);
T *elem;
int capacity;
int rear;
void resize(int newsize);
*/
template<class T>
bool OrderList<T>::Insert(int i,T &x){
//list is full
if(isFull()){
return false;
}
//i in sense
if(i<1 || i>rear+1){
return false;
}
//move and insert
for(int j=rear+1;j>=i+1;j--){
elem[j]=elem[j-1];
}
elem[i]=x;
rear++;
return true;
}
template<class T>
bool OrderList<T>::Remove(int i,T &x){
//i in sense
if(i<1 || i>rear+1){
return false;
}
//isempty
if(IsEmpty()){
return false;
}
//move and transmit value and decrease length
x=elem[i-1];
for(int j=i;j<=rear;j++){
elem[j]=elem[j+1];
}
rear--;
return true;
}
template<class T>
void OrderList<T>::input(){
while(true){
cin>>rear;
if(rear<=MAXsize-1){
break;
}
}
for(int i=0;i<=rear;i++){
cin>>elem[i];
cout<<i+1<<endl;
}
}
template<class T>
void OrderList<T>::output(){
for(int i=0;i<=rear;i++){
cout<<i+1<<":"<<elem[i]<<endl;
}
}
template<class T>
OrderList<T> OrderList<T>::operator = (OrderList<T> &l){
if(this==&l){
return l;
}
if(elem!=NULL)
delete []elem;
T value;
elem=new T[l.rear];
if(elem == NULL){
cerr<<"allocate memory error"<<endl;
}
for(int j=0;j<=l.rear;j++){
l.GetData(j,value);elem[j]=value;
}
}
链式存储.cpp和链式存储.h
#ifndef LinkNode_H
#define LinkNode_H
template<class T>
struct LinkNode
{
T elem;
LinkNode<T> *link;
LinkNode(LinkNode<T> *l=NULL){link=l;}
LinkNode(const T &e,LinkNode<T> *l=NULL){link=l;elem=e;}
};
/* default argument has to be set at rear of arg list*/
#endif // !
//.......................................................
#include<iostream>
#include"LinkNode.h"
using namespace std;
template<class T>
class LinkList:public List<T>{
private:
LinkNode<T> *first;
public:
LinkList(){first=new LinkNode<T>;}
LinkList(const T &x){first=new LinkNode<T>(x);}
LinkList(LinkList<T> &l);
~LinkList(){MakeEmpty();}
void MakeEmpty();
int Length()const;
LinkNode<T> *GetHead()const{return first;}
LinkNode<T> *Search(T x);
LinkNode<T> *Locate(int i);
bool GetData(int i,T &x)const;
bool SetData(int i,T &x);
bool Insert(int i,T &x);
bool Remove(int i,T &x);
bool IsEmpty()const{return first->link==NULL?true:false;}
bool IsFull()const{return false;}
void Sort();
void Input();
void Output();
LinkList<T>& operator=(LinkList<T> &l);
};
/* #ifndef LinkNode_H
#define LinkNode_H
template<class T>
struct LinkNode
{
T elem;
LinkNode<T> *link;
LinkNode(LinkNode<T> *l=NULL){link=l;}
LinkNode(const T &e,LinkNode<T> *l=NULL){link=l;elem=e;}
}; */
/* LinkList(LinkList<T> &l);
~LinkList(){MakeEmpty();}
void MakeEmpty();
int Length()const;
LinkNode<T> *GetHead()const{return first;}
LinkNode<T> *Search(T x);
LinkNode<T> *Locate(int i);
bool GetData(int i,T &x)const;
bool SetData(int i,T &x);
bool Insert(int i,T &x);
bool Remove(int i,T &x);
bool IsEmpty()const{return first->link==NULL?true:false;}
bool IsFull()const{return false;}
void Sort();
void Input();
void Output();
LinkList<T>& operator=(LinkList<T> &l); */
/* LinkNode<T> *first; */
template<class T>
LinkList<T>::LinkList(LinkList<T> &l){
//copy constructer
T value;
LinkNode<T> *src=l.GetHead();
LinkNode<T> *dest=first=new LinkNode<T>;
//transmit data
while(src->link!=NULL){
value=src->link->elem;
dest->link=new LinkNode<T>(value,NULL);
src=src->link;
dest=dest->link;
}
}
template<class T>
void LinkList<T>::MakeEmpty(){
LinkNode<T> *p,*t;
while(p->link!=NULL){
t=p->link;
p->link=p->link->link;
delete t;
}
}
template<class T>
int LinkList<T>::Length()const{
LinkNode<T> *p=first;
int ctr=0;
while(p->link!=NULL){
ctr++;
p=p->link;
}
return ctr;
}
/*
template<class T>
LinkList<T>::
*/
template<class T>
LinkNode<T> * LinkList<T>::Search(T x){
LinkNode<T> *p=first;
while(p->link!=NULL){
if(x==p->link->elem){
return p;
}
p=p->link;
}
return p;
}
template<class T>
LinkNode<T> * LinkList<T>::Locate(int i){
if(i<=0||i>Length()){
return NULL;
}
LinkNode<T> *p=first->link;
int ctr=1;
while(p!=NULL){
if(ctr==i){
return p;
}
ctr++;
p=p->link;
}
}
template<class T>
bool LinkList<T>::GetData(int i,T &x)const{
if(i<=0||i>Length()){
return false;
}
if(IsEmpty()){
return false;
}
LinkNode<T> *p=Locate(i);
return true;
}
template<class T>
bool LinkList<T>::SetData(int i,T &x){
if(i<=0||i>Length()){
return false;
}
if(IsEmpty()){
return false;
}
LinkNode<T> *p=Locate(i);
p->elem=x;
return true;
}
/*
template<class T>
LinkList<T>::
*/
/* bool Insert(int i,T &x);
bool Remove(int i,T &x);
bool IsEmpty()const{return first->link==NULL?true:false;}
bool IsFull()const{return false;}
void Sort();
void Input();
void Output();
LinkList<T>& operator=(LinkList<T> &l);
struct LinkNode
{
T elem;
LinkNode<T> *link;
LinkNode(LinkNode<T> *l=NULL){link=l;}
LinkNode(const T &e,LinkNode<T> *l=NULL){link=l;elem=e;}
}; */
template<class T>
bool LinkList<T>::Insert(int i,T &x){
if(i<=0||i>Length()+1){
return false;
}
//get i location
LinkNode<T> *p=Locate(i-1);//insert
LinkNode<T> *newnode=new LinkNode<T>(x,p->link);
p->link=newnode;
return true;
}
template<class T>
bool LinkList<T>::Remove(int i,T &x){
if(i<=0||i>Length()){
return false;
}
if(IsEmpty()){
return false;
}
LinkNode<T> *p=Locate(i-1);
x=p->link->elem;
LinkNode<T> *t;
t=p->link;
p->link=p->link->link;
delete t;
return true;
}