<>线性表__顺序表示
#include<stdio.h> #include<stdlib.h> //静态分配 #define MaxSize 50 typedef int
ElemType; typedef struct{ ElemType data[MaxSize]; int length; }SqList; //动态分配 #
define InitSize 100 typedef struct{ ElemType *data; int capacity; int length; }
SeqList; bool ListInsert(SqList &L,int i,ElemType e){ if(i<1 || i>L.length+1)
return false; if(L.length>=MaxSize) return false; for(int j=L.length;j>=i;j--) L
.data[j]=L.data[j-1]; L.data[i-1]=e; L.length++; return true; } bool ListDelete(
SqList&L,int i, ElemType &e){ if(i<1 || i>L.length) return false; e=L.data[i-1];
for(int j=i;j<L.length;j++) L.data[j-1]=L.data[j]; L.length--; return true; }
boolLocateElem(SqList L,ElemType e){ int i; for(int i=0;i<L.length;i++) if(L.
data[i]==e) return i+1; return 0; } void PrintList(SqList &L){ for(int i=0;i<L.
length;i++){ printf("%4d",L.data[i]); } printf("\n"); } int main(){ SqList L;
bool ret; ElemType del; L.data[0]=1; L.data[1]=2; L.data[2]=3; L.length=3; ret=
ListInsert(L,2,60); if(ret){ printf("插入成功\n"); PrintList(L); }else{ printf(
"插入失败\n"); } ret=ListDelete(L,1,del); if(ret){ printf("删除成功\n"); printf(
"删除元素值为%d\n",del); PrintList(L); }else{ printf("删除失败\n"); } ret=LocateElem(L,60)
; if(ret){ printf("查找成功\n"); printf("元素位置为%d\n",ret); PrintList(L); }else{
printf("查找失败\n"); } system("pause"); return 0; }
<>

<>线性表__链式表示
#include<stdio.h> #include<stdlib.h> typedef int ElemType; typedef struct LNode
); L->next=NULL; scanf("%d",&x); while(x!=9999){ s=(LNode*)malloc(sizeof(LNode))
; s->data=x; s->next=L->next; L->next=s; scanf("%d",&x); } return L; } LinkList
LNode)); LNode *s,*r=L; scanf("%d",&x); while(x!=9999){ s=(LNode*)malloc(sizeof(
LNode)); s->data=x; r->next=s; r=s; scanf("%d",&x); } r->next=NULL; return L; }
LNode*GetElem(LinkList L,int i){ int j=1; LNode *p=L->next; if(i==0) return L;
if(i<1) return NULL; while(p&&j<i){ p=p->next; j++; } return p; } LNode *
LocateElem(LinkList L,ElemType e){ LNode *p=L->next; while(p!=NULL&&p->data!=e)
p=p->next; return p; } bool ListFrontInsert(LinkList L,int i,ElemType e){
malloc(sizeof(LNode)); s->data=e; s->next=p->next; p->next=s; return true; }
false; } LinkList q; q=p->next; p->next=q->next; free(q); return true; } void
PrintList(LinkList L){ L=L->next; while(L!=NULL){ printf("%3d",L->data); L=L->
//CreatList1(L);//输入数据为3 4 5 6 7 9999 CreatList2(L);//输入数据为3 4 5 6 7 9999
PrintList(L); search=GetElem(L,2); if(search!=NULL){ printf("按序号查找成功\n"); printf
("%d\n",search->data); } search=LocateElem(L,6); if(search!=NULL){ printf(
"按值查找成功\n"); printf("%d\n",search->data); } ListFrontInsert(L,2,99); PrintList(L
); ListDelete(L,4); PrintList(L); system("pause"); }

<>List_tail_insert（CreatList2）

<>双链表
#include<stdio.h> #include<stdlib.h> typedef int ElemType; typedef struct DNode
while(x!=9999){ s=(DNode*)malloc(sizeof(DNode)); s->data=x; s->next=DL->next; if
(DL->next!=NULL){ DL->next->prior=s; } s->prior=DL; DL->next=s; scanf("%d",&x);
x; DL=(DLinkList)malloc(sizeof(DNode)); DNode *s,*r=DL; DL->prior=NULL; scanf(
"%d",&x); while(x!=9999){ s=(DNode*)malloc(sizeof(DNode)); s->data=x; r->next=s;
s->prior=r; r=s; scanf("%d",&x); } r->next=NULL; return DL; } DNode *GetElem(
DLinkList DL,int i){ int j=1; DNode *p=DL->next; if(i==0) return DL; if(i<1)
return NULL; while(p&&j<i){ p=p->next; j++; } return p; } bool DListFrontInsert(
false; } DLinkList s=(DNode*)malloc(sizeof(DNode)); s->data=e; s->next=p->next;
p->next->prior=s; s->prior=p; p->next=s; return true; } bool DListDelete(
DLinkList q; q=p->next; if(q==NULL) return false; p->next=q->next; if(q->next!=
NULL){ //删除最后一个元素，需要的判断 q->next->prior=p; } free(q); return true; } void
//Dlist_head_insert(DL); Dlist_tail_insert(DL);//输入数据为3 4 5 6 7 9999 PrintDList(
DL); search=GetElem(DL,2); if(search!=NULL){ printf("按序号查找成功\n"); printf("%d\n",
search->data); } DListFrontInsert(DL,3,99); PrintDList(DL); DListDelete(DL,2);
PrintDList(DL); system("pause"); }
<>Dlist_tail_insert

<>Dlist_tail_insert

<>栈（数组实现）
#include<stdio.h> #include<stdlib.h> //实现栈 可以用数组，也可以用链表 #define MaxSize 50
typedef int ElemType; typedef struct DNode{ ElemType data[MaxSize]; int top;
}SqStack; void InitStack(SqStack &S){ S.top=-1; } bool StackEmpty(SqStack &S){
if(S.top==-1) return true; else return false; } bool Push(SqStack &S,ElemType
x){ if(S.top==MaxSize-1){ return false; } S.data[++S.top]=x; return true; }
bool Pop(SqStack &S,ElemType &x){ if(-1==S.top) return false;
x=S.data[S.top--]; return true; } bool GetTop(SqStack &S,ElemType &x){
if(-1==S.top) return false; x=S.data[S.top]; return true; } int main(){ SqStack
S; bool flag; ElemType m; InitStack(S); flag=StackEmpty(S); if(flag){
printf("栈是空的\n"); } Push(S,3); Push(S,4); Push(S,5); flag=GetTop(S,m);
if(flag){ printf("获取栈顶元素为%d\n",m); } flag=Pop(S,m); if(flag){
printf("弹出元素为%d\n",m); } system("pause"); }

<>循环队列
#include<stdio.h> #include<stdlib.h> //队列可以用数组也可用链表实现 #define MaxSize 5
typedef int ElemType; typedef struct{ ElemType data[MaxSize]; int front,rear;
}SqQueue; void InitQueue(SqQueue &Q) { Q.rear=Q.front=0; } bool isEmpty(SqQueue
&Q) { if(Q.rear==Q.front) return true; else return false; } bool
EnQueue(SqQueue &Q,ElemType x){ if((Q.rear+1)%MaxSize==Q.front) return false;
Q.data[Q.rear]=x; Q.rear=(Q.rear+1)%MaxSize; return true; } bool
DeQueue(SqQueue &Q,ElemType &x){ if(Q.rear==Q.front) return false;
x=Q.data[Q.front]; Q.front=(Q.front+1)%MaxSize; return true; } int main(){
SqQueue Q; bool ret; ElemType element; InitQueue(Q); ret=isEmpty(Q); if(ret){
printf("队列为空\n"); }else{ printf("队列不为空\n"); } EnQueue(Q,3); EnQueue(Q,4);
EnQueue(Q,5); ret=EnQueue(Q,6); //ret=EnQueue(Q,7); if(ret){ printf("入队成功\n");
}else{ printf("入队失败\n"); } ret=DeQueue(Q,element); if(ret){
printf("出队成功，元素值为%d\n",element); }else{ printf("出队失败\n"); }
ret=DeQueue(Q,element); if(ret){ printf("出队成功，元素值为%d\n",element); }else{
printf("出队失败\n"); } ret=EnQueue(Q,8); if(ret){ printf("入队成功\n"); }else{
printf("入队失败\n"); } system("pause"); }

<>队列_链式
#include<stdio.h> #include<stdlib.h> //头部删除法，尾部插入法 typedef int ElemType;
bool IsEmpty(LinkQueue &Q){ if(Q.front==Q.rear) return true; else return false;
bool DeQueue(LinkQueue &Q,ElemType &x){ if(Q.front==Q.rear) return false;
Q.rear=Q.front; free(p); return true; } int main(){ LinkQueue Q; bool ret;
ElemType element; InitQueue(Q); EnQueue(Q,3); EnQueue(Q,4); EnQueue(Q,5);
EnQueue(Q,6); EnQueue(Q,7); ret=DeQueue(Q,element); if(ret){
printf("出队成功，元素值为%d\n",element); }else{ printf("出队失败\n"); } system("pause"); }

<>斐波那契数列
#include<stdio.h> #include<stdlib.h> int Fib(int n){ if(n==0) return 0; else
if(n==1) return 1; else return Fib(n-1)+Fib(n-2); } int main(){ int num;
while(scanf("%d",&num)!=EOF){ printf("Fib(%d) = %d\n",num,Fib(num)); }
system("pause"); }

30天阅读排行