剑指offer 单向链表增删

E:\arithmetic\Link


#include <stdio.h>
#include <stdlib.h>//molloc free

struct LinkNode;
void printLink(struct LinkNode *head);
void addToLink(struct LinkNode **head, int value);
void deleteLink(struct LinkNode **head, int value);
int main(int argc, char *argv[])
{
	//测试用例 a,添加删除正常值;b,传入链表为NULL;c,删除头结点,删除未节点,删除中间节点,删除不存在的节点 
	int a[] = {1,2,3,4,5,6,7,8,9,0};
	
	struct LinkNode *link1 = NULL;
	int i;
	for(i=0 ; i<10 ; i++){
		addToLink(&link1, a[i]);
	}
	printLink(link1);
	deleteLink(&link1, 1);
	printLink(link1);
	deleteLink(&link1, 0);
	printLink(link1);
	deleteLink(&link1, 12);
	printLink(link1);
	deleteLink(&link1, 4);
	printLink(link1);
	deleteLink(NULL, 4);
	return 0;
}

struct LinkNode {
	int value;
	struct LinkNode *next;
};
void printLink(struct LinkNode *head){
	if(head == NULL){
		printf("this link is NULL, quit!\n");
		return;
	}
	while(head != NULL){
		printf("%d\t", head->value);
		head = head->next;
	}
	printf("\n");
}

void addToLink(struct LinkNode **head, int value){
	struct LinkNode *t = malloc(sizeof(struct LinkNode));
	if(t == NULL){
		printf("malloc failed!\n");
		return;
	}
	t->value = value; //!!
	t->next = NULL;   //!!
	
	if(head == NULL){//!!
		printf("&head is NULL\n");
		return;
	}
	

	if(*head == NULL){//!!
		*head = t;//!!
		return;
	}
	
	struct LinkNode *h = *head;
	while(h->next != NULL)
		h = h->next;
	h->next = t;
}

void deleteLink(struct LinkNode **head, int value){
	if(head == NULL){
		printf("&link is NULL , quit!\n");
		return;
	}
	if(*head == NULL){
		printf("link is NULL, quit!\n");
		return;
	}
	struct LinkNode *t = *head;	
	if(t->value	 == value){
		*head = t->next;
	}
	else{
		struct LinkNode *t2 = NULL;
		while(t->value != value){
			t2 = t;
			t = t->next;
			if(t == NULL){
				printf("failed to find in link!\n");
				return;
			}
		}
		t2->next = t->next;		
	}
	free(t); 
}


单向链表是一种线性的数据结构,每个节点包含数据和向下一个节点的针。以下是C语言中对单向链表进行增删改查的基本操作示例: ```c // 定义链表节点结构体 typedef struct Node { int data; // 节点的数据 struct Node* next; // 向下一个节点的针 } Node; // 创建新节点函数 Node* createNode(int value) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = value; newNode->next = NULL; return newNode; } // 插入节点到链表头部 void insertAtStart(Node** head, int value) { Node* newNode = createNode(value); newNode->next = *head; *head = newNode; } // 插入节点到定位置 void insertAfter(Node* current, int value) { if (current == NULL) { printf("Current node is null.\n"); return; } Node* newNode = createNode(value); newNode->next = current->next; current->next = newNode; } // 删除节点 void deleteNode(Node** head, int key) { Node* temp = *head, *prev = NULL; if (temp != NULL && temp->data == key) { *head = temp->next; free(temp); return; } while (temp != NULL && temp->data != key) { prev = temp; temp = temp->next; } if (temp == NULL) { printf("Element not found in the list.\n"); return; } prev->next = temp->next; free(temp); } // 查找节点 Node* findNode(Node* head, int key) { Node* current = head; while (current != NULL) { if (current->data == key) return current; current = current->next; } return NULL; } // 打印链表 void printList(Node* head) { Node* temp = head; while (temp != NULL) { printf("%d -> ", temp->data); temp = temp->next; } printf("NULL\n"); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值