#pragma once
#include <stdio.h>
#include <stdlib.h>
#define count 10
typedef struct nodelist {
int data;
struct nodelist* next;
}list;
list* head;
void init() {
head = (list*)malloc(sizeof(list));
list *p = head;
list* np = NULL;
int i = 0;
while(p !=NULL) {
p->data = i;
if (i < count - 1) {
np = (list*)malloc(sizeof(list));
}
else {
np = NULL;
}
p->next = np;
p = np;
i++;
}
}
void print() {
list * p = head;
while (p != NULL) {
printf("%d\n", p->data);
p = p->next;
}
}
void reverse() {
list* p = head;
list* pre = NULL;
list* cur = NULL;
while (p != NULL) {
cur = p;
p = p->next;
cur->next = pre;
pre = cur;
}
head = cur;
}
void del_node(list* del) {
list* p = head;
if (head == del) {
head = del->next;
}
list* pre = NULL;
while(p != NULL && p != del) {
pre = p;
p = p->next;
}
if (p!=NULL && pre!= NULL) {
pre->next = p->next;
}
free(p);
}
void del_index(int n) {
int i = 0;
list* p = head;
while (i++ != n) {
p = p->next;
}
del_node(p);
}
c语言-链表
最新推荐文章于 2024-04-27 12:39:09 发布