這裏寫目錄標題
6-1 求單鏈表的表長 (10分)
本題要求實現一個函數,求帶頭結點的單鏈表的表長。
函數接口定義:
int Length ( LinkList L );
其中LinkList結構定義如下:
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
L是帶頭結點的單鏈表的頭指針,函數Length返回單鏈表的長度。
裁判測試程序樣例:
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
LinkList Create();/* 細節在此不表 */
int Length ( LinkList L );
int main()
{
LinkList L = Create();
printf("%d\n", Length(L));
return 0;
}
/* 你的代碼將被嵌在這裏 */
輸入樣例:
2 1 4 5 3 -1
樣例:
5
解題代碼:
int Length ( LinkList L ){
int l = 0;
if(L==NULL)
return 0;
while(L->next!=NULL&&L->data!=-1)
{
l++;
L=L->next;
}
return l;
}
6-2 帶頭結點的單鏈表插入操作 (10分)
解題代碼:
https://blog.csdn.net/qq_44164791/article/details/104677654
6-3 帶頭結點的單鏈表刪除操作 (10分)
解題代碼:
https://blog.csdn.net/qq_44164791/article/details/104677654
6-5 求單鏈表元素序號 (10分)
本題要求實現一個函數,求帶頭結點的單鏈表中元素序號。
函數接口定義:
int Locate ( LinkList L, ElemType e);
L是帶頭結點的單鏈表的頭指針,e是要查找的元素值。如果e在單鏈表中存在,函數Locate返回其序號(序號從1開始);否則,返回0。
裁判測試程序樣例:
#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
LinkList Create();/* 細節在此不表 */
int Locate ( LinkList L, ElemType e);
int main()
{
ElemType e;
LinkList L = Create();
scanf("%d",&e);
printf("%d\n", Locate(L,e));
return 0;
}
/* 你的代碼將被嵌在這裏 */
輸入樣例:
2 1 4 5 3 -1
5
輸出樣例:
4
解題代碼:
int Locate ( LinkList L, ElemType e)
{
LNode *p;
p=L->next;
int i = 0;
while(p)
{
i++;
if(p->data==e) return i;
p=p->next;
}
}
6-7 帶頭結點的單鏈表就地逆置 (10分)
本題要求編寫函數實現帶頭結點的單鏈線性表的就地逆置操作函數。L是一個帶頭結點的單鏈表,函數ListReverse_L(LinkList &L)要求在不新開闢節點的前提下將單鏈表中的元素進行逆置,如原單鏈表元素依次爲1,2,3,4,則逆置後爲4,3,2,1。
函數接口定義:
void ListReverse_L(LinkList &L);
其中 L 是一個帶頭結點的單鏈表。
裁判測試程序樣例:
//庫函數頭文件包含
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
//函數狀態碼定義
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType; //假設線性表中的元素均爲整型
typedef struct LNode
{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
Status ListCreate_L(LinkList &L,int n)
{
LNode *rearPtr,*curPtr; //一個尾指針,一個指向新節點的指針
L=(LNode*)malloc(sizeof (LNode));
if(!L)exit(OVERFLOW);
L->next=NULL; //先建立一個帶頭結點的單鏈表
rearPtr=L; //初始時頭結點爲尾節點,rearPtr指向尾巴節點
for (int i=1;i<=n;i++){ //每次循環都開闢一個新節點,並把新節點拼到尾節點後
curPtr=(LNode*)malloc(sizeof(LNode));//生成新結點
if(!curPtr)exit(OVERFLOW);
scanf("%d",&curPtr->data);//輸入元素值
curPtr->next=NULL; //最後一個節點的next賦空
rearPtr->next=curPtr;
rearPtr=curPtr;
}
return OK;
}
void ListReverse_L(LinkList &L);
void ListPrint_L(LinkList &L){
//輸出單鏈表
LNode *p=L->next; //p指向第一個元素結點
while(p!=NULL)
{
if(p->next!=NULL)
printf("%d ",p->data);
else
printf("%d",p->data);
p=p->next;
}
}
int main()
{
LinkList L;
int n;
scanf("%d",&n);
if(ListCreate_L(L,n)!= OK) {
printf("表創建失敗!!!\n");
return -1;
}
ListReverse_L(L);
ListPrint_L(L);
return 0;
}
/* 請在這裏填寫答案 */
輸入格式:
第一行輸入一個整數n,表示單鏈表中元素個數,接下來一行共n個整數,中間用空格隔開。
輸出格式:
輸出逆置後順序表的各個元素,兩個元素之間用空格隔開,最後一個元素後面沒有空格。
輸入樣例:
4
1 2 3 4
輸出樣例:
4 3 2 1
解題代碼:
void ListReverse_L(LinkList &L)
{
if(L == NULL || L->next == NULL || L->next->next == NULL)
return ;
LNode *p = L->next->next;
L->next->next = NULL;
while(p)
{
LNode *q = p->next;
p->next = L->next;
L->next = p;
p = q;
}
}
6-8 帶頭結點的鏈式表操作集 (10分)
本題要求實現帶頭結點的鏈式表操作集。
函數接口定義:
List MakeEmpty();
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );
其中List結構定義如下:
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
各個操作函數的定義爲:
List MakeEmpty():創建並返回一個空的線性表;
Position Find( List L, ElementType X ):返回線性表中X的位置。若找不到則返回ERROR;
bool Insert( List L, ElementType X, Position P ):將X插入在位置P指向的結點之前,返回true。如果參數P指向非法位置,則打印“Wrong Position for Insertion”,返回false;
bool Delete( List L, Position P ):將位置P的元素刪除並返回true。若參數P指向非法位置,則打印“Wrong Position for Deletion”並返回false。
裁判測試程序樣例:
#include <stdio.h>
#include <stdlib.h>
#define ERROR NULL
typedef enum {false, true} bool;
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
List MakeEmpty();
Position Find( List L, ElementType X );
bool Insert( List L, ElementType X, Position P );
bool Delete( List L, Position P );
int main()
{
List L;
ElementType X;
Position P;
int N;
bool flag;
L = MakeEmpty();
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
flag = Insert(L, X, L->Next);
if ( flag==false ) printf("Wrong Answer\n");
}
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
P = Find(L, X);
if ( P == ERROR )
printf("Finding Error: %d is not in.\n", X);
else {
flag = Delete(L, P);
printf("%d is found and deleted.\n", X);
if ( flag==false )
printf("Wrong Answer.\n");
}
}
flag = Insert(L, X, NULL);
if ( flag==false ) printf("Wrong Answer\n");
else
printf("%d is inserted as the last element.\n", X);
P = (Position)malloc(sizeof(struct LNode));
flag = Insert(L, X, P);
if ( flag==true ) printf("Wrong Answer\n");
flag = Delete(L, P);
if ( flag==true ) printf("Wrong Answer\n");
for ( P=L->Next; P; P = P->Next ) printf("%d ", P->Data);
return 0;
}
/* 你的代碼將被嵌在這裏 */
輸入樣例:
6
12 2 4 87 10 2
4
2 12 87 5
輸出樣例:
2 is found and deleted.
12 is found and deleted.
87 is found and deleted.
Finding Error: 5 is not in.
5 is inserted as the last element.
Wrong Position for Insertion
Wrong Position for Deletion
10 4 2 5
解題代碼:
List MakeEmpty() {
List L = (List)malloc(sizeof(List));
L->Next=NULL;
return L;
}
Position Find( List L, ElementType X ) {
L=L->Next;
while(L) {
if(L->Data==X) {
return L;
}
L=L->Next;
}
return ERROR;
}
bool Insert( List L, ElementType X, Position P ) {
List p = (List)malloc(sizeof(List));
p->Data=X;
p->Next=NULL;
List k=L;
while(k) {
if(k->Next==P) {
p->Next=P;
k->Next=p;
return true;
}
k=k->Next;
}
printf("Wrong Position for Insertion\n");
return false;
}
bool Delete( List L, Position P ){
if(L==P){
L=L->Next;
return true;
}
while(L){
if(L->Next==P){
L->Next=P->Next;
return true;
}
L=L->Next;
}
printf("Wrong Position for Deletion\n");
return false;
}
7-1 兩個有序鏈表序列的合併 (17分)
已知兩個非降序鏈表序列S1與S2,設計函數構造出S1與S2合併後的新的非降序鏈表S3。
輸入格式:
輸入分兩行,分別在每行給出由若干個正整數構成的非降序序列,用−1表示序列的結尾(−1不屬於這個序列)。數字用空格間隔。
輸出格式:
在一行中輸出合併後新的非降序鏈表,數字間用空格分開,結尾不能有多餘空格;若新鏈表爲空,輸出NULL。
輸入樣例:
1 3 5 -1
2 4 6 8 10 -1
輸出樣例:
1 2 3 4 5 6 8 10
解題代碼:
#include<stdio.h>
#include<stdlib.h>
#define ERROR 0
#define OK 1
typedef int ElemType;
typedef struct LNode{
ElemType Data;
struct LNode * Next;
}LNode, * List;
List Create_L();
List CombineToL(List L1,List L2);
int main(){
List L,L1,L2;
int m,n,i;
L1=Create_L();
L2=Create_L();
L=CombineToL(L1,L2);
if(!L)
printf("NULL"); //鏈表爲空時
else{
while(L->Next){
printf("%d ",L->Data);
L=L->Next;
}
printf("%d",L->Data);
}
return 0;
}List Create_L(){
List head,L,pro;
int n;
L=(List)malloc(sizeof(LNode));
head=L;
scanf("%d",&n);
if(n==-1){
L=NULL;
return L;
}
while(1){
if(n==-1){ //序列結束符號
pro->Next=NULL; //序列尾指向NULL
free(L); //釋放多餘節點
return head;
}
L->Data=n;
L->Next=(List)malloc(sizeof(LNode));
pro=L;
L=L->Next;
scanf("%d",&n);
}
}List CombineToL(List L1,List L2){
List L,head;
L=(List)malloc(sizeof(LNode)); //建立一個空節點,起到頭結點的作用,便於後續拼接
head=L;
while(L1&&L2){
if(L1->Data<=L2->Data){
L->Next=L1;
L=L->Next;
L1=L1->Next;
}
else{
L->Next=L2;
L=L->Next;L2=L2->Next;
}
}
if(L1){ //L2進行到空或者L2初始爲空
L->Next=L1;
L=head;
head=head->Next;
free(L);
return head;
}
else if(L2){ //L1進行到空或者L1初始爲空
L->Next=L2;
L=head;
head=head->Next;
free(L);
return head;
}
else //兩者初始皆爲空
return NULL;
}
7-2 單鏈表的創建及遍歷 (17分)
讀入n值及n個整數,建立單鏈表並遍歷輸出。
輸入格式:
讀入n及n個整數。
輸出格式:
輸出n個整數,以空格分隔(最後一個數的後面沒有空格)。
輸入樣例:
在這裏給出一組輸入。例如:
2
10 5
輸出樣例:
在這裏給出相應的輸出。例如:
10 5
解題代碼:
#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node*next;
}linklist;
linklist *CreatListR(int n){
int i,m;
linklist *head,*s,*r;
head=(linklist*)malloc(sizeof(linklist));
r=head;
for(i=0;i<n;i++){
s=(linklist*)malloc(sizeof(linklist));
scanf("%d",&m);
s->data=m;
r->next=s;
r=s; }
r->next=NULL;
return head;}
int main(){
int n;
scanf("%d",&n);
if(n<=0) return 0;
linklist *s;
s=CreatListR(n);
s=s->next;
printf("%d",s->data);
while(s->next!=NULL){
s=s->next;
printf(" %d",s->data);
}
return 0;
}