一、单向链表
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct list_node
{
int num;
struct list_node *next;
};
struct list_node *init_list_head()
{
struct list_node *head = NULL;
head = malloc(sizeof(struct list_node));
head->next = NULL;
return head;
}
void insert_list_head(struct list_node *head, int num)
{
struct list_node *new = malloc(sizeof(struct list_node));
new->next = NULL;
new->num = num;
new->next = head->next;
head->next = new;
}
void insert_list_last(struct list_node *head, int num)
{
struct list_node *new = malloc(sizeof(struct list_node));
new->next = NULL;
new->num = num;
struct list_node *p = NULL;
for (p = head; p->next != NULL; p = p->next)
; //p指向最后一个节点
new->next = p->next;
p->next = new;
}
void show_list_first(struct list_node *head)
{
struct list_node *p = NULL;
for (p = head->next; p != NULL; p = p->next)
{
printf("%d\n", p->num);
}
}
void delete_list_node(struct list_node *head, int num)
{
struct list_node *p = NULL;
struct list_node *q = NULL;
for (q = head, p = head->next; p != NULL; q = p, p = p->next)
{
if (p->num == num)
{
q->next = p->next;
free(p);
}
}
}
void delete_list(struct list_node *head)
{
struct list_node *p = NULL;
struct list_node *q = NULL;
for (q = p = head; p != NULL; p = q)
{
q = p->next;
free(p);
return 0;
}
return -1;
}
int main()
{
//初始话链表头节点
struct list_node *head = init_list_head();
int num = 10;
//像链表中插入信息,头插法
insert_list_head(head, num);
insert_list_head(head, 20);
//尾插法
show_list_first(head);
printf("#\n");
insert_list_last(head, 30);
insert_list_last(head, 40);
//向后遍历
show_list_first(head);
//向前遍历,链表结构体中没指向前的指针,没办法向前遍历
//删除节点
delete_list_node(head, 20);
printf("#\n");
show_list_first(head);
//删除链表
delete_list(head);
return 0;
}
二、单向循环链表
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct list_node{
int num;
struct list_node*next;
};
struct list_node* init_list_node(){
struct list_node*head=malloc(sizeof(struct list_node));
head->next=head;
return head;
}
void insert_node_head(struct list_node*head,int num){
struct list_node*new=malloc(sizeof(struct list_node));
new->num=num;
new->next=head->next;
head->next=new;
}
void insert_node_last(struct list_node*head,int num){
struct list_node*new=malloc(sizeof(struct list_node));
new->next=NULL;
new->num=num;
struct list_node*p=NULL;
for(p=head;p->next!=head;p=p->next);
new->next=p->next;
p->next=new;
}
void show_list_first(struct list_node*head){
struct list_node*p=NULL;
for(p=head->next;p!=head;p=p->next){
printf("%d\n",p->num);
}
}
int deldete_list_node(struct list_node*head,int num){
struct list_node*p=NULL;
struct list_node*q=NULL;
for(q=head,p=head->next;p!=head;q=p,p=p->next){
if(p->num==num){
q->next=p->next;
free(p);
return 0;
}
}
return-1;
}
void delete_list(struct list_node*head){
struct list_node*p=NULL;
struct list_node*q=NULL;
for(p=q=head;p!=head;p=q){
q=p->next;
free(p);
}
}
int main(){
//创建头节点
struct list_node*head=init_list_node();
//头插法插入数据
insert_node_head(head,10);
insert_node_head(head,20);
printf("#\n");
show_list_first(head);
//尾插法插入数据
insert_node_last(head,30);
insert_node_last(head,40);
printf("#\n");
//向后遍历链表
show_list_first(head);
//删除节点
deldete_list_node(head,20);
printf("#\n");
show_list_first(head);
//删除链表
delete_list(head);
}
三、双向不循环链表
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct list_node{
int num;
struct list_node*next;
struct list_node*prev;
};
struct list_node*init_list_node(){
struct list_node*head=malloc(sizeof(struct list_node));
head->next=NULL;
head->prev=NULL;
return head;
}
void insert_list_head(struct list_node*head,int num){
struct list_node*new=malloc(sizeof(struct list_node));
new->num=num;
new->next=NULL;
new->prev=NULL;
new->next=head->next;
new->prev=head;
if(head->next!=NULL){
head->next->prev=new;
}
head->next=new;//这里是不能放到上面的
}
void insert_list_last(struct list_node*head,int num){
struct list_node*new=malloc(sizeof(struct list_node));
new->num=num;
new->next=NULL;
new->prev=NULL;
struct list_node*p=NULL;
for(p=head;p->next!=NULL;p=p->next);
new->next=p->next;
new->prev=p;
p->next=new;
}
void show_list_first(struct list_node*head){
struct list_node*p=NULL;
for(p=head->next;p!=NULL;p=p->next){
printf("%d\n",p->num);
}
}
void show_list_last(struct list_node*head){
struct list_node *p = NULL;
for (p = head; p->next != NULL; p = p->next)
;
for (; p != head; p = p->prev)
{
printf("%d\n", p->num);
}
}
int delete_list_node(struct list_node*head,int num){
struct list_node*p=NULL;
struct list_node*q=NULL;
for(q=head,p=head->next;p!=NULL;q=p,p=p->next){
if(p->num==num){
q->next=p->next;
if(p->next!=NULL){
p->next->prev=q;
}
free(p);
return 0;
}
}
}
void delete_list(struct list_node*head){
struct list_node*p=NULL;
struct list_node*q=NULL;
for(p=q=head;p!=NULL;p=q){
q=p->next;
free(p);
}
}
int main(){
//创建头节点
struct list_node*head=init_list_node();
printf("#\n");
//头插法
insert_list_head(head,10);
insert_list_head(head,20);
show_list_first(head);
printf("#\n");
//尾插法
insert_list_last(head,30);
insert_list_last(head,40);
//从首部遍历
show_list_first(head);
printf("#\n");
//从尾部遍历
show_list_last(head);
printf("#\n");
//删除节点
delete_list_node(head,20);
show_list_first(head);
//删除链表
delete_list(head);
return 0;
}
四、双向循环链表
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct list_node
{
int num;
struct list_node *next;
struct list_node *prev;
};
struct list_node *init_list()
{
struct list_node *head = malloc(sizeof(struct list_node));
head->prev = head;
head->next = head;
return head;
}
void insert_list_head(struct list_node *head, int num)
{
struct list_node *new = malloc(sizeof(struct list_node));
new->num = num;
new->next = NULL;
new->prev = NULL;
new->next = head->next;
new->prev = head;
head->next->prev = new;
head->next = new;
}
void insert_list_last(struct list_node *head, int num)
{
struct list_node *new = malloc(sizeof(struct list_node));
new->num=num;
new->next = head;
new->prev = NULL;
struct list_node *p = NULL;
p = head->prev;
p->next = new;
new->prev = p;
head->prev = new;
}
void show_list_head(struct list_node *head)
{
struct list_node *p = NULL;
for (p = head->next; p != head; p = p->next)
{
printf("%d\n", p->num);
}
}
void show_list_last(struct list_node *head)
{
struct list_node *p = NULL;
for (p = head->prev; p != head; p = p->prev)
{
printf("%d\n", p->num);
}
}
int delete_list_node(struct list_node *head, int num)
{
struct list_node *p = NULL;
struct list_node *q = NULL;
for (q = head, p = head->next; p != head; q = p, p = p->next)
{
if (p->num == num)
{
q->next=p->next;
p->next->prev=q;
free(p);
return 0;
}
}
return -1;
}
void delete_list(struct list_node *head)
{
struct list_node *p = NULL;
struct list_node *q = NULL;
for (p = q = head; p != head; p = q)
{
q = p->next;
free(p);
}
}
int main()
{
//申请头指针
struct list_node *head = init_list();
//头插法
insert_list_head(head, 10);
insert_list_head(head, 30);
show_list_head(head);
printf("#\n");
//尾插法
insert_list_last(head, 20);
insert_list_last(head, 40);
//首部开始遍历
show_list_head(head);
printf("#\n");
//尾部遍历
show_list_last(head);
printf("#\n");
//删除节点
delete_list_node(head, 20);
show_list_last(head);
printf("#\n");
//删除链表
delete_list(head);
return 0;
}