指针与链表

1.应用指针实现顺序后移

(谭浩强,《C程序设计(第四版)》,清华大学出版社:第八章,P291,第4题)
有n个整数,使前面各数顺序向后移m个位置,最后m个数变成最前面m个数,见下图所示,输入n个整数和输出调整后的n个整数。
在这里插入图片描述
设计实现函数void f(int *a,int n,int m),函数的功能为将整型数组a存储的n个数依次后移m个位置,最后m个数变成最前面m个数。
裁判测试程序样例:

#include <stdio.h>
#include <string.h>
#define N 20

/* 请在这里填写答案 */

int main(void){
    int i,n,m;
    int a[N];
    scanf("%d",&n);
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    scanf("%d",&m);
    f(a,n,m);
    for(i=0;i<n;i++)
        printf("%5d",a[i]);
    return 0;
}

输入格式:
第一行输入整数个数n(n<20)。 第二行输入n个整数,数据间以空格为间隔。 第三行输入后移量m。

输出格式:
输出从大到小排序后的5个字符串,一行一个。

输入样例:
6
1 2 3 4 5 6
3

输出样例:
4 5 6 1 2 3

//主要的思路是利用指针将指针代表的数组中的数据调换位置
//一次一次遍历
//由于在函数中可以利用函数递归 

void  f(int *a,int n,int m){
	int* p;
	int be;
	be=*(a+n-1);
	for(p=a+n-1;p>a;p--){
		*p=*(p-1);	
	}//目的:遍历除了最后一位其他的都向右边移动一位 (数值) 
	*p=be;//目的:最后一位挪动到第一位回来(数值) 
	m--;
	if(m) f(a,n,m);//利用递归(这道题我觉得用循环也是可以的) 
	return ; 
	
}

2.应用字符串指针实现字符串排序功能

程序输入5个字符串(字符数小于N=20),实现从大到小排序后输出排序后的各字符串。
设计实现函数void f(char (*c)[N],int n),函数的功能为将字符串数组c从大到小排序。
裁判测试程序样例:

#include <stdio.h>
#include <string.h>
#define N 20

/* 请在这里填写答案 */

int main(void){
    int i;
    char c[5][N];
    for(i=0;i<5;i++)
        gets(c[i]);
    f(c,5);
    for(i=0;i<5;i++)
        puts(c[i]);
    return 0;
}

输入格式:
输入5个字符串(字符数小于N=20),一行一个。

输出格式:
输出从大到小排序后的5个字符串,一行一个。

输入样例:
123
234
124
243
521
输出样例:
521
243
234
124
123

strcpy函数,即string copy(字符串复制)的缩写。strcpy是一种C语言的标准库函数,strcpy把含有’\0’结束符的字符串复制到另一个地址空间,返回值的类型为char*。

//本题我的问题在于不知道二维数组内容如何换序                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    oid f(char (*c)[N],int n){
	int i,j,k;
     for(i=0;i<4;i++){
        k=i;
        for(j=i+1;j<5;j++){
            if(strcmp(c[k],c[j])<0)
                k=j;   
		}
        if(k!=i){
            char temp[100];
            strcpy(temp, c[k]);
            strcpy(c[k], c[i]);
            strcpy(c[i], temp);
            //到这里就类似于去一个中间变量
			//temp=a;a=b;b=temp;这个意思 
        }
	}
	return; 
}

3.链表原地逆序

在不申请新结点的情况下,利用原有的结点空间将给定的单链表逆序重新排列。
其中链表结点结构如下:

struct node{//链表结点结构;
    int data;//用于存储结点信息
    struct node *next;
};

函数struct node * create(int n)的作用是构建有n个元素的单链表,并将头结点指针返回。
函数void print(struct node *head)的作用是将head指向的链表(有头结点)输出。
函数void rev(struct node head)的作用是将head指向的链表(有头结点)原地逆置翻转。

###输入格式: main(void)中首先输入整数n,调用create(n)构建有n个元素的单链表,构建链表时在一行中依次输入n个整数,各数之间以空格为间隔。

###输出格式: 构建链表后,main(void)调用print(head)函数输出构建后的链表;在调用 rev(head)逆置链表后再调用print(head)函数输出逆置后的链表。

裁判测试程序样例:

#include<stdio.h>
#include<stdlib.h>
struct node{//链表结点结构
    int data;
    struct node * next;
};
struct node * create(int n){//构建有n个元素的链表
    struct node *head=NULL,*p=NULL,*q=NULL;
    int i;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    p=head;
    for(i=0;i<n;i++){
        q=(struct node *)malloc(sizeof(struct node));
        q->next=NULL;
        scanf("%d",&(q->data));
        p->next=q;
        p=q;
    }
    return head;
}
//以上的链表添加元素也需要熟练掌握

/* 请在这里填写答案 */

void print(struct node *head){//链表输出
    while(head->next){
        head=head->next;
        printf("%5d",head->data);
    }
    printf("\n");
}
int main(){
    struct node *head=NULL;
    int n;
    scanf("%d",&n);
    head=create(n);
print(head);
    rev(head);
    print(head);
    return 0;
}

输入样例:
10
1 2 3 4 5 6 7 8 9 10
输出样例:
1 2 3 4 5 6 7 8 9 10
10 9 8 7 6 5 4 3 2 1

void rev(struct node* head){
	//目的:只要交换head的位置(也就是从头挪到尾,头尾遍历一遍) 
    struct node* p=head->next;
    //这个操作是给p赋初值,让p等于第一个节点的地址,当然这一步和下面一步骤不可换序
    head->next=NULL;
    //这个操作是让指针的头节点指向空,也就是说让head节点不再和第一个节点保持联系
    while (p)
    {
        struct node* s=p->next;
        //这里的s指针是作为记录指针而存在的
        //主要目的是在这一次遍历中我要让链表中的顺序完全逆序倒置。
        p->next=head->next;
        //让头指针head的下一位指向线性表中的(逆序)
        head->next=p;
        //head的下一位是我刚刚倒置完成的序列
        p=s;
	}
	//上述循环的操作是
	return ;
}

知道我表述的不清楚,不懂就上图
在这里插入图片描述

在此我也想要介绍一下链表的插入方法之一:尾插法

struct node * create(int n){//构建有n个元素的链表
    struct node *head=NULL,*p=NULL,*q=NULL;
    int i;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    p=head;
    for(i=0;i<n;i++){
        q=(struct node *)malloc(sizeof(struct node));
        q->next=NULL;
        scanf("%d",&(q->data));
        p->next=q;
        p=q;
    }
    return head;
}

如果要申请链表的空间,那么我们应该加上#include<stdlib.h>头文件,那么在此我们也介绍一下head=(struct node *)malloc(sizeof(struct node));其实就是申请了malloc(sizeof(struct node))申请了一个动态结构体空间,并将指针强制转换成指向struct node类型,另外需要注意的是,在声明了指针变量一定要将指针变量先初始化为NULL,防止成为野指针。
尾插法需要使用两个指针p和q,这两个指针一个用来申请新的内存空间q=(struct node *)malloc(sizeof(struct node));
一个用来和新的内存空间建立联系,再让现在已经成为链表的p和新申请的q节点建立联系,最后让p指针直接指向q节点。

4.链表删除操作

有a,b两个链表,链表中每个结点的信息包括学号(不超过8个字符),姓名(不超过20个字符)。要求定义结点结构体类型,编写程序实现从a链表中删去与b链表中学号相同的那些结点,main(void)中调用struct Node *create(void)构建a、b链表,并将操作后的a链表输出。

设计要求:
(1)设计并定义struct Node类型,每个结点的数据包括学号num(不超过8个字符,char[]型),姓名name(不超过20个字符,char[]型)。
(2)设计实现函数void delNode(struct Node *a,struct Node *b),其功能是实现从a链表中删去与b链表中学号相同的那些结点。
(3)设计实现函数struct Node * create(void),其功能为通过输入结点信息来构建链表,输入的格式为:第1行学号,第2行姓名,输入的学号为“0”时表示输入结束。
裁判测试程序样例:

#include<stdio.h>

/* 请在这里填写答案 */

void print(struct Node *head){
    struct Node *p=head->next;
    while(p)    {
        printf("%s-%s\n",p->num,p->name);
        p=p->next;
    }
}
int main(void){
    struct Node *a,*b;
    a=create();
    b=create();
    delNode(a,b);
    print(a);
    return 0;
}
#include<string.h>
#include<stdlib.h>
struct Node{
	char num[9];
	char name[20];
	struct Node *next;
}; 
struct Node * create(void){
	struct Node*head=NULL,* p=NULL,*q=NULL;
	head=(struct Node *)malloc(sizeof(struct Node));
	head->next=NULL;
	int pan=0;
	p=head;
	while(1){
		q=(struct Node *)malloc(sizeof(struct Node));
		q->next=NULL;
		scanf("%s",q->num);
		if(strcmp(q->num,"0")==0)
		return head;
		else{scanf("%s",q->name); 
		p->next=q;
		p=q;
		}	
		
	}
	
}
void delNode(struct Node *a,struct Node *b){
	struct Node *l=NULL,*r=NULL,*j=NULL;j=a;
	while(b->next!=NULL){
		l=a;b=b->next;
		while(a->next!=NULL){
			a=a->next;
			if(strcmp(a->num,b->num)==0){
				l->next=l->next->next;
			}
				l=l->next;
		}
		a=j;
	} return;
}

链表解决约瑟夫问题

n个人围成一圈, 从1到n顺序排号。从第k个人开始报数(从1到m报数),凡报到m的人退出圈子,问最后留下的是原来第几号。
main(void)输入n、k、m的值
对于n个人组成的约瑟夫环,当从第k个人开始报数(从1到m报数),凡报到m的人退出约瑟夫环的问题,设计实现函数struct node *joseph(int n,int k,int m) 应用单链表结构返回最后留下的人的信息。
链表结点结构:

struct node{//链表结点结构;
    int data;//用于存储人员序号
    struct node *next;
};

输入格式:
在一行中输入人数n,开始序号k和报数m的值,各数之间以空格为间隔。

输出格式:
输出最后留下的人的序号。

裁判测试程序样例:

#include<stdio.h>
#include<stdlib.h>
struct node{//链表结点结构;
    int data;
    struct node *next;
};

/* 请在这里填写答案 */

int main(void){
    int n,m,k;
    struct node *head;
    scanf("%d%d%d",&n,&k,&m);
    head=joseph(n,k,m);
    printf("%d",head->data);
    free(head);
    return 0;
}

约瑟夫问题就是链表的删除操作,直到最后一个同学自己指向自己为止.

struct node *joseph(int n,int k,int m){
	struct node*head=NULL,*p=NULL,*q=NULL,*t=NULL;
	head=(struct node *)malloc(sizeof(struct node));
	q=head;
	int i=1;
	while(n--){
		p=(struct node *)malloc(sizeof(struct node));
		p->next=NULL;
		q->next=p;
		p->data=i++;
		q=q->next;
		//p->next=NULL;
	}
	q=head->next;
	t=p;
	p->next=head->next;k--;
	while(k--){
		q=q->next;
		t=t->next;//t是比q落后的一位 
	}
	m--;
	while(q->next!=q){
		int o=m;
		while(o--){	
			q=q->next;t=t->next;
		}
		q=t;
		q->next=q->next->next;
	}
	return q;
}

数组解决约瑟夫问题

洛谷–>约瑟夫问题

#include<stdio.h>
int main(){
	bool a[10000]={0};
	int n,m,p=0;
	scanf("%d %d",&n,&m);
	for(int i=1;i<=n;i++){
		
		for(int j=0;j<m;j++){
			if(++p>n) p=1;if(a[p]) j--;
		}
		printf("%d ",p);a[p]=1;
	}
	return 0;
} 
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值