c单向链表 gcc 7.3.0

1、核心

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct stu{
	char name[20];
	char num[20];
	struct stu *next;
};
struct stu *CreateList(){
	struct stu *p,*next,*head;	
	char name1[20];
	char num1[20];
	int n=0;
	while('q' !=name1[0]){
		n++;
		if(1==n) {
			head=(struct stu *)malloc(sizeof(struct stu));
			p=head;
			printf("%d号学生的名字:(q回到菜单)",n);
			scanf("%s",name1);getchar();
			if('q' == name1[0] && '\0' == name1[1]) 
				return head;
			printf("%d号学生的电话号码:",n);
			scanf("%s",num1);getchar();
            		strcpy(head->name,name1);
            		strcpy(head->num,num1);
			continue;
        	}
        	printf("%d号学生的名字:(q回到菜单)",n);
		scanf("%s",name1);getchar();
		if('q' == name1[0] && '\0' == name1[1]) 
			return head;
		printf("%d号学生的电话号码:",n);
		scanf("%s",num1);getchar();

        	next=(struct stu *)malloc(sizeof(struct stu));		
		strcpy(next->name,name1);
		strcpy(next->num,num1);
		p->next=next;
		p=next;
	}
	p->next=NULL;
	return head;
}//创建链表
//输出链表
void PrintList(struct stu *head){		
	struct stu *p;	
	int pos=1;
    	p=head;
	if(NULL == p){
		printf("当前表中无数据\n");
		return;
	}
	printf("\n序号\t姓名\t学生:\n");
	while(p){
		printf("%d\t%s\t%s\n",pos++,p->name,p->num);
		p = p->next;
	}
}//输出链表
int  main(){
	struct stu *p;
	p=CreateList();
	PrintList(p);
}

2、全部的样子

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct stu{
	char name[20];
	char num[20];
	struct stu *next;
};
struct stu *CreateList(){
	struct stu *p,*next,*head;	
	char name1[20];
	char num1[20];
	int n=0;
	while('q' !=name1[0]){
		n++;
		if(1==n) {
			head=(struct stu *)malloc(sizeof(struct stu));
			p=head;
			printf("%d号学生的名字:(q回到菜单)",n);
			scanf("%s",name1);getchar();
			if('q' == name1[0] && '\0' == name1[1]) 
				return head;
			printf("%d号学生的电话号码:",n);
			scanf("%s",num1);getchar();
            		strcpy(head->name,name1);
            		strcpy(head->num,num1);
			continue;
        	}
        	printf("%d号学生的名字:(q回到菜单)",n);
		scanf("%s",name1);getchar();
		if('q' == name1[0] && '\0' == name1[1]) 
			return head;
		printf("%d号学生的电话号码:",n);
		scanf("%s",num1);getchar();

        	next=(struct stu *)malloc(sizeof(struct stu));		
		strcpy(next->name,name1);
		strcpy(next->num,num1);
		p->next=next;
		p=next;
	}
	p->next=NULL;
	return head;
}//创建链表

//输出链表
void PrintList(struct stu *head){		
	struct stu *p;	
	int pos=1;
    	p=head;
	if(NULL == p){
		printf("当前表中无数据\n");
		return;
	}
	printf("\n序号\t姓名\t学生:\n");
	while(p){
		printf("%d\t%s\t%s\n",pos++,p->name,p->num);
		p = p->next;
	}
}//输出链表
//扩展部分
//插入
struct stu * ListInsert(struct stu *head,int pos){
	struct stu *p,*insert;	
	int i;
    	p=head;
	char name1[20];
	char num1[20];
	if(NULL == p){
		printf("链表为空,需要重新建表\n");
		p=CreateList();
		return p;
	}
	printf("请输入学生的名字:");
	scanf("%s",name1);getchar();
	printf("请输入学生电话号码:");
	scanf("%s",num1);getchar();
	insert=(struct stu *)malloc(sizeof(struct stu));
	strcpy(insert->name,name1);
	strcpy(insert->num,num1);
	if(0 == pos){//处理第一个前插入
		insert->next=head;
		head=insert;
		return head;
	}
	for(i=0;i<pos-1;i++){//p在pos处
		p=p->next;
	}
	if(NULL == p) {
		printf("数据位置超出\n");
		return head;
	}

	insert->next=p->next;
	p->next=insert;
	return head;
}
struct stu *ListDelete(struct stu *head,int pos){//删除第几个学生数据
	struct stu *p,*q;	
	int i;
    p=head;
	if(1==pos){
		q=p;
		p=p->next;
		free(q);
		return p;
	}
	for(i=0+2;i<pos;i++){
		p=p->next;
		if(NULL==p->next){
			printf("位置输入错误");
			return head;
		}
	}
	q=p->next;
	if(NULL==q->next){
		p->next=NULL;
		free(q);
		return head;
	}
	p->next=q->next;
	free(q);
	return head;
	
}
//查找
int  main(){
	struct stu *p;
	char chooce;
    	int pos;
	p=CreateList();
	PrintList(p);
	printf("\n*************************************\n");
	printf("1、打印链表\n2、插入数据\n");
	printf("3、删除数据\n4、查找数据\n");
	printf("请输入选择<1-2-3-4>(按其他任意键退出):");
	scanf("%c",&chooce);
	getchar();
	while(1){		
		if('1'==chooce){
			PrintList(p);
		}else if('2'==chooce){
			printf("输入插入第几个学生后面\n");
			scanf("%d",&pos);
			getchar();
			p=ListInsert(p,pos);
		}else if('3'==chooce){
			printf("输入删除第几个学生:");
			scanf("%d",&pos);
			getchar();
			p=ListDelete(p,pos);
		}else if('4'==chooce){
			printf("暂不支持\n");	
		}else{
			break;
		}
		printf("\n*************************************\n");
		printf("1、打印链表\n2、插入数据\n");
		printf("3、删除数据\n4、查找数据\n");
		printf("\n*************************************\n");
		printf("请输入选择<1-2-3-4>(按其他任意键退出):");
		scanf("%c",&chooce);
		getchar();			
	}
}

3、截图效果
图片.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值