1.单循环链表
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE {
int data;
struct NODE* next;
}NODE,*PNODE;
PNODE init() {
PNODE headnode = (PNODE)malloc(sizeof(NODE));
headnode->data = 0;
headnode->next = headnode;
return headnode;
}
void HeadInsert(PNODE list, int num) {
PNODE NEWNODE = (PNODE)malloc(sizeof(NODE));
NEWNODE->data = num;
NEWNODE->next = list->next;
list->next = NEWNODE;
list->data++;
}
void TailInsert(PNODE list, int num) {
PNODE temp = list;
while (temp->next!=list) {
temp = temp->next;
}
PNODE NEWNODE = (PNODE)malloc(sizeof(NODE));
NEWNODE->data = num;
NEWNODE->next = temp->next;
temp->next = NEWNODE;
list->data++;
}
void traverse(PNODE list) {
PNODE temp = list->next;
while (temp!=list) {
printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL");
}
int isempty(PNODE list) {
if (list->data == 0) {
return 1;
}
else
{
return 0;
}
}
void delete(PNODE list,int pos) {
if (isempty(list)) {
return;
}
if (pos<1 || pos>list->data) {
return;
}
PNODE temp = list;
int cnt = 0;
while (cnt != pos - 1) {
temp = temp->next;
cnt++;
}
PNODE current = temp->next;
temp->next = current->next;
free(current);
list->data--;
}
int main() {
PNODE head = init();
HeadInsert(head, 1);
HeadInsert(head, 2);
HeadInsert(head, 3);
HeadInsert(head, 4);
delete(head, 3);
traverse(head);
return 0;
}
2.双链表
1.注意插入要进行判断
2.注意删除时也要进行判断
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE {
int data;
struct NODE* pre;
struct NODE* next;
}NODE, * PNODE;
PNODE init() {
PNODE headnode = (PNODE)malloc(sizeof(NODE));
headnode->data = 0;
headnode->pre = NULL;
headnode->next = NULL;
return headnode;
}
void HeadInsert(PNODE list, int num) {
PNODE NEWNODE = (PNODE)malloc(sizeof(NODE));
NEWNODE->data = num;
NEWNODE->next = list->next;
NEWNODE->pre = list;
//如果链表中有元素,则将前一个结点和后一个结点与新结点相连
//如果链表为空,则新结点直接与头结点相连
if (list->next) {
list->next->pre = NEWNODE;
list->next = NEWNODE;
}
else
{
list->next = NEWNODE;
}
list->data++;
}
void TailInsert(PNODE list, int num) {
PNODE temp = list;
while (temp->next) {
temp = temp->next;
}
PNODE NEWNODE = (PNODE)malloc(sizeof(NODE));
NEWNODE->data = num;
NEWNODE->pre = temp;
NEWNODE->next = temp->next;
temp->next = NEWNODE;
list->data++;
}
int isempty(PNODE list) {
if (list->data == 0) {
return 1;
}
else
{
return 0;
}
}
void delete(PNODE list, int data) {
//找到需要删除的结点,将此结点的前一个结点与后一个结点相连
//如果需要删除的结点为最后一个结点,则直接将前一个结点的next=node->next
PNODE node = list->next;
while (node) {
if (node->data == data) {
node->pre->next = node->next;
if (node->next) {
node->next->pre = node->pre;
}
free(node);
list->data--;
return;
}
node = node->next;
}
return;
}
void traverse(PNODE list) {
PNODE temp = list->next;
while (temp) {
printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL");
}
int main() {
PNODE head = init();
TailInsert(head, 1);
TailInsert(head, 2);
TailInsert(head, 3);
TailInsert(head, 4);
delete(head, 4);
traverse(head);
return 0;
}
3.双循环链表
#include <stdio.h>
#include <stdlib.h>
typedef struct NODE {
int data;
struct NODE* pre;
struct NODE* next;
}NODE,*PNODE;
PNODE init() {
PNODE headnode = (PNODE)malloc(sizeof(NODE));
headnode->data = 0;
headnode->pre = headnode;
headnode->next = headnode;
return headnode;
}
void HeadInsret(PNODE list,int num) {
PNODE NEWNODE = (PNODE)malloc(sizeof(NODE));
NEWNODE->data = num;
NEWNODE->pre = list;
NEWNODE->next = list->next;
list->next->pre = NEWNODE;
list->next = NEWNODE;
list->data++;
}
void TailInsert(PNODE list, int num) {
PNODE temp = list;
while (temp->next != list) {
temp = temp->next;
}
PNODE NEWNODE = (PNODE)malloc(sizeof(NODE));
NEWNODE->data = num;
NEWNODE->pre = temp;
NEWNODE->next = temp->next;
temp->next->pre = NEWNODE;
temp->next = NEWNODE;
list->data++;
}
void pre_traverse(PNODE list) {
PNODE temp = list->pre;
while (temp != list) {
printf("%d->", temp->data);
temp = temp->pre;
}
printf("NULL\n");
}
void next_traverse(PNODE list) {
PNODE temp = list->next;
while (temp != list) {
printf("%d->", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
void delete(PNODE list, int data) {
PNODE node = list->next;
while (node!=list) {
if (node->data == data) {
node->pre->next = node->next;
node->next->pre = node->pre;
free(node);
list->data--;
return;
}
node = node->next;
}
}
int main() {
PNODE list = init();
HeadInsret(list, 1);
HeadInsret(list, 2);
HeadInsret(list, 3);
HeadInsret(list, 4);
delete(list, 1);
pre_traverse(list);
next_traverse(list);
return 0;
}