c语言数据结构基本操作文档格式.docx
《c语言数据结构基本操作文档格式.docx》由会员分享,可在线阅读,更多相关《c语言数据结构基本操作文档格式.docx(46页珍藏版)》请在冰豆网上搜索。
Statuslistempty_Sq(SqListc)
/*测试顺序表是否为空*/
if(c.length!
=0)return(FALSE);
return(TRUE);
StatusListInsert_Sq(SqList&
L,inti,ElemTypee)
/*在第i个位置上插入一个元素*/
{intj,*newbase;
if(i<
1||i>
L.length+1)returnERROR;
if(L.length>
=L.listsize){
newbase=(ElemType*)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(ElemType));
if(!
newbase)exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
for(j=L.length;
j>
=i;
--j)
L.elem[j]=L.elem[j-1];
L.elem[j]=e;
++L.length;
intLocateElem_Sq(SqListL,ElemTypee)/*返回元素e在顺序表中的位置*/
{inti=1;
ElemType*p=L.elem;
while(i<
=L.length&
&
!
(*p==e))
{i++;
p++;
if(i<
=L.length)returni;
returnFALSE;
StatusListDelete_Sq(SqList&
L,inti,int&
e)
/*删除第i个位置上的元素*/
{intj;
L.length)returnERROR;
e=L.elem[i-1];
for(j=i;
j<
L.length;
j++)
L.elem[j-1]=L.elem[j];
L.length--;
voidPrint_Sq(SqListL)/*输出顺序表*/
{inti;
if(listempty_Sq(L))
printf("
\nSequentialList'
slengthis%d.It'
sempty!
"
L.length);
else
slengthis%d.Theseelementare:
"
for(i=0;
i<
i++)
%d"
L.elem[i]);
voidmenu()
{printf("
\n"
);
\n**************************\n"
1-------PrinttheSequentialList.\n"
2-------InsertadataintheSequentialList.\n"
3-------DeleteadataintheSequentialList.\n"
4-------Getaelement'
slocationtintheSqList.\n"
5-------CleartheSequentialList.\n"
6-------Exit.\n"
**************************\n"
voidmenuselect(SqListL)
{intk,i,done=1;
ElemTypee;
charch[10];
while(done)
{menu();
Pleasechoose:
scanf("
%d"
&
k);
getchar();
switch(k)
{case1:
Print_Sq(L);
break;
case2:
\nInputthelocationanddatayouwanttoInsert:
%d,%d"
i,&
e);
if(ListInsert_Sq(L,i,e)==ERROR)printf("
Insertlocationisnotcorrect!
case3:
\nInputthelocationyouwanttodeletedata:
scanf("
i);
if(ListDelete_Sq(L,i,e)==ERROR)
printf("
Deletelocationisnotexit!
else
\nDeletedatais%d."
e);
break;
case4:
\nInputthedatayouwanttofind:
if(LocateElem_Sq(L,e)!
=FALSE)
\nData%dlocationintheSequentialListis%d,e,LocateElem_Sq(L,e));
elseprintf("
\n%disnotintheSequentialList.\n"
e);
case5:
clearlist_Sq(L);
;
case6:
done=0;
voidmain()
{ElemTypee;
SqListLa;
intn,i;
clrscr();
initlist_Sq(La);
InputanumberoftheelementintheSequentialList(n<
=%d):
Length);
n);
Entertheseelements:
for(i=1;
=n;
{scanf("
ListInsert_Sq(La,i,e);
menuselect(La);
getch();
/*顺序表的归并操作*/
union_Sq(SqList*La,SqListLb)
{Int
i,La_len=La->
length,Lb_len=Lb.length,j;
=Lb_len;
e=Lb.elem[i-1];
LocateElem_Sq(La,e))
ListInsert_Sq(La,++La_len,e);
MergeList(SqListLa,SqListLb,SqList*Lc)
{inti=1,j=1,k=0,La_len=La.length,Lb_len=Lb.length;
ElemTypeai,bj;
while((i<
=La_len)&
(j<
=Lb_len))
{
ai=La.elem[i-1];
bj=Lb.elem[j-1];
if(ai<
=bj){ListInsert_Sq(Lc,++k,ai);
i++;
else{ListInsert_Sq(Lc,++k,bj);
j++;
=La_len){ai=La.elem[-1+i++];
ListInsert_Sq(Lc,++k,ai);
while(j<
=Lb_len){bj=Lb.elem[-1+j++];
ListInsert_Sq(Lc,++k,bj);
SqListLa,Lb,Lc;
initlist_Sq(Lb);
\nInputanumberoftheelementintheSequentialListLa(n<
\nEntertheseelements:
ListInsert_Sq(&
La,i,e);
\nInputanumberoftheelementintheSequentialListLb(n<
Lb,i,e);
Print_Sq("
La"
La);
Lb"
Lb);
union_Sq(&
La,Lb);
Laafterunion(La&
Lb)"
initlist_Sq(Lc);
MergeList(La,Lb,&
Lc);
Lc(LaorLb)"
Lc);
clearlist_Sq(Lc);
Lcafterclear"
/*单链表的基本操作*/
alloc.h>
typedefintET;
typedefET*Ep;
typedefstructLNode
{ETdata;
structLNode*next;
}LNode,*LinkList;
/*逆序建立一个长度为n的单链表*/
voidCreatList(LinkList*L,intn)
LinkListp,q;
ETe;
p=(LinkList)malloc(sizeof(LNode));
p->
next=NULL;
*L=q=p;
Pleaseinputthesedatas:
for(i=n;
i>
0;
i--)
{p=(LinkList)malloc(sizeof(LNode));
data=e;
next=q->
next;
q->
next=p;
/*初始化单链表*/
voidInit(LinkList*L)
{intn;
Pleaseinputthenumberofthenode:
CreatList(L,n);
/*打印单链表中的结点元素值*/
voidprintlk(LinkListL)
{LinkListp;
p=L->
while(p){
%d->
p->
data);
p=p->
NULL\n"
/*在单链表的第i个结点位置之后插入一个新结点*/
intListInsert(LinkListL,inti,ETe)
{intj=0;
LinkListp,s;
/*指针p初始值指向第1个结点*/
while(p&
j<
i-1)/*指针p指向第i个结点位置,记数器j指向第i-1个结点位置*/
{p=p->
++j;
p||j>
i-1)returnFALSE;
s=(LinkList)malloc(sizeof(LNode));
s->
next=p->
next=s;
returnTRUE;
/*删除单链表中的第i个结点*/
intListDelete(LinkListL,inti,ET*e)
{LinkListp,q;
intj=0;
p=L;
/*指针p初始值指向头结点*/
i-1)/*指针p指向第i-1个结点位置,记数器j也指向第i-1个结点位置*/
(p->
next)||(j>
i-1))
q=p->
/*指针q指向要删除的第i个结点位置*/
*e=q->
data;
free(q);
intInsert(LinkListL)/*插入函数*/
{inti,flag;
ETdata;
\nPleaseinputtheinsertposition:
InputthedatayouwanttoInsert:
flag=ListInsert(L,i,data);
returnflag;
StatusDelete(LinkListL)/*删除函数*/
\nPleaseinputthedeleteposition:
flag=ListDelete(L,i,&
if(flag==TRUE)
Deletedelementis%d\n"
/*取单链表中第i个结点的数据元素值*/
intGetElem(LinkListL,inti,ET&
{intj=1;
LinkListp;
i)
i)returnFALSE;
e=p->
StatusGetElem_List(LinkListL)
\nPleaseinputtheposition:
flag=GetElem(L,i,e);
Getelementis%d\n"
/*在单链表中查询第一个满足判定条件的数据元素*/
/*若存在,则返回它的位序,否则返回0*/
intLocateElem(LinkListL,ETe)
{inti=0;
while(p)
{i++;
if(p->
data==e)returni;
elsep=p->
return0;
StatusLocateElem_List(LinkListL)
\nPleaseinputtheelement:
flag=LocateElem(L,e);
if(flag)
Locationofa%dintheLAis%d\n"
e,flag);
/*栈的基本操作源程序*/
#include"
stdio.h"
stdlib.h"
alloc.h"
conio.h"
#defineERROR0
#defineOVERFLOW2
#defineSTACK_INIT_SIZE10
#defineSTACKINCREMENT2
typedefcharET;
typedefstructstack{
ET*base,*top;
intstacksize;
}Stack;
StatusInitStack(Stack*S)
{
S->
base=(ET*)malloc(STACK_INIT_SIZE*sizeof(ET));
base)exit(OVERFLOW);
S->
top=S->
base;
stacksize=STACK_INIT_SIZE;
StatusGetTop(Stack*S,ET*e)
{if(S->
top==S->
base)returnERROR;
*e=*(S->
top-1);
StatusPush(Stack*S,ETe)
top-S->
base>
=S->
stacksize)
{S->
base=(ET*)realloc(S->
base,(S->
stacksize+STACKINCREMENT)*sizeof(ET));
base+S->
stacksize;
stacksize+=STACKINCREMENT;
*S->
top++=e;
StatusPop(Stack*S,ET*e)
*e=*--S->
top;
StatusStackEmpty(Stack*S)
base)returnTRUE;
elsereturnFALSE;
voidDestroy_Stack(Stack*S)
base)free(S->
base);
base=NULL;
top=NULL;
stacksize=0;
voidClear_Stack(Stack*S)
{S->
1-------InsertadataintheStack.\n"
2-------DeletetheStacktopdataintheStack.\n"
3-------PrintStacktopelement.\n"
4-------CleartheStack.\n"
5-------DestroytheStack.\n"
voidmenuselect(Stack*S)
{intk,done=1;
InputthedatayouwanttoInsert:
%c"
Push(S,e);
b