线性表 | 栈和队列 | 串数组广义表
//线性表的顺序表示
#define MAXSIZE 100
#define Elemtype int
#define Status bool
#define OK 1
#define ERROR 0
#define OVERFLOW
typedef struct
{
Elemtype *elemt;
int length;
}Sqlist;
Status InitList(Sqlist &L)
{
L.elemt=new Elemtype[MAXSIZE];
if(!L.elemt) exit(OVERFLOW);
L.length=0;
return OK;
}
Status GetElem(Sqlist L,int i,Elemtype &e)
{
if(i<1||i>L.length) return ERROR;
e = L.elemt[i-1];
return OK;
}
int LocateELem(Sqlist L,ELemtype e)
{
for(int i=0;i<L.length;++i)
{
if(L.elemt[i]==e)
{
break;
return i+1;
}
}
return ERROR;
}
Status ListInsert(Sqlist &L,int i,Elemtype e)
{
if(i<1||i>L.length) return ERROR;
if(L.length==MAXSIZE) return ERROR;
for(int j=L.length-1;j>=i-1;--j)
{
L.elemt[j+1]=L.elemt[j];
}
L.elemt[i-1]=e;
L.length++;
return OK;
}
Status ListDelete(Sqlist &L,int i)
{
if(i<1||i>L.length) return ERROR;
for(int j=i-1;j<L.length;++j)
{
L.elemt[j]=L.elemt[j+1];
}
L.length--;
return OK;
}
#define MAXSIZE 100
#define Elemtype int
#define Status bool
#define OK 1
#define ERROR 0
#define OVERFLOW
//线性表的链式表示
typedef struct node
{
Elemtype data;
struct node *next;
}LNode,*LinkList;
Status InitList(LinKList &L)
{
L = new LNode;
L->next=NULL;
return OK;
}
Status GetElem(LinkList L,int i,Elemtype e)
{
LinkList p=L->next;int j=1;
while(p&&j<i)
{
p=p->next;
++j;
}
if(!p||j>i) return ERROR;
e=p->data;
return OK;
}
LNode *LocateElem(LinkList L,Elemtype e)
{
LinkList p=L->next;
while(p&&p->data!=e)
{
p=p->next;
}
return p;
}
Status ListInsert(LinkList &L,int i,Eletype e)
{
LinkList p=L;
int j=0;
while(p&&j<i)
{
p=p->next;
++j;
}
if(!(p->next) || j>(i-1)) return ERROR;
LinkList q=new LNode;
q->data=e;
q->next=p->next;
p->next=q;
return OK;
}
Status ListDelete(LinkList &L,int i)
{
LinkList p=L;int j=0;
while(p&&j<(i-1))
{
p=p->next;
++j;
}
if(!(p->next)||j>(i-1)) return ERROR;
LinkList q=p->next;
p->next=q->next;
delete q;
}
void CreateList_H(LinkList &L,int n)
{
L=new LNode;
L->next=NULL;
for(int i=0;i<n;++i)
{
LinkList p=new LNode;
cin>>p->data;
p->next=L->next;
L->next=p;
}
}
void CreateList_R(LinkList L,int n)
{
L=new LNode;
L->next=NULL;
LinkList r=L;
for(int i=0;i<n;++i)
{
LinkList p=new LNode;
cin>>p->data;
p->next=NULL;
r->next=p;
r=p;
}
}
#define MAXSIZE 100
#define SElemtype int
#define Status bool
#define OK 1
#define ERROR 0
#define OVERFLOW
//栈的顺序表示
typedef struct node
{
SElemtype *base;
SElemtype *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S)
{
S.base=new SElemtype[MAXSIZE];
if(!S.base) exit(OVERFLOW);
S.top=S.base;
S.stacksize=MAXSIZE;
return OK;
}
Status Push(SqStack &S,SElemtype e)
{
if(S.top-S.base==S.stacksize) return ERROR;
*S.top=e;
S.top++;
return OK;
}
Status Pop(SqStack &S,SELemtype &e)
{
if(S.top==S.base) return ERROR;
S.top--;
e=*S.top;
return OK;
}
SElemtype GetTop(SqStack S)
{
if(S.top!=S.base)
return *(S.top-1);
}
#define MAXSIZE 100
#define SElemtype int
#define Status bool
#define OK 1
#define ERROR 0
#define OVERFLOW
//栈的链式表示 (不用设头)
typedef struct node
{
Elemtype data;
struct node *next;
}StackNode,*LinkStack;
Status InitStack(LinkStack &S)
{
S=NULL;
return OK;
}
Status Push(LinkStack &S,SElemType e)
{
LinkStack p=new StackNode;
p->data=e;
p->next=S;
S=p;
return OK;
}
Status Pop(LinkStack &S,SElemtype e)
{
if(S==NULL) return ERROR;
e=S->data;
LinkStack p=S;
S=S->next;
delete p;
return OK;
}
SElemtype GetTop(LinkStack S)
{
if(S!=NULL)
{
return S->data;
}
}
#define MAXSIZE 100
#define QElemtype int
#define Status bool
#define OK 1
#define ERROR 0
#define OVERFLOW
//队列的顺序表示
typedef struct list
{
QElemtype *base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue &Q)
{
Q.base=new QElemtype[MAXSIZE];
if(!Q.base) exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
int QueueLength(SqQueue Q)
{
return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;
}
Status EnQueue(SqQueue &Q,QElemtype e)
{
if((Q.rear+1+MAXSIZE)%MAXSIZE=Q.front)return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXSIZE;
return OK;
}
Status DeQueue(SqQueue &Q,QELemtype &e)
{
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front= (Q.front+1)%MAXSIZE;
return OK;
}
SElemType GetHead(SqQueue Q)
{
if(Q.front!=Q.rear)
return Q.base[Q.front];
}
#define MAXSIZE 100
#define QElemtype int
#define Status bool
#define OK 1
#define ERROR 0
#define OVERFLOW
//队列的链式表示
typedef struct node
{
QElemtype data;
struct node *next;
}QNnode,*QueuePtr;
typedef struct list
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=new QNode;
Q.front->next=NULL;
return OK;
}
Status EnQueue(LinkQueue &Q,QElemtype e)
{
QueuePtr p=new QNode;
p->data=e
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
Status DeQueue(LinkQueue &Q,QElemtype &e)
{
if(Q.front==Q.rear)return ERROR;
LinkQueue p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(p==Q.rear) Q.rear=Q.front;
delete p;
return OK;
}
SElemtype GetHead(LinkQueue Q)
{
if(Q.front != Q.rear)
{
return Q.front->next->data;
}
}
#define MAXSIZE 100
#define QElemtype int
#define Status bool
#define OK 1
#define ERROR 0
#define OVERFLOW
//串的顺序表示
#define LENGTH 10000
typedef struct node
{
char *c[LENGTH];
int length;
};
typedef struct node
{
char c[LENGTH];
int length;
}
//串的链式存储
typedef struct node
{
char ch[LENGTH];
struct node *next;
}Chunk;
typedef struct
{
Chunk *head,*tail;
int length;
}Lstring;