线性表__顺序表示
#include
#include
#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;
}
bool LocateElem(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;
}
![王道《数据结构考研复习指导》代码_第1张图片](http://img.e-com-net.com/image/info8/ace721ba0d9744c795f0956139a3fea1.png)
线性表__链式表示
#include
#include
typedef int ElemType;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
LinkList CreatList1(LinkList &L){
LNode *s;int x;
L=(LinkList)malloc(sizeof(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 CreatList2(LinkList &L){
int x;
L=(LinkList)malloc(sizeof(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){
LinkList p=GetElem(L,i-1);
if(NULL==p){
return false;
}
LinkList s=(LNode*)malloc(sizeof(LNode));
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
bool ListDelete(LinkList L,int i){
LinkList p=GetElem(L,i-1);
if(NULL==p){
return 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->next;
}
printf("\n");
}
int main(){
LinkList L;
LinkList search;
CreatList2(L);
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_head_insert(CreatList1)
![王道《数据结构考研复习指导》代码_第2张图片](http://img.e-com-net.com/image/info8/6d19e98274e74c3b9818ad103c93435b.png)
List_tail_insert(CreatList2)
![王道《数据结构考研复习指导》代码_第3张图片](http://img.e-com-net.com/image/info8/d47834af4f33496ba34a0c1befbd7382.png)
双链表
#include
#include
typedef int ElemType;
typedef struct DNode{
ElemType data;
struct DNode *prior,*next;
}DNode,*DLinkList;
DLinkList Dlist_head_insert(DLinkList &DL){
DNode *s;int x;
DL=(DLinkList)malloc(sizeof(DNode));
DL->next=NULL;
DL->prior=NULL;
scanf("%d",&x);
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);
}
return DL;
}
DLinkList Dlist_tail_insert(DLinkList &DL){
int 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(DLinkList DL,int i,ElemType e){
DLinkList p=GetElem(DL,i-1);
if(NULL==p){
return 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 DL,int i){
DLinkList p=GetElem(DL,i-1);
if(NULL==p){
return false;
}
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 PrintDList(DLinkList DL){
DL=DL->next;
while(DL!=NULL){
printf("%3d",DL->data);
DL=DL->next;
}
printf("\n");
}
int main(){
DLinkList DL;
DLinkList search;
Dlist_tail_insert(DL);
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
![王道《数据结构考研复习指导》代码_第4张图片](http://img.e-com-net.com/image/info8/c268d583e20748df97270432667e0b48.png)
Dlist_tail_insert
![王道《数据结构考研复习指导》代码_第5张图片](http://img.e-com-net.com/image/info8/832f70285d6849e586a4571c28ad47f9.png)
栈(数组实现)
#include
#include
//实现栈 可以用数组,也可以用链表
#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");
}
![运行结果](http://img.e-com-net.com/image/info8/7dc1fc30d3004638bd592c2b741e4e2d.jpg)
循环队列
#include
#include
//队列可以用数组也可用链表实现
#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");
}
![王道《数据结构考研复习指导》代码_第6张图片](http://img.e-com-net.com/image/info8/d41a0cad13f24de9a01508f333f4f183.png)
队列_链式
#include
#include
//头部删除法,尾部插入法
typedef int ElemType;
typedef struct LinkNode{
ElemType data;
struct LinkNode *next;
}LinkNode;
typedef struct{
LinkNode *front,*rear;
}LinkQueue;
void InitQueue(LinkQueue &Q){
Q.front=Q.rear=(LinkNode*)malloc(sizeof(LinkNode));
Q.front->next=NULL;
}
bool IsEmpty(LinkQueue &Q){
if(Q.front==Q.rear)
return true;
else
return false;
}
void EnQueue(LinkQueue &Q,ElemType x){
LinkNode *s=(LinkNode *)malloc(sizeof(LinkNode));
s->data=x;s->next=NULL;
Q.rear->next=s;
Q.rear=s;
}
bool DeQueue(LinkQueue &Q,ElemType &x){
if(Q.front==Q.rear) return false;
LinkNode *p=Q.front->next;
x=p->data;
Q.front->next=p->next;
if(Q.rear==p)
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");
}
![运行结果](http://img.e-com-net.com/image/info8/b176fcc100414176b53097e2b925570a.png)
斐波那契数列
#include
#include
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");
}