12.10 耗时几分钟一次性成功了,单循环链表的增删和打印

发布于 2024-12-10  75 次阅读


请看VCR:

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include<stdlib.h>

typedef struct node {
	int data;
	struct node* next;
}node;

node* create(int data) {
	node* newnode = malloc(sizeof(node));
	newnode->data = data;
	newnode->next = NULL;
}

void insert(int data, node** head) {
	node* newnode = create(data);
	if (*head == NULL) {
		*head = newnode;
		newnode->next = *head;
	}
	else {
		node* temp = *head;
		while (temp->next != *head) {
			temp = temp->next;
		}
		temp->next = newnode;
		newnode->next = *head;
	}
}

void delete(int data, node** head) {
	node* cur = *head;
	node* pre = NULL;
	do{
		if (data == cur->data) {
			if (cur == *head) {
				if (cur->next == *head) {
					*head = NULL;
					free(cur);
				}
				else {
					*head = cur->next;
					free(cur);
					cur = *head;
				}
			}
			else {
				pre->next = cur->next;
				free(cur);
				cur = pre->next;
			}
		}
		else {
			pre = cur;
			cur = cur->next;
		}
	} while (cur != *head);
}

void show(node* head) {
	node* temp = head;
	while (1) {
		printf("%d ", temp->data);
		if (temp->next == head) {
			break;
		}
		temp = temp->next;
	}
}

int main() {
	int a, b, c;
	scanf("%d %d %d", &a, &b, &c);
	node* head = NULL;
	insert(a, &head);
	insert(b, &head);
	insert(c, &head);
	int d;
	scanf("%d", &d);
	if (d == a) {
		delete(a, &head);
	}
	else if (d == b) {
		delete(b, &head);
	}
	else if (d == c) {
		delete(c, &head);
	}
	show(head);
	free(head);
}

一次性成功💖

届ける言葉を今は育ててる
最后更新于 2024-12-10