代码有参考,只粘了一道题的题干,剩下的懒得弄了,题目在PTA | 程序设计类实验辅助教学平台 (pintia.cn)
若有错误还请指正,感谢!
6-1单链表逆转
本题要求实现一个函数,将给定的单链表逆转。
函数接口定义:
List Reverse( List L );
其中List
结构定义如下:
typedef struct Node *PtrToNode; struct Node { ElementType Data; /* 存储结点数据 */ PtrToNode Next; /* 指向下一个结点的指针 */ }; typedef PtrToNode List; /* 定义单链表类型 */
L
是给定单链表,函数Reverse
要返回被逆转后的链表。
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node { ElementType Data; PtrToNode Next; };
typedef PtrToNode List; List Read(); /* 细节在此不表 */
void Print( List L ); /* 细节在此不表 */
List Reverse( List L );
int main() {
List L1, L2;
L1 = Read();
L2 = Reverse(L1);
Print(L1);
Print(L2);
return 0;
} /* 你的代码将被嵌在这里 */
输入样例:
5
1 3 4 5 2
输出样例:
1
2 5 4 3 1
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
List Reverse(List L){
List p=NULL,q=NULL;
while(L){
q=L->Next;
L->Next=p;
p=L;
L=q;
}
return p;
}
6-2 顺序表操作集
List MakeEmpty(){
List L;
L=(List)malloc(sizeof(struct LNode));
L->Last=-1;
return L;
}
Position Find(List L,ElementType X){
for(int i=0;i<=L->Last;i++){
if(L->Data[i]==X){
return i;
}
}
return ERROR;
}
bool Insert(List L,ElementType X,Position P){
if(L->Last+1==MAXSIZE){
printf("FULL");
return false;
}
if(P<0||P>L->Last+1){
printf("ILLEGAL POSITION");
return false;
}
for(int 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){
if(P<0||P>L->Last){
printf("POSITION %d EMPTY",P);
return false;
}
for(int i=P+1;i<L->Last+2;i++){
L->Data[i-1]=L->Data[i];
}
L->Last--;
return true;
}
6-3 求链式表的表长
int Length(List L){
int i=0;
List p=L;
while(p){
i++;
p=p->Next;
}
return i;
}
6-4 链式表的按序号查找
ElementType FindKth( List L, int K ){
int cnt=1;
PtrToLNode tmp;
tmp=L;
while(tmp&&cnt<K){
tmp=tmp->Next;
cnt++;
}
if(tmp&&cnt==K){
return tmp->Data;
}else{
return ERROR;
}
}
6-5 链式表操作集
Position Find(List L,ElementType X){
List p=L;
while(p){
if(p->Data==X){
return p;
}
p=p->Next;
}
return ERROR;
}
List Insert(List L,ElementType X,Position P){
List head=L;
List p=(List)malloc(sizeof(List));
p->Data=X;
p->Next=NULL;
if(L==P){
p->Next=L;
return p;
}
while(L){
if(L->Next==P){
p->Next=L->Next;
L->Next=p;
return head;
}
L=L->Next;
}
printf("Wrong Position for Insertion\n");
return ERROR;
}
List Delete(List L,Position P){
if(L==P){
L=L->Next;
return L;
}
List head=L;
while(L){
if(L->Next==P){
L->Next=P->Next;
return head;
}
L=L->Next;
}
printf("Wrong Position for Deletion\n");
return ERROR;
}
6-6 带头结点的链式表操作集
List MakeEmpty(){
List L=(PtrToLNode)malloc(sizeof(struct LNode));
L->Next=NULL;
return L;
}
Position Find(List L,ElementType X){
List tmp=L->Next;
while(tmp){
if(tmp->Data==X){
return tmp;
}
tmp=tmp->Next;
}
return ERROR;
}
bool Insert(List L,ElementType X,Position P){
List tmp=L;
List p=(List)malloc(sizeof(struct LNode));
p->Data=X;
while(tmp){
if(tmp->Next==P){
p->Next=tmp->Next;
tmp->Next=p;
return true;
}
tmp=tmp->Next;
}
printf("Wrong Position for Insertion\n");
return false;
}
bool Delete(List L,Position P){
List tmp=L;
while(tmp){
if(tmp->Next==P){
tmp->Next=P->Next;
free(P);
return true;
}
tmp=tmp->Next;
}
printf("Wrong Position for Deletion\n");
return false;
}