我把数据结构中常用的数据类型定义汇总了一下,可能有些地方会有疏漏, 欢迎指正.
先看下线性表,单链表,栈和队列
//total
#define OVERFLOW -2
#define ERROR 0
#define OK 1
typedef int ElemType;
typedef int status;
//线性表
#define List_Init_Size 100;
#define List_Increment 10;
typedef struct{
ElemType *elem;
int length;
int listsize;
}SqList;
//Initiate
int init_sqlist(SqList &L){
=(ElemType *)malloc(List_Init_Size*sizeof(ElemType));
if (!) exit(OVERFLOW);
=0;
=List_Init_Size;
return OK;
}
//insert
int insert_elem(SqList &L,ElemType e,int i){
int n;
if (i<1||i>-1) then return Error;
if (>=) {
=(ElemType *)realloc(,(listsize+List_Increment)*sizeof(ElemType));
if (!) exit(OVERFLOW);
+=List_Increment;
}
/*
q=&[i-1];
for (p=&[-1];p>=q;--p) *(p+1)=*p;
*q=e;
*/
for (n=-1;n>=i-1;--n){
[n+1]=[n];
}
[i-1]=e;
++;
return OK
};
//delete
int delete_elem(SqlList &L, ElemType &e, int i){
ElemType p,q;
if (i<0 || i>-1) return Error;
q=&[i-1];
e=*q;
for (p=q;p<[-1];++p) *p=*(p+1);
--;
return OK;
}
//Search
int find_elem(SqlList L,ElemType &e,int i){
e=sq
}
//单链表
typedef struct LNode{
Elemtype data;
struct LNode *next;
}LNode,*LinkList;
int init_linklist(LinkList &L){
L=(LinkList)malloc(sizeof(LNode));
=NULL;
return OK;
};
status insert_node(LinkList &L, ElemType e, int i){
int j;
LNode *p,*q;
j=1;
//e=(LNode *)malloc(sizeof(LNode));
p=L;
while (p && j<i){
p=p->next;
++j;
}
if (!p||(i<1)) then return Error;
q=(LinkList)malloc(sizeof(LNode));
q->data=e;
q->next=p->next;
p->next=q;
return OK;
}
Status delete_node(LinkList &L, int i,ElemType e){
LNode *p,*q;
int j; j=1;
p=L;
while (p&&j<i) {
p=p->next;
++j;
};
if (!p||i<1) then return Error;
q=p->next;
p->next=q->next;
e=q->next;
free(q);
return OK;
}
//栈
#define Stack_Init_Size 100
typedef struct{
Elemtype *base;
Elemtype *top;
int stacksize;
}StackList;
int init_statcklist(StackList &S){
=(ElemType *)malloc(Stack_Init_Size*sizeof(ElemType));
if (!) exit(OVERFLOW);
=0;
=;
=Stack_Init_Size;
return OK; //Don't forget
}
status push(StackList &S, ElemType e){
//=+1;
if ()>= then {
=(ElemType *)realloc(,(+stackincrement)*sizeof(ElemType));
if (!) then exit (OVERFLOW);
=+;
+=staxkincrement;
};
*=e;
++;
//or *++=e;
return OK;
}
Status pop(StackList &s, ElmeType &e){
if (=) then return Error;
=-1;
e=*;
//e=*--
return OK;
}
//队列
#define QLength 100;
typedef struct{
Elemtype *base;
int front;
int rear;
}QList;
Status init_qlist(QList &Q){
=(ElemType *)malloc(Maxsize*sizeof(ElemType));
if (!) exit(OVERFLOW);
=0;
=;
return OK;
}
Status enter_queue(QList &q, ElemType e){
if (+)%Maxsize=Maxsize then return Error;
*[]=e;
=(+1)%Maxsize;
return OK;
}
Status del_queue(QList &q, ElemType &e){
if (=) then return Error;
e=*[];
=(+1)%Maxsize;
return OK;
}