顺序表及链表的应用.docx
《顺序表及链表的应用.docx》由会员分享,可在线阅读,更多相关《顺序表及链表的应用.docx(27页珍藏版)》请在冰豆网上搜索。
顺序表及链表的应用
设计一顺序表的应用代码
Head.h
#defineLIST_INIT_SIZE100//线性表存储空间的初始分配量
#defineLISTINCREMENT10//线性表存储空间的分配增量
#defineSTACK_INIT_SIZE100//栈存储空间初始分配量
#defineSTACKINCREMENT10//栈存储空间分配增量
#defineMAXQSIZE10//最大队列长度
typedefintElemType;
typedefcharSElemType;
typedefintQElemType;
typedefstruct{
int*elem;//存储空间基址
intlength;//当前长度
intlistsize;//当前分配的存储容量
}SqList;
typedefstruct{
SElemType*base;//在栈构造之前和销毁之后,base的值为null
SElemType*top;//栈顶指针
intstacksize;//当前已分配的存储空间,以元素为单位
}SqStack;
typedefstruct
{
QElemType*base;//初始化的动态分配存储空间
intfront;//头指针,若队列不空,指向队列头元素
intrear;//尾指针,若队列不空,指向队列尾元素的下一个位置
}SqQueue;
voidCheck1();
voidCheck2();
voidCheck3();
voidCheck4();
intInitList_Sq(SqList*L);
intCreateList(SqList*L);
intDeleteElem_Sq(SqList*L);
intPrintElem(SqListL);
intDeleteElem_1(SqList*L);
intDevideList_Sq(SqListL,SqList*La,SqList*Lb);
intInitStack(SqStack*L);
intPush(SqStack*S,SElemTypee);
intStackEmpty(SqStackS);
intGetTop(SqStackS,SElemType*e);
intPop(SqStack*S,SElemType*e);
intCorrect(charexp[],intn);
intInitQueue(SqQueue*Q);
intCreateQueue(SqQueue*Q);
intPrintQueue(SqQueueQ);
intEnQueue(SqQueue*Q,QElemTypee);
intDeQueue(SqQueue*Q,QElemType*e);
……………………………………………………………………………………………………….
main.cpp
#include
#include"head.h"
#include"stdlib.h"
#include
voidmain()
{
intchoice;
do
{
printf("\n顺序表的应用\n");
printf("\n------------主菜单--------------\n");
printf("
(1)删除线性表中值为item的元素\n");
printf("
(2)将一个顺序表分拆成两个顺序表\n");
printf("(3)判断括弧是否匹对\n");
printf("(4)循环队列中插入和取出节点\n");
printf("(0)退出系统\n");
printf("\n请选择操作步骤:
");
scanf("%d",&choice);
if(choice<0&&choice>4)continue;
switch(choice)
{
case1:
Check1();break;
case2:
Check2();break;
case3:
Check3();break;
case4:
Check4();break;
case0:
exit(0);
default:
break;
}
}while
(1);
}
……………………………………………………………………………………………………….
Check1.cpp
#include"head.h"
#include
voidCheck1(){
SqListL1;
printf("实现删除线性表中值为item的元素的算法:
\n");
CreateList(&L1);//实现第一个算法
PrintElem(L1);
DeleteElem_1(&L1);
PrintElem(L1);
}
CreateList.cpp
#include"head.h"
#include"stdio.h"
#include"stdlib.h"
//创建顺序表,在顺序表中输入数据元素。
intCreateList(SqList*L)
{
inti,n;
if(!
InitList_Sq(L))exit(-1);
printf("请输入顺序表的长度:
");
scanf("%d",&n);
(*L).length=n;
ElemType*p=(*L).elem;
printf("请输入顺序表的数据元素:
");
for(i=0;i{
scanf("%d",p);
p++;
}
return1;
}
PrintElem.cpp
#include"head.h"
#include
intPrintElem(SqListL)
{
if(L.length==0)printf("Empty!
\n");
inti;
for(i=0;iprintf("%4d",L.elem[i]);
printf("\n");
return1;
}
DeleteElem_1.cpp
#include"head.h"
#include
intDeleteElem_1(SqList*L)
{
int*p,*q,item;
printf("请输入要删去的元素:
");
scanf("%d",&item);
p=(*L).elem;
q=(*L).elem+(*L).length-1;
while(p{
if(*p==item)
{
*p=*q;
q--;
(*L).length--;
}
elsep++;
}
if(*p==item)(*L).length--;
return1;
}
……………………………………………………………………………………………………….
Check2().cpp
#include"head.h"
#include
voidCheck2(){
SqListL2,La,Lb;
printf("实现将一个顺序表分拆成两个顺序表的算法:
\n");
CreateList(&L2);//实现第二个算法
PrintElem(L2);
DevideList_Sq(L2,&La,&Lb);
PrintElem(La);
PrintElem(Lb);
}
DevideList_Sq.cpp
#include"head.h"
intDevideList_Sq(SqListL,SqList*La,SqList*Lb)
{
InitList_Sq(La);InitList_Sq(Lb);
(*La).length=(*La).length=0;
int*p,*q;
p=L.elem;
q=L.elem+L.length-1;
for(;p<=q;p++)
{
if((*p)>0)(*La).elem[(*La).length++]=*p;
else(*Lb).elem[(*Lb).length++]=*p;
}
return1;
}
InitList_Sq.cpp
#include"head.h"
#include
//构造一个空的线性表
intInitList_Sq(SqList*L)
{
(*L).elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!
((*L).elem))exit(-1);//存储分配失败
(*L).length=0;//空表长度为0
(*L).listsize=LIST_INIT_SIZE;//初始存储容量
return1;
}
……………………………………………………………………………………………………….
Check3().cpp
#include"head.h"
#include
#include
voidCheck3(){
printf("实现判断括弧是否匹对的算法:
\n");
chara[20];//实现第三个算法
inttag;
printf("InputString:
");
scanf("%s",a);
tag=Correct(a,strlen(a));
if(tag)printf("correct!
\n");
elseprintf("wrong!
\n");
}
Correct.cpp
#include"head.h"
intCorrect(charexp[],intn)
{
SqStackS;InitStack(&S);
char*p=exp,e,x;
inti;
for(i=0;i{if(*p=='('||*p=='['||*p=='{')Push(&S,*p);
elseif(*p==')'||*p==']'||*p=='}')
{
GetTop(S,&e);
switch(*p)
{
case')':
if(e=='(')Pop(&S,&x);elsereturn0;break;
case']':
if(e=='[')Pop(&S,&x);elsereturn0;break;
case'}':
if(e=='{')Pop(&S,&x);elsereturn0;break;
}
}
}
if(!
StackEmpty(S))return0;
elsereturn1;
}
GetTop.cpp
#include"head.h"
intGetTop(SqStackS,SElemType*e)
{//若栈不空,则用e返回S的栈顶元素,并返回1,否则返回0
if(S.top==S.base)return0;
else*e=*(S.top-1);
return1;
}
InitStack.cpp
#include
#include"head.h"
//构造一个空栈
intInitStack(SqStack*S)
{
(*S).base=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!
(*S).base)exit(-1);//存储分配失败
(*S).top=(*S).base;
(*S).stacksize=STACK_INIT_SIZE;
return1;
}
Pop.cpp
#include"head.h"
intPop(SqStack*S,SElemType*e)
{//若栈不空,则删除S的栈顶元素,用e返回其值,并返回1,否则返回0
if(StackEmpty(*S))return0;
*e=*(--(*S).top);
return1;
}
Push.cpp
#include
#include"head.h"
intPush(SqStack*S,SElemTypee)
{//插入元素e为新的栈顶元素
if((*S).top-(*S).base>=(*S).stacksize)//栈满,追加存储空间
{
(*S).base=(SElemType*)realloc((*S).base,((*S).stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!
(*S).base)exit(-1);//存储分配失败
(*S).stacksize+=STACKINCREMENT;
}
*((*S).top++)=e;
return1;
}
StackEmpty.cpp
#include"head.h"
intStackEmpty(SqStackS)
{
if(S.top==S.base)return1;
elsereturn0;
}
……………………………………………………………………………………………………….
Check4().cpp
#include"head.h"
#include
voidCheck4(){
printf("实现循环队列中插入和取出节点的算法:
\n");
SqQueueQ;//实现第四个算法
QElemTypee;
CreateQueue(&Q);
printf("请输入插入的节点:
");
scanf("%d",&e);
PrintQueue(Q);
EnQueue(&Q,e);
PrintQueue(Q);
DeQueue(&Q,&e);
PrintQueue(Q);
}
CreateQueue.cpp
#include
#include"head.h"
//创建一个队列
int(SqQueue*Q)
{
intn,i;
InitQueue(Q);
printf("输入队列的长度:
");
scanf("%d",&n);
if(n>MAXQSIZE){printf("overflow!
\n");return0;}
for(i=0;iscanf("%d",((*Q).base)+i);
(*Q).rear=n-1;
return1;
}
DeQueue.cpp
#include"head.h"
#include
intDeQueue(SqQueue*Q,QElemType*e)
{//若队列不空,则删除Q的队头元素,用e返回其值,并返回1,否则返回0
if((*Q).front==(*Q).rear){printf("TheQueueisempty!
");return0;}
*e=(*Q).base[(*Q).front];
(*Q).front=((*Q).front+1)%MAXQSIZE;
return1;
}
EnQueue.cpp
#include
#include"head.h"
intEnQueue(SqQueue*Q,QElemTypee)
{//插入元素e为新的队尾元素
if(((*Q).rear+1)%MAXQSIZE==(*Q).front){printf("overflow!
\n");return0;}//队列满
((*Q).base)[(*Q).rear]=e;
(*Q).rear=((*Q).rear+1)%MAXQSIZE;
return1;
}
InitQueue.cpp
#include"head.h"
#include
//构造一个空队列
intInitQueue(SqQueue*Q)
{
(*Q).base=(QElemType*)malloc(MAXQSIZE*sizeof(QElemType));
if(!
((*Q).base))exit(-1);//存储空间分配失败
(*Q).front=(*Q).rear=0;
return1;
}
PrintQueue.cpp
#include"head.h"
#include
intPrintQueue(SqQueueQ)
{
if(Q.front==Q.rear){printf("empty!
");return0;}
inti,n;
QElemType*p;
n=(Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
p=&((Q.base))[Q.front];
for(i=0;i{
printf("%4d",*p);
if(p==&Q.base[MAXQSIZE-1])p=Q.base;
elsep++;
}
printf("\n");
return1;
}
……………………………………………………………………………………………………….
设计二链表的应用代码
Head.h
typedefintElemType;
typedefstructLNode
{//线性表的单链表存储结构
ElemTypedata;
structLNode*next;
}LNode,*LinkList;
typedefstructDuLNode{
//线性表的双向链表存储结构
ElemTypedata;
structDuLNode*prior;
structDuLNode*next;
intfreq;
}DulNode,*DuLinkList;
voidCreateList_L(LinkList*L,intn);
intPrintLinkList(LinkListL);
intEqualLinkList(LinkList*La,LinkList*Lb);
voidCreatCircularLinkList(LinkList*L,intn);
intMonkeyKing(LinkListL,intn);
intLinkListLength_Dul(DuLinkListL);
intCreateLinkList_Dul(DuLinkList*L,intn);
intPrintLinkList_Dul(DuLinkListL);
intLocate(DuLinkList*L,ElemTypee);
//intInterSection(LinkListLa,LinkListLb,LinkList*Lc);
voidCheck_1();
voidCheck_2();
voidCheck_3();
……………………………………………………………………………………………………....
main.cpp
#include
#include"head.h"
#include
voidmain()
{
intchoice;
do
{
printf("\n链表的应用\n");
printf("\n------------主菜单--------------\n");
printf("
(1)删除两个递增链表中不同的元素\n");
printf("
(2)猴子选大王\n");
printf("(3)实现locate函数\n");
printf("(0)退出系统...\n");
printf("\n请选择操作步骤:
");
scanf("%d",&choice);
if(choice<0&&choice>3)continue;
switch(choice)
{
case1:
Check_1();break;
case2:
Check_2();break;
case3:
Check_3();break;
case0:
exit(0);
default:
break;
}
}while
(1);
}
……………………………………………………………………………………………………....
Check_1.cpp
#include"head.h"
#include
voidCheck_1()
{
LinkListLa,Lb;
intn1,n2;
printf("请输入单链表La的长度:
");
scanf("%d",&n1);
printf("请按逆位序输入:
\n");
CreateList_L(&La,n1);
printf("La:
");
PrintLinkList(La);
printf("请输入单链表Lb的长度:
:
");
scanf("%d",&n2);
printf("请按逆位序输入:
\n");
CreateList_L(&Lb,n2);
printf("Lb:
");
PrintLinkList(Lb);
EqualLinkList(&La,&Lb);
printf("删除元素以后\n");
printf("La:
");
PrintLinkList(La);
printf("Lb:
");
PrintLinkList(Lb);
}
CreateList_L.cpp
#include
#include"head.h"
#include
voidCreateList_L(LinkList*L,intn)
{//逆位序输入n个元素的值,建立带表头结点的单性线性表L
inti;
LinkListp;
*L=(LinkList)malloc(sizeof(LNode));
(*L)->next=NULL;//先建立一个带头结点的单链表
for(i=n;i>0;--i)
{
p=(LinkList)malloc(sizeof(LNode));//生成新结点
scanf("%d",&(p->data));//输入元素值
p->next=(*L)->next;
(*