吉林大學超星高級語言程序設計 實驗09 動態數據組織

填空題,不怎麼難,比較懶直接給程序,自己對着填,給予參考,歡迎大家一起交流 :-)
1
題目編號:Exp09-Basic01

題目名稱:創建單鏈表

題目描述:請填寫缺失代碼完成程序,實現如下功能:

根據從鍵盤隨機輸入以0結束的若干非零整數,建立一個單鏈表;之後將此鏈表中保存的數字順次輸出,相鄰數字間以一個西文空格間隔,最後一個數字後無任何字符;若是空鏈表,則輸出NULL。

例如,

輸入:5 4 2 1 3 0

輸出:5 4 2 1 3

輸入:0 5 4 2 1 3 0

輸出:NULL

#include<stdio.h>
#include<malloc.h>
struct  cell{
   
   
	int x;
	struct cell *next; 
};
struct cell *build(void){
   
   
	struct cell *head,*tmp,*p;
	head=tmp=p=NULL;
	int n;
	head=(struct cell*)malloc(sizeof(struct cell));
	scanf("%d",&head->x);
	tmp=head;
	tmp->next=NULL;
	if(head->x==0)head=NULL;
	else{
   
   
	do{
   
   
		p=(struct cell*)malloc(sizeof(struct cell));
		scanf("%d",&p->x);
		tmp->next=p;
		tmp=p;
		tmp->next=NULL;
	}while(p->x!=0);
}
	return head;
}
void print(struct cell *head){
   
   
	struct cell *p;
	printf("%d",head->x);
	p=head->next;
	while(p->x!=0){
   
   
		printf(" %d",p->x);
		p=p->next;
	}
}
void release(struct cell*head){
   
   
	struct cell *p;
	while(head!=NULL){
   
   
		p=head;
		head=p->next;
		free(p);
	}
}
int main(void) {
   
   
 struct cell* head;
 head = build();
 if(head!=NULL)
        print(head);
    else
        printf("NULL");
 release(head);
}

2
題目編號:Exp09-Basic02,GJBook3-13-06

題目名稱:刪除單鏈表重複結點

題目描述:請填寫缺失代碼完成程序,實現如下功能:

首先根據鍵盤隨機輸入,以0結束的若干非零整數建立單鏈表;然後刪除此鏈表中值重複的結點僅保留一個,且不改變原有結點順序;最後將刪除後鏈表中各結點值輸出,相鄰數字間以一個西文空格間隔,最後一個數字後無任何字符;若是空鏈表,則輸出NULL。

例如,

輸入:5 4 2 1 3 0 輸出:5 4 2 1 3

輸入: 4 2 1 3 3 2 0 輸出:4 2 1 3

輸入: 0 4 2 3 2 0 輸出:NULL

#include<stdio.h>
#include<malloc.h>
struct  cell{
   
   
	int x;
	struct cell *next; 
};
struct cell *build(void){
   
   
	struct cell *head,*tmp,*p;
	head=tmp=p=NULL;
	int n;
	head=(struct cell*)malloc(sizeof(struct cell));
	scanf("%d",&head->x);
	tmp=head;
	tmp->next=NULL;
	if(head->x==0)head=NULL;
	else{
   
   
	do{
   
   
		p=(struct cell*)malloc(sizeof(struct cell));
		scanf("%d",&p->x);
		tmp->next=p;
		tmp=p;
		tmp->next=NULL;
	}while(p->x!=0);
}
	return head;
}
struct cell* del2one(struct cell* head){
   
   
	struct cell *p,*q,*r;
	p=head;
	while(p!=NULL){
   
   
		q=p;
		while(q->next!=NULL){
   
   
			if(q->next->x==p->x){
   
   
				r=q->next;
				q->next=r->next;
			}
			else{
   
   
				q=q->next;
			}
		}
		p=p->next;
	}
	return head;
} 
void print(struct cell *head){
   
   
	struct cell *p;
	printf("%d",head->x);
	p=head->next;
	while(p->x!=0){
   
   
		printf(" %d",p->x);
		p=p->next;
	}
}
void release(struct cell*head){
   
   
	struct cell *p;
	while(head!=NULL){
   
   
		p=head;
		head=p->next;
		free(p);
	}
}
int main(void) {
   
   
struct cell* head;
head = build();
head=del2one(head);
if(head!=NULL)
       print(head);
   else
       printf("NULL");
release(head);
}

這裏我看了別人的代碼,情況更復雜 https://blog.csdn.net/tao_627/article/details/88687243?utm_source=app
3
題目編號 :Exp09-Basic03

題目名稱:求單鏈表中間結點

題目描述:請填寫缺失代碼完成程序,實現如下功能:

首先根據鍵盤隨機輸入,以0結束的若干非零整數建立單鏈表;

然後尋找處於鏈表中間位置的結點,若中間結點有兩個,則設定前一個爲中間位置結點;

最後將從中間結點開始到鏈表尾各結點值輸出,相鄰數字間以一個西文空格間隔,最後一個數字後無任何字符。

若是空鏈表,則輸出NULL。

例如,

輸入:5 4 2 1 3 0

輸出:2 1 3

輸入: 4 2 1 3 3 2 0

輸出:1 3 3 2

#include<stdio.h>
#include<malloc.h>
struct  cell{
   
   
	int x;
	struct cell *next; 
};
struct cell *build(void){
   
   
	struct cell *head,*tmp,*p;
	head=tmp=p=NULL;
	int n;
	head=(struct cell*)malloc(sizeof(struct cell));
	scanf("%d",&head->x);
	tmp=head;
	tmp->next=NULL;
	if(head->x==0)head=NULL;
	else{
   
   
	do{
   
   
		p=(struct cell*)malloc(sizeof(struct cell));
		scanf("%d",&p->x);
		tmp->next=p;
		tmp=p;
		tmp->next=NULL;
	}while(p->x!=0);
}
	return head;
}
struct cell* mid(struct cell *head){
   
   
	struct cell *p0,*p;
	int n=0,i,j=0;
	if(head==NULL)head==NULL;
	else{
   
   
	p=head;
	while(p!=NULL){
   
   
		p=p->next;
		n=n+1;
	}
	j=n/2;
	p0=head;
	while(j!=1){
   
   
		p0=p0->next;
		j--; 
	}
	head=p0;
}
	return head;
}
void print(struct cell *head){
   
   
	struct cell *p;
	printf("%d",head->x);
	p=head->next;
	while(p->x!=0){
   
   
		printf(" %d",p->x);
		p=p->next;
	}
}
void release(struct cell*head){
   
   
	struct cell *p;
	while(head!=NULL){
   
   
		p=head;
		head=p->next;
		free(p);
	}
}
int main(void) {
   
   
 struct cell* head,*half;
 head = build();
 half = mid(head);
 if(half!=NULL)
        print(half);
    else
        printf("NULL");
 release(head);
}

4
題目編號:Exp09-Basic04

題目名稱:單鏈表交換兩結點

題目描述:請填寫缺失代碼完成程序,實現如下功能:

首先根據鍵盤隨機輸入,以0結束的若干非零整數建立單鏈表;

然後根據輸入的兩個索引位置交換鏈表上的兩個結點(鏈表首元素索引爲1,且要交換的兩個索引位置不相鄰);

最後鏈表各結點值輸出,相鄰數字間以一個西文空格間隔,最後一個數字後無任何字符。

若是空鏈表,則輸出NULL。

例如,

輸入:1 2 3 4 5 6 0 1 5

輸出:5 2 3 4 1 6

輸入:0 1 2 3 4 5 6 0 1 5

輸出:NULL

#include <stdio.h>
#include <malloc.h>
struct cell {
   
   //單鏈表結點結構體定義
 int x;
 struct cell* next;
};
struct cell* build(void) {
   
   //新建單鏈表,並將建好的單鏈表首結點地址返回
 struct cell* head, * tmp, * p;
 head = tmp = p = NULL;
 int n;
 head=(struct cell*)malloc(sizeof(struct cell));
 scanf("%d",&head->x);
 tmp=head;
 tmp->next=NULL;
 if(head->x==0)head=NULL;
 else{
   
   
 	do{
   
   
 		p=(struct cell*)malloc(sizeof(struct cell));
 		scanf("%d",&p->x);
 		if(p->x==0)break;
 		tmp->next=p;
 		tmp=p;
 		tmp->next=NULL;
	 }while(p->x!=0);
 }
 return head;//返回單鏈表頭
}
struct cell* swap(struct cell* head,int m,int n) {
   
   //交換索引爲m和n的兩個結點,head是單鏈表首結點指針
 if(head==NULL) return NULL;
    struct cell* pm=head, * pn=head;
 struct cell* pm0 = NULL, * pn0 = NULL;
 struct cell* tmp;
 int i;
 for (i = 1;i < m && pm != NULL;i++) {
   
    
  pm0 = pm;
  pm = pm->next;
 }
 for (i = 1;i < n && pn != NULL;i++) {
   
   
  pn0 = pn;
  pn = pn->next;
 }
 if (pm != NULL && pn != NULL && m != n) {
   
   //索引爲m,n的結點位於鏈表中間
      
  if (pm0 != NULL && pn0 != NULL) {
   
   
  	tmp=pm->next;
  	pm->next=pn->next;
	pn->next=tmp;
	pm0->next=pn;
	pn0->next=pm;
	pm=pm0->next;
	pn=pn0->next; 
  }
  if (pm0 == NULL && pn0 != NULL) {
   
   
  	head=pn;
  	tmp=pn->next;
  	pn->next=pm->next;
  	pn0->next=pm;
  	pm->next=tmp;
  }
  if (pm0 != NULL && pn0 == NULL) {
   
   
  	head=pm;
  	tmp=pm->next;
  	pm->next=pn->next;
  	pm0->next=pn;
  	pn->next=tmp;
  }
 }
 return head;
}
void print(struct cell* head) {
   
   //打印整個單鏈表,head是單鏈表首結點指針
      struct cell *p;
      printf("%d",head->x);
      p=head->next;
      while(p!=NULL){
   
   
      	printf(" %d",p->x);
      	p=p->next;
	  }
}
void release(struct cell* head) {
   
   //釋放單鏈表空間,head是單鏈表首結點指針
      struct cell *p;
	while(head!=NULL){
   
   
		p=head;
		head=p->next;
		free(p);
	}
}
int main(void) {
   
   
 struct cell* head;
 int m, n;
 head = build();
 scanf("%d%d", &m, &n);
 head = swap(head,m,n);
 if(head!=NULL)
        print(head);
    else
        printf("NULL");
 release(head);
}

5
題目編號 :Exp09-Basic05,GJBook3例-13-04

題目名稱:單鏈表存儲法雷序列

題目描述:請填寫缺失代碼完成程序,實現如下功能:

給定一個正整數n,用單鏈表遞增存儲n階法雷序列各項值。n階法雷序列是把所有不可約分的分數j/i(0<i≤n; 0≤j≤i)遞增排序的序列。

輸入一個正整數n;輸出n階法雷序列各項分數形式,分數的分子和分母間以/連接,各個分數間以一個西文空格間隔,最後一個數字後無任何字符。若是空鏈表或n不符合要求,則輸出NULL。

例如,

輸入:3

輸出:0/1 1/3 1/2 2/3 1/1

#include <stdio.h>
#include <malloc.h>
struct  farlei_item {
   
   
 int   numerator, denominator;   // 分子、分母
 struct  farlei_item* next;   // 連接部分
};
typedef  struct  farlei_item* farleipointer;
int  gcd(int x, int y) {
   
       /*  求最大公約數 */
    if(y==0)return x;
	else return gcd(y,x%y); 
}
/*構造法雷序列,並返回序列頭指針*/
farleipointer farlei(int n) {
   
   
 int i, j;
 farleipointer fn, r, r0, p;
 fn = r = r0 = p = NULL;
 if (n < 1)return NULL; //如果n<=0,則沒有法雷序列
 fn = (farleipointer)malloc(sizeof(struct farlei_item));  //構造0/1
 fn->numerator = 0;
 fn->denominator = 1;
 p = (farleipointer)malloc(sizeof(struct farlei_item));   //構造1/1
 p->numerator = 1;
 p->denominator = 1;
 fn->next = p;
 p->next = NULL;
 for(i=2;i<=n;i++){
   
   
 	for(j=1;j<i;j++){
   
   
 		if(gcd(i,j)==1){
   
   
 			r=fn;
 			while(j*(r->denominator)>i*(r->numerator)){
   
   
 				r0=r;
 				r=r->next;
			 }
			 p=(farleipointer)malloc(sizeof(struct farlei_item));
			 p->numerator=j;
			 p->denominator=i;
			 p->next=r;
			 r0->next=p;
		 }
	 }
 } 
 return fn;
}
void print(farleipointer fn) {
   
   //輸出fn引導的法雷序列
     farleipointer p;
     printf("%d/%d",fn->numerator,fn->denominator);
     p=fn->next;
     while(p!=NULL){
   
   
     	printf(" %d/%d",p->numerator,p->denominator);
     	p=p->next;
	 }
}
void release(farleipointer head) {
   
   //釋放單鏈表空間,head是單鏈表首結點指針
      farleipointer p;
	while(head!=NULL){
   
   
		p=head;
		head=p->next;
		free(p);
	}
}
int main(void) {
   
   
 int n;
 farleipointer fn;
 scanf("%d", &n);
 fn = farlei(n); //生成n級法雷序列
 if(fn!=NULL)
        print(fn);
    else
        printf("NULL");
 release(fn);
 return 0;
}

課本例題,看書。

6
題目編號:Exp09-Enhance01

題目名稱:單鏈表倒數第K個結點

題目描述:請填寫缺失代碼完成程序,實現如下功能:

首先根據鍵盤隨機輸入,以0結束的若干非零整數建立單鏈表;

然後根據輸入的整數K,輸出鏈表倒數第K個結點的值,相鄰數字間以一個西文空格間隔,最後一個數字後無任何字符;

若不存在則輸出NULL。

例如,

輸入:1 2 3 4 5 6 7 8 0 3

輸出:6



輸入:0 2 3 4 5 6 7 8 0 3

輸出:NULL
#include <stdio.h>
#include <malloc.h>
struct cell {
   
   //單鏈表結點結構體定義
 int x;
 struct cell* next;
};
struct cell* build(void) {
   
   //新建單鏈表,並將建好的單鏈表首結點地址返回
 struct cell* head, * tmp, * p;
 head = tmp = p = NULL;
 int n;
 head=(struct cell*)malloc(sizeof(struct cell));
 scanf("%d",&head->x);
 tmp=head;
 tmp->next=NULL;
 if(head->x==0)head=NULL;
 else{
   
   
 	do{
   
   
 		p=(struct cell*)malloc(sizeof(struct cell));
 		scanf("%d",&p->x);
 		if(p->x==0)break;
 		tmp->next=p;
 		tmp=p;
 		tmp->next=NULL;
	 }while(p->x!=0);
 }
 return head;//返回單鏈表頭
}
struct cell * back(struct cell* head, int k) {
   
   //求鏈表倒數第k個結點,head是單鏈表首結點指針
    struct cell *p,*p0;
    int n=1,i;
    if(head==NULL)head=NULL;
    else{
   
   
    p=head;
    while(p->next!=NULL){
   
   
    	n++;
    	p=p->next;
	}
	if(k>n){
   
   head=NULL;return head;
}
	n=n-k;
	p0=head;
	for(i=n;i>0;i--){
   
   
	 p0=p0->next;
}
head=p0;
p0->next=NULL;
}
return head;
}
void release(struct cell* head) {
   
   //釋放單鏈表空間,head是單鏈表首結點指針
 struct cell *p;
	while(head!=NULL){
   
   
		p=head;
		head=p->next;
		free(p);
	}
}
int main(void) {
   
   
 struct cell* head,*result;
 int k;
 head = build();
 scanf("%d", &k);
 result = back(head,k);
 if (result != NULL)
        printf("%d",result->x);
    else
        printf("NULL");
 release(head);
}

7
題目編號:Exp09-Enhance03

題目名稱:合併單鏈表

題目描述:請填寫缺失代碼完成程序,實現如下功能:

首先從鍵盤輸入一行以0結束的若干非零整數,建立一個單鏈表,輸入的整數順序爲數字非遞減順序;

然後以同樣的方式,仍在第一行繼續輸入並建立第二個單鏈表;

之後將兩個鏈表合併形成一個新鏈表,使得新鏈表依然保持數字非遞減順序;

最後驗證輸出新鏈表所有值,相鄰數字間以一個西文空格間隔,最後一個數字後無任何字符。若是空鏈表,則輸出NULL。

例如,

輸入:2 3 4 4 5 6 0 1 3 4 6 7 0

輸出:1 2 3 3 4 4 4 5 6 6 7



輸入:0 0

輸出:NULL
#include <stdio.h>
#include <malloc.h>
struct cell {
   
   //單鏈表結點結構體定義
 int x;
 struct cell* next;
};
struct cell* build(void) {
   
   //新建單鏈表,並將建好的單鏈表首結點地址返回
 struct cell* head, * tmp, * tail;
 head = tmp = tail = NULL;
 int n, i;
    head=(struct cell*)malloc(sizeof(struct cell));
    scanf("%d",&head->x);
    tmp=head;
    tmp->next=NULL;
    if(head->x==0)head=NULL;
    else{
   
   
    	do{
   
   
    	tail=(struct cell*)malloc(sizeof(struct cell));
    	scanf("%d",&tail->x);
    	if(tail->x==0)break; 
    	tmp->next=tail;
    	tmp=tail;
    	tmp->next=NULL;	
	}while(tail->x!=0);
}
 return head;//返回單鏈表頭
}
struct cell* combine(struct cell* p, struct cell* q) {
   
   //合併兩個鏈表p和q
 struct cell* head= NULL,*p0=NULL,*q0=NULL,*r=NULL;
 if (p == NULL && q!= NULL) return q;
 if (p != NULL && q == NULL) return p;
 if (p == NULL && q == NULL) return NULL;
 if(p!=NULL&&q!=NULL){
   
   
 	head=p;
 	while(head->next!=NULL){
   
   
 		head=head->next;
	 }
	 head->next=q;
	 return p;
 }
}
void print(struct cell* head) {
   
   //打印整個單鏈表,head是單鏈表首結點指針
     struct cell *p,*p0,*r,*r0,*q;
     struct cell *k;
     p0=NULL;
     p=head;
     while(p!=NULL){
   
   
     	r=head;
     	while((r->x<p->x)&&r!=p){
   
   
     		r0=r;
     		r=r->next;
		 }
		 if(r!=p){
   
   
		 	q=p;
		 	p0->next=p->next;
		 	p=p0;
		 	if(r==head){
   
   
		 		q->next=head;
		 		head=q;
			 }else{
   
   
			 	q->next=r;
			 	r0->next=q;
			 }
		 }
		 p0=p;
		 p=p->next;
	 }
     printf("%d",head->x); 
	k=head->next;
	while(k!=NULL){
   
   
		printf(" %d",k->x);
		k=k->next;
	}
}
void release(struct cell* head) {
   
   //釋放單鏈表空間,head是單鏈表首結點指針
 struct cell *p;
	while(head!=NULL){
   
   
		p=head;
		head=p->next;
		free(p);
	}
}
int main(void) {
   
   
 struct cell* head1,*head2, *result;
 head1 = build();
 head2 = build();
 result = combine(head1,head2);
 if (result != NULL)
  print(result);
 else
  printf("NULL");
 release(result);
 return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章