链表的基本操作(增,删,查,找)

#include <stdio.h>
#include <stdlib.h>

//1.Declarative node structure
struct node{
	int data;
	struct node *next;
};
//5.Declarative
void printfLinkList(struct node *head);
void insertLinkList(struct node *head,int location,int value);
void deleteLinkList(struct node *head,int location);
void updateLinkList(struct node *head,int oldValue,int newValue);
void main(){
	//2.initialization
	int num[] = {20,21,3,19,19,45};
	int length = sizeof(num)/sizeof(num[0]);
	struct node *head = NULL, *tail = NULL,*newNode = NULL;
	head = (struct node*)malloc(sizeof(struct node*));
	head->data=0;
	tail=head;
	//3.Connect the new node to the linked list
	for(int i = 0 ; i<length;i++){
		newNode = (struct node*)malloc(sizeof(struct node*));
		newNode->data=num[i];
		newNode->next=NULL;
		tail->next=newNode;
		tail=tail->next;
	}
	head->data=length;
	//4.1 query
	printf("query:\n");
	printfLinkList(head);
	//4.2 insert
	int location = 0,value = 88;
	insertLinkList(head,location,value);
	printfLinkList(head);
	//4.3 delete
	location = 1;
	deleteLinkList(head,location);
	printfLinkList(head);
	//4.4 update
	int	oldValue = 21;
	int	newValue = 88;
	updateLinkList(head,oldValue,newValue);
	printfLinkList(head);
}
//6.definition
void printfLinkList(struct node *head){
	struct node * tail;
	tail=head->next;
	while(tail!=NULL){
		printf("%4d",tail->data);
		tail=tail->next;
	};
	printf("\n");
}
void insertLinkList(struct node *head,int location,int value){
	printf("insert:\n");
	struct node*tail;
	tail = head;
	int i = 0;
	//1.find the location of you want to insert
	while(i<location){
		tail=tail->next;
		i++;
	}
	//2.creates a new node to insert
	struct node * newNode = NULL;
	newNode = (struct node *)malloc(sizeof(struct node*));
	newNode->data = value;
	newNode->next = NULL;
	//3.connects the back of tail to become the new node's next
	newNode->next = tail->next;
	//4.connects the new node to the back of tail
	tail->next = newNode;
	(head->data)++;
}
void deleteLinkList(struct node *head,int location){
	printf("delete:\n");
	struct node *tail;
	tail = head;
	int i = 0;
	//1.find the location before the node you want to delete
	while(i<location){
		tail=tail->next;
		i++;
	}
	//2.creates a new node to save the location of delete
	struct node *delNode;
	//3.replace the delete location after the tail to be delNode 
	delNode = tail->next;
	//4.connects tail and the location of after delNode
	tail->next = delNode->next;
	//5.free the space of delNode
	free(delNode);
	(head->data)--;
}
void updateLinkList(struct node *head,int oldValue,int newValue){
	printf("update\n");
	struct node *tail;
	bool flag = false;
	tail = head;
	//1.Determine if the value exists find it
	while(true){
		//2.if it dosen't exist exit
		if(tail->next==NULL)
			break;
		//3.if find it change the flag to be true and exit
		if(tail->data==oldValue){
			flag = true;
			break;
		}
		tail=tail->next;
	}
	//4.if the flag is false print error message otherwise replace the oldValue to newValue
	if(flag == false){
		printf("the %d is not exist",oldValue);
	}else{
		tail->data=newValue;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值