C语言基本数据结构之一(线性链表的增,删,改,查及倒序)

这学期刚好学了计算机软件基础,便顺带把C的基本数据结构过一遍,在期末的时候抽空整理了一下知识点,以免以后忘记了,如有错误,希望大家能帮忙改正 大笑废话不多说, 直接上代码,里面注释也相对详细
#include <stdio.h>
#include <malloc.h>
#define MAX 100
typedef struct LinkList list;
struct LinkList{
	int data;
	struct LinkList *next;
};
list *ListHead(){
	 list *L= (list *) malloc(sizeof(list));
	 L->next = NULL;
	return L;
}
/*
初始化线性列表
*/
void initList(list *L){
	printf("\n\n  输入0退出 \n ");
	int in = 1,d;
	while(in!=0){
		list *n = (list *) malloc(sizeof(list));
		n->next=NULL;
		printf("\n  请输入list数据 \n");
		scanf("%d",&d);
		n->data = d;
		L->next = n;
		L = L->next;
		printf("\n  继续输入?0退出 \n ");
		scanf("%d",&in);
	}
	return ;
}
/*
对线性列表遍历
*/
void selectAll(list *L){
	printf("\n\n  select all : \n");
	list *p;
	p=L;
	int i =-1;
	while(NULL!=p->next){
		i++;
		p = p->next;
		printf("\n  the %d position  data is %d \n",i,p->data);
	}
	return;
}
/*
根据位置查询
*/
void selectWhere(list *L,int a){
	printf("\n\n  selectWhere :\n");
	int n = -1;
	list *p = L;
	while((NULL!=p->next)&&(n<a)){
		n++;
		p = p->next;
	}
	if(a>n){
		printf("\n  无此点数据\n");
		return;
	}
	printf("\n  the  %d position  data is %d \n",n,p->data);
	return;
}
/*
根据位置添加数据
*/
void add(list *L,int pos,int data){
	printf("\n\n  add data \n\n");
	if((NULL==L) || (pos < 0)){
		printf("\n  error ! \n");
		return;
	} 
	list *p = L;//后继指针
	list *q =  L->next;
	list *n;
	int a = 0;
	while((NULL!= q->next) && ( a< pos)){
		p = q;
		q = q->next;
		a++;
	}
	printf("\n  a is %d pos is %d",a,pos);
	if(a <pos){
		printf( "\n  position  %d is  error! \n",pos);
		return;
	}
	n = (list * )malloc(sizeof(list));
	n->data = data;
	printf("\n   data is %d \n",n->data);
	n->next = q;
	p->next = n;
}
/*
获取线性列表的长度
*/
void getLenght(list *L){
	printf("\n\n  get  list Lenght \n");
	list *p = L;
	int a = 0;
	while(NULL!=p->next){
		a++;
		p = p->next;
	}
	printf("\n  list lenght is %d \n",a);
	return;
}
/**
修改位置的数据
*/
void change(list *L, int pos,int data){
	printf("\n\n  change data \n");
	if((NULL==L) || (pos < 0)){
		printf("\n  error ! \n");
		return;
	} 
	//list *p = L;//后继指针
	list *q =  L;
	list *n;
	int a = -1;
	while((NULL!= q) && ( a< pos)){
		q = q->next;
		a++;
	}
	printf("\n   a is %d pos is %d",a,pos);
	if(a < pos){
		printf( "\n  position  %d is  error! \n",pos);
		return;
	}
	q->data = data;
}
/*
删除指定位置,并显示剩余数据
*/
void DeletAShow(list *L,int b){
	printf("\n\n  Delete and show :\n");
	int a = -1;
	list *pre,*p = L;

	while((NULL!=p->next)&&(a!=b)){
		a++;
		pre = p;
		p = p->next;
	}
	if(b>a){
		printf( "\n  position  %d is  error! \n",b);
		return;
	}
	printf("\n  the Delete position is  %d and the data is %d \n",a,p->data);
	pre->next = p->next;
	free(p);
	p = L;
	printf("\n  the leave data is :  \n");
	while(NULL!=p->next){
		p = p->next;
		printf("  [ %d ]  ",p->data);
	}
	return ;
}
/*
将线性列表倒序
*/
list *over(list *H){
	printf("\n\n  over list \n ");
	list *r,*q,*p;
	r = H;
	p = r->next;
	q = p->next;
	if(NULL==p){
		printf("\n  list is null !\n");
		return NULL;
	}
	while((NULL!=q)){
		printf("\n  q is not null \n");
		p->next = r;
		r = p;
		p = q;
		q = q->next;
	}
	H->next = NULL;
	p->next = r;
	return p;
}
void main(){
	list *Head,*p;
	Head = ListHead();
	//添加数据
	initList(Head);
	//遍历所有数据
	selectAll(Head);
	//根据位置获取数据
	selectWhere(Head,1);
	//获取list长度
	getLenght(Head);
	//删除所给位置相应的数据并显示剩余数据
	DeletAShow(Head,1);
	//添加数据
	add(Head,1,1111);
	printf("\n  添加数据后:\n ");
	p = Head;
	while(NULL!=p->next){
		p = p->next;
		printf("\n  data is %d \n", p->data);
	
	}
	//修改数据
	change(Head,0,222);
	printf("\n  修改数据后:\n ");
	p = Head;
	while(NULL!=p->next){
		p = p->next;
		printf("\n  data is %d \n", p->data);
	
	}
	//将list反序
	p = over(Head);
	printf("\n  反序后:\n ");
	while(NULL!=p->next){
		printf("\n  data is %d \n",p->data);
		p = p->next;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值