6-1 鏈表逆置(20 分)函數
本題要求實現一個函數,將給定單向鏈表逆置,即表頭置爲表尾,表尾置爲表頭。鏈表結點定義以下:測試
struct ListNode {spa
int data;設計
struct ListNode *next;指針
};code
函數接口定義:blog
struct ListNode *reverse( struct ListNode *head );接口
其中head是用戶傳入的鏈表的頭指針;函數reverse將鏈表head逆置,並返回結果鏈表的頭指針。element
裁判測試程序樣例:it
#include <stdio.h>
#include <stdlib.h>
struct ListNode {
int data;
struct ListNode *next;
};
struct ListNode *createlist(); /*裁判實現,細節不表*/
struct ListNode *reverse( struct ListNode *head );
void printlist( struct ListNode *head )
{
struct ListNode *p = head;
while (p) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
struct ListNode *head;
head = createlist();
head = reverse(head);
printlist(head);
return 0;
}
/* 你的代碼將被嵌在這裏 */
輸入樣例:
1 2 3 4 5 6 -1
輸出樣例:
6 5 4 3 2 1
struct ListNode *reverse( struct ListNode *head ) { struct ListNode *p,*q=NULL,*r=NULL; if(head==NULL) { return NULL; } if(head->next==NULL) { return head; } p=head; while(p!=NULL) { q=p->next; p->next=r; r=p; p=q; } return r; }
6-2 求鏈式表的表長(10 分)
本題要求實現一個函數,求鏈式表的表長。
函數接口定義:
int Length( List L );
其中List結構定義以下:
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode List;
L是給定單鏈表,函數Length要返回鏈式表的長度。
裁判測試程序樣例:
#include <stdio.h>
#include <stdlib.h>
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode List;
List Read(); /* 細節在此不表 */
int Length( List L );
int main()
{
List L = Read();
printf("%d\n", Length(L));
return 0;
}
/* 你的代碼將被嵌在這裏 */
輸入樣例:
1 3 4 5 2 -1
輸出樣例:
5
int Length( List L ) { if(L==NULL) { return 0; } else { int len=1; while(L->Next!=NULL) { len++; L=L->Next; } return len; } }
6-3 鏈式表操做集(20 分)
本題要求實現鏈式表的操做集。
函數接口定義:
Position Find( List L, ElementType X );
List Insert( List L, ElementType X, Position P );
List Delete( List L, Position P );
其中List結構定義以下:
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
各個操做函數的定義爲:
Position Find( List L, ElementType X ):返回線性表中首次出現X的位置。若找不到則返回ERROR;
List Insert( List L, ElementType X, Position P ):將X插入在位置P指向的結點以前,返回鏈表的表頭。若是參數P指向非法位置,則打印「Wrong Position for Insertion」,返回ERROR;
List Delete( List L, Position P ):將位置P的元素刪除並返回鏈表的表頭。若參數P指向非法位置,則打印「Wrong Position for Deletion」並返回ERROR。
裁判測試程序樣例:
#include <stdio.h>
#include <stdlib.h>
#define ERROR NULL
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
ElementType Data;
PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;
Position Find( List L, ElementType X );
List Insert( List L, ElementType X, Position P );
List Delete( List L, Position P );
int main()
{
List L;
ElementType X;
Position P, tmp;
int N;
L = NULL;
scanf("%d", &N);
while ( N-- ) {
scanf("%d", &X);
L = Insert(L, X, L);
if ( L==ERROR ) 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 {
L = Delete(L, P);
printf("%d is found and deleted.\n", X);
if ( L==ERROR )
printf("Wrong Answer or Empty List.\n");
}
}
L = Insert(L, X, NULL);
if ( L==ERROR ) printf("Wrong Answer\n");
else
printf("%d is inserted as the last element.\n", X);
P = (Position)malloc(sizeof(struct LNode));
tmp = Insert(L, X, P);
if ( tmp!=ERROR ) printf("Wrong Answer\n");
tmp = Delete(L, P);
if ( tmp!=ERROR ) printf("Wrong Answer\n");
for ( P=L; 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
Position Find( List L, ElementType X ) { if(L==NULL) { return ERROR; } else { while(L!=NULL) { if(L->Data==X) { return L; } L=L->Next; } } return ERROR; } List Insert( List L, ElementType X, Position P ) { List a,q; a=L; q=NULL; if(P==L) { q=(Position)malloc(sizeof(struct LNode)); q->Data=X; q->Next=L; return q; } else { while(L->Next!=P&&L->Next!=NULL) { L=L->Next; } if(L->Next==NULL&&P!=NULL) { printf("Wrong Position for Insertion\n"); return ERROR; } else { q=(Position)malloc(sizeof(struct LNode)); q->Data=X; L->Next=q; q->Next=P; return a; } } } List Delete( List L, Position P ) { List a; a=L; if(L==NULL||P==NULL) { printf("Wrong Position for Deletion\n"); return ERROR; } else { if(P==L) { a=L->Next; return a; } else { while(L->Next!=P&&L->Next!=NULL) { L = L->Next; } if(L->Next==NULL) { printf("Wrong Position for Deletion\n"); return ERROR; } else { L->Next=L->Next->Next; return a; } } } }
7-1 兩個有序鏈表序列的合併(20 分)
已知兩個非降序鏈表序列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> typedef struct Node* List; struct Node { int data; struct Node *Next; }; List InitList(); void print(List L); void read(List L); void combine(List S1,List S2,List S3); int main() { List S1,S2,S3; S1=InitList(); S2=InitList(); S3=InitList(); read(S1); read(S2); combine(S1,S2,S3); print(S3); return 0; } List InitList() { List L; L=(List)malloc(sizeof(struct Node)); if(!L) { return NULL; } L->Next=NULL; return L; } void print(List L) { L=L->Next; if(L==NULL) { printf("NULL"); return; } while(L) { if(L->Next==NULL) printf("%d",L->data); else printf("%d ",L->data); L=L->Next; } } void read(List L) { List p; int x; scanf("%d",&x); while(x!=-1) { p=(List)malloc(sizeof(struct Node)); if(!p) { return; } p->data=x; p->Next=NULL; L->Next=p; L=p; scanf("%d",&x); } } void combine(List S1,List S2,List S3) { S1=S1->Next; S2=S2->Next; while(S1!=NULL&&S2!=NULL) { if(S1->data>S2->data) { S3->Next=S2; S2=S2->Next; } else { S3->Next=S1; S1=S1->Next; } S3=S3->Next; } if(S1==NULL&&S2==NULL) { return; } if(S1!=NULL) { S3->Next=S1; } else { S3->Next=S2; } return; }
7-2 兩個有序鏈表序列的交集(20 分)
已知兩個非降序鏈表序列S1與S2,設計函數構造出S1與S2的交集新鏈表S3。
輸入格式:
輸入分兩行,分別在每行給出由若干個正整數構成的非降序序列,用−1表示序列的結尾(−1不屬於這個序列)。數字用空格間隔。
輸出格式:
在一行中輸出兩個輸入序列的交集序列,數字間用空格分開,結尾不能有多餘空格;若新鏈表爲空,輸出NULL。
輸入樣例:
1 2 5 -1
2 4 5 8 10 -1
輸出樣例:
2 5
#include<stdio.h> #include<stdlib.h> typedef struct Node* List; struct Node { int data; struct Node *Next; }; List InitList(); void print(List L); void read(List L); void Insersection(List S1,List S2,List S3); int main() { List S1,S2,S3; S1=InitList(); S2=InitList(); S3=InitList(); read(S1); read(S2); Insersection(S1,S2,S3); print(S3); return 0; } List InitList() { List L; L=(List)malloc(sizeof(struct Node)); if(!L) { return NULL; } L->Next=NULL; return L; } void print(List L) { L=L->Next; if(L==NULL) { printf("NULL"); return; } while(L) { if(L->Next==NULL) printf("%d",L->data); else printf("%d ",L->data); L=L->Next; } } void read(List L) { List p; int x; scanf("%d",&x); while(x!=-1) { p=(List)malloc(sizeof(struct Node)); if(!p) { return; } p->data=x; p->Next=NULL; L->Next=p; L=p; scanf("%d",&x); } } void Insersection(List S1,List S2,List S3) { List pa,pb,pc,q; pc=S3; pa=S1->Next; pb=S2->Next; while(pa!=NULL&&pb!=NULL) { if(pa->data<pb->data) { pa=pa->Next; } else if(pa->data>pb->data) { pb=pb->Next; } else { q=(List)malloc(sizeof(struct Node)); q->data=pa->data; pc->Next=q; pc=q; pa=pa->Next; pb=pb->Next; } } pc->Next=NULL; }