函数题
6-1 单链表逆转

List Reverse(List L)
{
List head, tail, mid;
tail = NULL;
mid = L;
while (mid != NULL)
{
head = mid->Next;
mid->Next = tail;
tail = mid;
mid = head;
}
return tail;
}
6-2 顺序表操作集

List MakeEmpty(){
List L;
L=(List)malloc(sizeof(struct LNode));
L->Last=-1;
return L;
}
Position Find( List L, ElementType X ){
Position i=0;
while(i<=L->Last&&L->Data[i]!=X)
i++;
if(i>L->Last) return ERROR;
else return i;
}
bool Insert( List L, ElementType X, Position P ){
Position i;
if(L->Last==MAXSIZE-1){
printf("FULL");
return false;
}
if(P<0||P>L->Last+1){
printf("ILLEGAL POSITION");
return false;
}
for(i=L->Last;i>=P;i--)
L->Data[i+1]=L->Data[i];
L->Data[P]=X;
L->Last++;
return true;
}
bool Delete( List L, Position P ){
Position j;
if(P<0||P>L->Last){
printf("POSITION %d EMPTY",P);
return false;
}
for(j=P;j<L->Last;j++)
L->Data[j]=L->Data[j+1];
L->Last--;
return true;
}
6-3 链式表的按序号查找

ElementType FindKth( List L, int K )
{
if(L==NULL)
return ERROR;
while(--K) //此处用k--会报错,注意前缀和后缀区别
{ //往后移动K-1次,查找第K个元素
if(!L->Next)
return ERROR; //若K值大于链表长度,则返回ERROR
else L=L->Next;
}
return L->Data; //返回第K个元素的数据域
}
6-4 在一个数组中实现两个堆栈

/* 你的代码将被嵌在这里 */
Stack CreateStack( int MaxSize )
{
Stack p=(Stack)malloc(sizeof(struct SNode));
p->MaxSize=MaxSize;
p->Data=(ElementType*)malloc(sizeof(ElementType)*MaxSize);
p->Top1=-1;
p->Top2=MaxSize;
return p;
}
bool Push( Stack S, ElementType X, int Tag )
{
//如果堆栈已满,Push函数必须输出“Stack Full”并且返回false
if(S->Top2-S->Top1==1)
{
printf("Stack Full\n");
return false;
}
if(Tag==1)
{
S->Top1++;
S->Data[S->Top1]=X;
}
else
{
S->Top2--;
S->Data[S->Top2]=X;
}
return true;
}
ElementType Pop( Stack S, int Tag )
{
//如果某堆栈是空的,则Pop函数必须输出“Stack Tag Empty”(其中Tag是该堆栈的编号),并且返回ERROR
if(Tag==1)
{
if(S->Top1==-1)
{
printf("Stack %d Empty\n",Tag);
return ERROR;
}
return S->Data[S->Top1--];
}
else
{
if(S->Top2==S->MaxSize)
{
printf("Stack %d Empty\n",Tag);
return ERROR;
}
return S->Data[S->Top2++];
}
}
6-5 求二叉树高度

int GetHeight( BinTree BT ){
if(BT==NULL)return 0;
int x,y;
x=GetHeight(BT->Left)+1;
y=GetHeight(BT->Right)+1;
return x>y?x:y;
}
6-6 求单链表结点的阶乘和

int factoral( int n );
int FactorialSum( List L ){
int ansFac = 0;
for ( List point = L; point != NULL; point = point->Next ) {
ansFac += factoral(point->Data);
}
return ansFac;}
int factoral( int n )
{
int ans = 1;
if ( n == 0 ) {
ans = 1;
} else {
for ( int i = 1; i <= n; i ++ ) {
ans *= i;}
}
return ans;}
6-7 二叉搜索树的操作集

BinTree Insert( BinTree BST, ElementType X )
{
if(BST==NULL)
{
BinTree gg=(BinTree)malloc(sizeof(struct TNode));
gg->Data=X;
gg->Left=NULL;
gg->Right=NULL;
BST=gg;
return BST;
}
else
{
BinTree hh=BST,parent;
int flag;
while(hh!=NULL)
{
parent=hh;
if(X>hh->Data)
hh=hh->Right,flag=1;
else
hh=hh->Left,flag=0;
}
BinTree tmp=(BinTree)malloc(sizeof(struct TNode));
tmp->Data=X;
tmp->Left=NULL;
tmp->Right=NULL;
hh=tmp;
if(flag)parent->Right=hh;
else parent->Left=hh;
return BST;
}
}
Position Find( BinTree BST, ElementType X )
{
if(BST)
{
while(BST)
{
if(X==BST->Data)return BST;
else if(X>BST->Data)
BST=BST->Right;
else if(X<BST->Data)
BST=BST->Left;
}
}
return BST;
}
Position FindMin( BinTree BST )//找到搜索二叉树中最小值
{
BinTree gg=NULL;
if(BST)
{
while(BST)
gg=BST, BST=BST->Left;
}
return gg;
}
Position FindMax( BinTree BST )//找到搜索二叉树最大值
{
BinTree gg=NULL;
if(BST)
{
while(BST)
gg=BST,BST=BST->Right;
}
return gg;
}
BinTree Delete( BinTree BST, ElementType X )
{
//删除处理的关键在于 删除这个节点
//之后还要就是将后面是节点给补上去
Position tmp;
if(!BST)
{
printf("Not Found\n");
return BST;
}
else if(X<BST->Data)
BST->Left=Delete(BST->Left,X);
else if(X>BST->Data)
BST->Right=Delete(BST->Right,X);
else
{
if(BST->Left && BST->Right) { /* 被删除的结点有左右子结点 */
tmp=FindMin(BST->Right); /* 在右子树中找到最小结点填充删除结点 */
BST->Data = tmp ->Data;
BST->Right=Delete(BST->Right,BST->Data);/* 递归删除要删除结点的右子树中最小元素 */
//这一步无法理解
}else
{
if(!BST->Left)
BST=BST->Right;
else if(!BST->Right)
BST=BST->Left;
}
}
return BST;
}
6-8 二叉搜索树中的最近公共祖先

int find(Tree T,int u)
{
if(!T)
return 0;
if(T->Key==u)
return 1;
if(T->Key<u)
return find(T->Right,u);
if(T->Key>u)
return find(T->Left,u);
}
int LCA( Tree T, int u, int v )
{
if(!T)
return ERROR;
if(!find(T,u)||!find(T,v))
return ERROR;
if(u==T->Key||v==T->Key)
return T->Key;
if(u>T->Key&&v<T->Key||u<T->Key&&v>T->Key)
return T->Key;
if(u>T->Key)
return LCA(T->Right,u,v);
if(u<T->Key)
return LCA(T->Left,u,v);
}
6-9 邻接矩阵存储图的深度优先遍历

void DFS( MGraph Graph, Vertex V, void (*Visit)(Vertex) )
/*第一个参数是传入二维数组的头地址(即图)
第二个参数是结点编号
第三个参数是传入Visit()这个函数的地址(可以自行百度函数是怎么作为参数传递的)*/
{
/*从第V个顶点出发递归地深度优先遍历图G*/
int i;
Visited[V] = true;//标记为true,说明已经遍历过了
Visit(V); //打印出V这个结点
for(i = 0; i < Graph->Nv; i++) //遍历V的每个邻接点
{
if(Graph->G[V][i] == 1 && !Visited[i])
/*Graph->G[V][i] == 1说明有结点,!Visited[i]为真,说明未遍历过*/
{
DFS(Graph, i, Visit); //递归调用DFS
}
}
}
6-10 邻接表存储图的广度优先遍历

void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) )
{
int queue[11]; //定义一个队列
int l = 0, r = 0;
queue[r++] = S;
Visit(S);
Visited[S] = true;
PtrToAdjVNode tmp;
while(l != r) //队列不为空
{
tmp = Graph->G[queue[l++]].FirstEdge;
while(tmp)
{
Vertex pos = tmp->AdjV;
if(!Visited[pos])
{
Visit(pos);
Visited[pos] = true;
queue[r++] = pos;
}
tmp = tmp->Next;
}
}
}
程序填空题








这篇博客聚焦于数据结构的函数题目,包括单链表逆转、顺序表操作、链式表查找、双堆栈实现、二叉树高度计算、链表节点阶乘和、二叉搜索树操作、最近公共祖先查找、邻接矩阵与邻接表的图遍历等算法问题。
4187

被折叠的 条评论
为什么被折叠?



