c语言编写的带头节点的增删改查

c语言编写的带头节点的增删改查

// 带头节点的链表的增删改差
#include <stdio.h>
#include<stdlib.h>
#include <time.h>
typedef  struct LNode{
	int data;
	struct LNode *next;
}LNode;


LNode * List_HeadInsert(int n){//头插法建立链表
	LNode *head,*s;
	int a;
	head = (LNode *)malloc(sizeof(LNode));
	if(head == NULL){
		printf("建立失败\n");
		return 0;
		
	}
	head->next = NULL;
	for(int i=0;i<n;i++){
		scanf("%d",&a);

		s = (LNode *)malloc(sizeof(LNode));
		if(head == NULL){
			printf("新建节点失败\n");
			return 0;
			
		}
		s->next = head->next;
		s->data=a;
		head->next = s;
	}
	
	return head;
	
}


LNode * create(int n){//尾插法创建一个有n个节点且带头节点的单链表
	LNode *head,*p,*s;
	int a;
	head = (LNode *)malloc(sizeof(LNode));
	head->next = NULL;
	p = head;
	if (head == NULL) {
		printf("建立失败\n");
		return 0;
	}
	printf("请输入%d个数字\n",n);
	for(int i=0;i<n;i++){
		scanf("%d",&a);
		s = (LNode *)malloc(sizeof(LNode));
		if(s == NULL){
			printf("节点建立失败");
			return 0;
			
		}
		s->data = a;
		s->next = NULL;
		p->next = s;
		p = p->next;
	}
	
	return head;
	
}

/********此下面的操作皆为以尾插法为前提进行***********************/



LNode * InsertList(LNode *L,int n,int elem)//在第n个位置插入元素elem
{LNode *p,*s;
	p=L;
	for(int i=1;i<n;i++){
		if(p == NULL){
			printf("插入位置错误");
			return p;
		}
		p=p->next;
	}
	
	s=(LNode *)malloc(sizeof(LNode));
	if(s == NULL){
		printf("节点建立失败");
		return 0;
		
	}
	s->data = elem;
	s->next = p->next;
	p->next = s;
	printf("在第%d位置插入数据%d成功\n",n,elem);
	return L;
}


LNode * DeletList(LNode *L,int n){//删掉第i个数据
	LNode *p,*s;
	p=L;
	for (int i=1; i<n; i++) {
	if (p == NULL) {
		printf("删除位置节点错误");
		return L;
	}
		p=p->next;
	}
	s = p->next;
	p->next = p->next->next;
	free(s);
	printf("删除第%d位的数据成功\n",n);
	return L;
	
}

LNode * ChangeList(LNode *L,int n,int elem){ //将第n位的数据更改为elem
	LNode *p;
		p=L;
		for(int i=1;i<n;i++){
			
			if(p == NULL){
				printf("更改位置错误");
				return p;
			}
			p = p->next;
		}
		p->next->data = elem;
		printf("将第%d位的节点的数据更改为%d成功\n",n,elem);
		return L;
	
}


LNode * SearchList1(LNode *L,int n){//按位查找
	LNode *p;
		p=L;
		for(int i=1;i<n;i++){
			if(p == NULL){
				printf("查找位置错误");
				return p;
			}
			p = p->next;
		}
		printf("第%d位的数据为%d\n",n,p->next->data);
	return L;

}

void SearchList2(LNode *L,int elem){//按值查找
	LNode *p;
	p=L;
	int i = 0;
	while(p->next){
i++;
		p = p->next;

	if (p->data == elem) {
						break;
	}
}
	printf("数值%d在此链表中的位置为:%d\n",elem,i);

	//return i;

}



void display(LNode *L) {//遍历输出链表
	LNode *temp = L;  //创建一个临时结点用来遍历
	temp = L;
	//只要temp->next不指向NULL就输出
	printf("此链表的存储数据为\n");
	while (temp->next) {
		temp = temp->next;
		printf("%d", temp->data);
	}
	printf("\n");
}

	

int main(){
	LNode *L,*G;
	int a,b,c;
	printf("请输入头插法初始链表的个数:");
	scanf("%d",&a);

	G = List_HeadInsert(a);
	display(G);
	
	printf("请输入尾插法初始链表的个数:");
	scanf("%d",&b);

	L = create(b);
	display(L);
	
	L = InsertList(L, 2, 4);
	display(L);
	
	
	L = DeletList(L, 3);
	display(L);
	
	L = ChangeList(L, 4, 8);
	display(L);
	
	L = SearchList1(L, 4);
	
	SearchList2(L, 8);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值