请看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);
}
一次性成功💖
Comments NOTHING