1、单向循环链表
#include "my_head.h"
typedef int ElemType_t;
//设计一个链表节点 结构体数据类型
typedef struct node{
ElemType_t data;//数据域
struct node *next;//指针域
}Node_t;
//创建一条只有头节点得链表并且初始化
Node_t * creat_head_list()
{
//新建链表头节点--申请一个结构体Node_t的空间
Node_t *head = malloc(sizeof(Node_t));
if(head == NULL)
{
printf("malloc head error!\n");
return NULL;
}
//初始化
head->next = head;//头节点下一个指向回头节点
return head;
}
void insert_list(Node_t *head,ElemType_t inputData)
{
//新建一个节点并初始化
Node_t *newNode = malloc(sizeof(Node_t));
if(newNode == NULL)
printf("malloc error!\n");
newNode->data = inputData;
newNode->next = head;
//遍历链表,找到最后一个节点
Node_t *p = NULL;
for(p=head;p->next!=head;p=p->next);
//将新节点插入链表尾部
p->next = newNode;
return;
}
void show_list(Node_t *head)
{
//遍历链表
Node_t *p = NULL;
for(p=head->next;p!=head;p=p->next)
{
printf("p->data:%d\t",p->data);
}
printf("\n");
return;
}
int main(int argc,char *argv[])
{
//定义一条链表
Node_t *head = NULL;
head = creat_head_list();
insert_list(head,10);
insert_list(head,30);
insert_list(head,50);
insert_list(head,70);
show_list(head);
return 0;
}
/*my_head.h*/
#ifndef MY_HEAD_H_
#define MY_HEAD_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdbool.h>
#endif
二、双向循环链表
#include "my_head.h"
typedef int ElemType_t;
typedef struct node{
ElemType_t data;
struct node *next;
struct node *pre;
}Node_t;
Node_t *creat_head()
{
//为头节点申请空间
Node_t *head = malloc(sizeof(Node_t));
if(head == NULL)
printf("malloc head error!\n");
//为头节点赋值
head->next = NULL;
head->pre = NULL;
//将头节点返回
return head;
}
void insert_nodeTolist_tail(Node_t *head,ElemType_t inputData)
{
//为新节点创建空间
Node_t *newNode = malloc(sizeof(Node_t));
if(newNode == NULL)
{
printf("malloc newNode error!\n");
return;
}
newNode->data = inputData;
newNode->next = NULL;
//遍历链表,找最后一个节点
Node_t *p = NULL;
for(p=head;p->next!=NULL;p=p->next);
//让最后一个节点的后继指针指向新节点
//让新节点的前驱指针指向最后一个节点
p->next = newNode;
newNode->pre = p;
return ;
}
void print_alllistToshow_forward(Node_t *head)
{
Node_t *p = NULL;
for(p=head->next;p!=NULL;p=p->next)
{
printf("p->data:%d\t",p->data);
}
printf("\n");
return;
}
void print_alllistToshow_backward(Node_t *head)
{
Node_t *p = NULL;
for(p=head;p->next!=NULL;p=p->next);
for(;p!=head;p=p->pre)
{
printf("p->data:%d\t",p->data);
}
printf("\n");
return;
}
Node_t *insert_listTosort(Node_t *head,ElemType_t sortData)
{
Node_t *newNode = malloc(sizeof(Node_t));
if(newNode == NULL)
{
printf("malloc newNode error!\n");
return NULL;
}
newNode->data = sortData;
Node_t *q = NULL;
Node_t *p = NULL;
for(p=head->next;p!=NULL;q=p,p=p->next)
{
if(p->data > sortData)
break;
}
if(p == NULL)
{
q->next = newNode;//插入的数据最大
}
else
{
if(p == head->next)
{
newNode->next = head->next;
head->next = newNode;
}
else
{
q->next = newNode;
newNode->next = p;
p->pre = newNode;
newNode->pre = q;
}
}
return head;
}
void insert_nodeTolist_head(Node_t *head,ElemType_t Headdata)
{
Node_t *newNode = malloc(sizeof(Node_t));
if(newNode == NULL)
return;
newNode->data = Headdata;
newNode->next = head->next;
newNode->pre = head;
if(head->next!=NULL) //代表后面有人
{
head->next->pre = newNode; //就让后面那个人指向我
}
head->next = newNode;
return;
}
void delete_listNodedata(Node_t *head,ElemType_t delData)
{
Node_t *p = NULL;
Node_t *q = NULL;
for(q=head,p=head->next;p!=NULL;q=p,p=p->next)
{
if(p->data == delData)
{
q->next = p->next;
if(p->next != NULL)
{
p->next->pre = q;
}
free(p);
return;
}
else
{
printf("no data!\n");
}
}
return;
}
void gedit_listNodedata(Node_t *head , ElemType_t geditData, ElemType_t newData)
{
Node_t *newNode = malloc(sizeof(Node_t));
if(newNode == NULL)
return;
Node_t *p = NULL;
for(p=head;p->next!=NULL;p=p->next)
{
if(p->data == geditData)
{
printf("have this data!\n");
break;
}
}
p->data = newData;
printf("gedit success,newdata:%d\n",p->data);
return;
}
void delete_list(Node_t *head)
{
Node_t *p = NULL;
Node_t *q = NULL;
for(p=q=head;p!=NULL;p=q)
{
q = p->next;
free(p);
}
return;
}
int main(int argc ,char *argv[])
{
//创建一条链表
Node_t *head = NULL;
head = creat_head();
//尾插数据
insert_nodeTolist_tail(head,20);
insert_nodeTolist_tail(head,40);
insert_nodeTolist_tail(head,60);
insert_nodeTolist_tail(head,80);
insert_nodeTolist_tail(head,100);
//头插
insert_nodeTolist_head(head,10);
insert_nodeTolist_head(head,8);
//打印正序链表
print_alllistToshow_forward(head);
//打印反序链表
print_alllistToshow_backward(head);
printf("---------------------------------\n");
//有序插入数据
head = insert_listTosort(head,33);
//打印正序链表
print_alllistToshow_forward(head);
//打印反序链表
print_alllistToshow_backward(head);
printf("---------------------------------\n");
//删除某一个数据
delete_listNodedata(head,8);
//打印正序链表
print_alllistToshow_forward(head);
//打印反序链表
print_alllistToshow_backward(head);
//修改某一个数据
gedit_listNodedata(head,100,111);
//打印正序链表
print_alllistToshow_forward(head);
//打印反序链表
print_alllistToshow_backward(head);
printf("---------------------------------\n");
//删除整条链表
delete_list(head);
//打印正序链表
print_alllistToshow_forward(head);
//打印反序链表
print_alllistToshow_backward(head);
return 0;
}