吉林大学超星高级语言程序设计 实验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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章