链表顺序结构
链表结构
typedef int ElementType;
typedef struct{
ElementType Data[MAXSIZE];
int Last;
}List;
创建一个空顺序表
List* MakeEmpty(){
List *Ptrl;
Ptrl = (List*)malloc(sizeof(List));
Ptrl->Last = -1;
return Ptrl;
}
插入
void Insert(ElementType X,int i ,List* Ptrl){
int j;
if(Ptrl->Last == MAXSIZE - 1){
printf("表满");
return;
}
if(i < 1 || i > Ptrl->Last + 2){
printf("位置不合法");
return;
}
for(j = Ptrl->Last ; j >= i - 1 ; j --)
Ptrl->Data[j + 1] = Ptrl->Data[j];
Ptrl->Data[i - 1] = X;
Ptrl->Last ++;
return;
}
查找
int Find(ElementType X ,List* Ptrl){
int i = 0;
while(i <= Ptrl->Last && Ptrl->Data[i] != X)
i ++;
if(i > Ptrl->Last)
return -1;
else
return i;
}
删除
void Delete(int i,List *Ptrl){
int j;
if( i < 1 || i >Ptrl->Last + 1){
printf("不存在第%d个元素",i);
return;
}
for(j = i ; j <= Ptrl->Last ; j ++)
Ptrl->Data[j - 1] = Ptrl->Data[j];
Ptrl->Last --;
return;
}
链表链式结构实现
结构
typedef int ElementType;
typedef struct Node{
ElementType Data;
struct Node*Next;
}List;
创建链表
List* CreateNode(){
List *p = (List*)malloc( sizeof(List) );
p->Data = 0;
p->Next = NULL;
return p;
}
按序号查找
List* FindKth(int K,List* Ptrl){
List *p = Ptrl->Next;
int i = 1;
while(p != NULL && i < K){
p = p->Next;
i ++;
}
if(i == K)
return p;
else
return NULL;
}
按值查找
List* Find(ElementType X,List *Ptrl){
List *p = Ptrl->Next;
while(p != NULL && p->Data != X)
p = p->Next;
return p;
}
指定位序插入
List* Insert(ElementType X , int i , List* Ptrl){
List *p = NULL,*s = NULL;
if(i == 1){
s = (List*)malloc(sizeof(List));
s->Data = X;
s->Next = Ptrl->Next;
Ptrl->Next = s;
return Ptrl;
}
p = FindKth(i - 1 , Ptrl);
if(p == NULL){
printf("参数i错");
return NULL;
}
else{
s = (List*)malloc(sizeof(List));
s->Data = X;
s->Next = p->Next;
p->Next = s;
return Ptrl;
}
}
删除指定位序元素
List* Delete(int i,List *Ptrl){
List *p = NULL,*s = NULL;
if(i == 1){
s = Ptrl->Next;
Ptrl->Next = s->Next;
free(s);
return Ptrl;
}
p = FindKth(i - 1,Ptrl);
if(p == NULL){
printf("第%d个结点不存在",i - 1);
return NULL;
}else if( p->Next == NULL){
printf("第%d个结点不存在",i);
return NULL;
}else{
s = p->Next;
p->Next = s->Next;
free(s);
return Ptrl;
}
}
打印链表
void PrintNode(List* Ptrl){
List* p = Ptrl->Next;
while(p){
printf(" %d ",p->Data );
p = p->Next;
}
}
栈
栈的结构
#define ERROR -1
typedef int ElementType;
typedef struct{
ElementType Data[MaxSize];
int Top;
}Stack;
创建栈
Stack* CreateStack(){
Stack *s = NULL;
s = (Stack*)malloc(sizeof(Stack));
s->Top = -1;
return s;
}
入栈
void Push(Stack* PtrS,ElementType item){
if(PtrS->Top == MaxSize - 1){
printf("栈满");
return;
}else{
PtrS->Data[++ (PtrS->Top)] = item;
return;
}
}
出栈
ElementType Pop(Stack * PtrS){
if(PtrS->Top == -1){
printf("栈空");
return ERROR;
}else
return(PtrS->Data[(PtrS->Top) -- ]);
}
判断栈空
int IsEmpty(Stack* S){
if(S->Top == -1)
return 1;
else
return 0;
}
队列
队列结构
#define ERROR -1
typedef int ElementType;
typedef struct{
ElementType Data[MaxSize];
int rear;
int front;
}Queue;
创建队列
Queue* CreateQueue(){
Queue * q;
q = (Queue*)malloc(sizeof(Queue) );
q->rear = -1;
q->front = -1;
return q;
}
入队
void AddQ(Queue* PtrQ, ElementType item){
PtrQ->Data[++ (PtrQ->rear)] = item;
}
出队
ElementType DeleteQ(Queue* PtrQ){
if(PtrQ->front == PtrQ->rear){
printf("队列空");
return ERROR;
}else{
return PtrQ->Data[++ PtrQ->front];
}
}