单链表 循环单链表 循环双链表的各种插入方法及删除方法

一、线性表

定义n个数据的有限集合
特点:除第一个元素外,其他元素有且仅有一个直接前驱
除最后一个元素,其他元素有且仅有一个直接后继

二、顺序表

定义:将线性表中的元素相继存放在一个连续的存储空间中
可利用一维数组描述存储结构
特点:线性表的顺序存储方式
遍历:顺序访问,可以随机存取

三、链表

1、单链表(不带头结点)

特点:每个元素由结点构成
线性结构:
结点可以连续,可以不连续存储
结点的逻辑顺序和物理顺序可以不一致
表可以扩充

结点类型创建

#include<stdio.h>
#include<stdlib.h>
#define MALLOC_OK 1
#define MALLOC_NO 0
#define CREATE_OK 1
#define CREATE_NO 0

struct node
{
	int num;
	char name[20];
	char sex;
	
	struct node * next; //当在结构体中定义指针时 指针类型必须是结构体名称
};

插入

a)头插法

在这里插入图片描述

typedef struct node Node;
typedef Node * Link;

//创建链表
void create_link(Link * head)
{
	*head = NULL;//空链表 头指针指向空
}

int is_malloc_ok(Link new_node)
{
	if(NULL == new_node)
	{
		printf("malloc error!\n");
		return MALLOC_NO;
	}
	
	return MALLOC_OK;
}
void create_node(Link * new_node)
{
	*new_node = (Link)malloc(sizeof(Node));
	if(MALLOC_OK == is_malloc_ok(*new_node))
	{
		return CREATE_OK;
	}
	return CREATE_NO;
}

void insert_node_head(Link * head,Link new_node)
{
	new_node->next = *head;//head 是二级指针 取内容 赋值所以加*
	*head = new_node;
}

void display(Link head)
{
	Node *p = NULL;
	p = head;
	while(p != NULL){
		printf("%d\n",p->num);
		p = p->next;
	}
}

void release_link(Link * head)
{
	Link p =NULL;
	p = *head;//
	while(*head != NULL)
	{
		*head = (*head)->next;//优先级
		free(p);
		p = *head;
	}
	printf("link is empty");
}
int main()
{
	Link head = NULL;//创建头指针 指向NULL 防止野指针
	Link new_node = NULL;
	int i;
	create_link(&head);//创建链表 取回头结点的地址所以要用到二级指针但并不是定义二级指针而是取一级指针的地址
	for(i = 0;i<10;i++)
	{
		if( CREATE_OK == create_node(&new_node);//创建结点并判断是否成功
		{	
		new_node->num = i+1;
		insert_node_head(&head,new_node);//head 代表的是插入哪个链表(用头指针来表示)
		}
	}
	dispaly(head);
	release_link(&head);//释放链表 相当于释放地址所以取& 使用二级指针
	
	return 0;
}
		

b)尾插法

typedef struct node Node;
typedef Node * Link;

//创建链表
void create_link(Link * head)
{
	*head = NULL;//空链表 头指针指向空
}

int is_malloc_ok(Link new_node)
{
	if(NULL == new_node)
	{
		printf("malloc error!\n");
		return MALLOC_NO;
	}
	
	return MALLOC_OK;
}
void create_node(Link * new_node)
{
	*new_node = (Link)malloc(sizeof(Node));
	if(MALLOC_OK == is_malloc_ok(*new_node))
	{
		return CREATE_OK;
	}
	return CREATE_NO;
}

void insert_node_tail(Link * head,Link new_node)
{
	Link p = NULL;
	p = *head;
	if(*head == NULL){//链表为空时
		*head = new_node;
		new_node->next = NULL;
	}
	else
	{
		while(p->next ! = NULL)
		{
			p = p->next;
		}	
		p->next = new_node;
		new_node->next = NULL;
	}
}

void release_link(Link * head)
{
	Link p =NULL;
	p = *head;//
	while(*head != NULL)
	{
		*head = (*head)->next;//优先级
		free(p);
		p = *head;
	}
	printf("link is empty");
}
int main()
{
	Link head = NULL;//创建头指针 指向NULL 防止野指针
	Link new_node = NULL;
	int i;
	create_link(&head);//创建链表 取回头结点的地址所以要用到二级指针但并不是定义二级指针而是取一级指针的地址
	for(i = 0;i<10;i++)
	{
		if( CREATE_OK == create_node(&new_node);//创建结点并判断是否成功
		{	
		new_node->num = i+1;
		insert_node_tail(&head,new_node);//尾插法 &head 用二级指针是因为链表为空时 插入第一个结点 头指针改变了 之后不再变
		}
	}
	release_link(&head);//释放链表 相当于释放地址所以取& 使用二级指针
	
	return 0;
}

c)中间插入法(插前面)

与尾插法头插法其余一样 不一样的是加入了一个insert_node_mid函数 使用这个函数进行插入
而insert_node_tail是用尾插法插入了十个结点

typedef struct node Node;
typedef Node * Link;

//创建链表
void create_link(Link * head)
{
	*head = NULL;//空链表 头指针指向空
}

int is_malloc_ok(Link new_node)
{
	if(NULL == new_node)
	{
		printf("malloc error!\n");
		return MALLOC_NO;
	}
	
	return MALLOC_OK;
}
int create_node(Link * new_node)
{
	*new_node = (Link)malloc(sizeof(Node));
	if(MALLOC_OK == is_malloc_ok(*new_node))
	{
		return CREATE_OK;
	}
	return CREATE_NO;
}

void insert_node_tail(Link * head,Link new_node)
{
	Link p = NULL;
	p = *head;
	if(*head == NULL){//链表为空时
		*head = new_node;
		new_node->next = NULL;
	}
	else
	{
		while(p->next ! = NULL)
		{
			p = p->next;
		}	
		p->next = new_node;
		new_node->next = NULL;
	}
}
void display(Link head){
		Node * p;
		p = head;
		while(p != NULL){
			printf("%d\n",p->num);
			p = p->next;
		}
	}

void release_link(Link * head)
{
	Link p =NULL;
	p = *head;
	while(*head != NULL)
	{
		*head = (*head)->next;//优先级
		free(p);
		p = *head;
	}
}
void insert_node_mid_front(Link * head,Link new_node,int loc)
{
	Link p = NULL;
	Link q = NULL;

	p = q = *head;
	while(p != NULL && p->num != loc)//里面的条件顺序不能颠倒 不然会出现段错误 因为 当p= NULL时 p没有num 所以会出现段错误
	{
		q = p;
		p = p->next;
	}

	if(p == *head)
	{
		new_node->next = *head;
		*head = new_node;
	}
	else
	{
		q->next = new_node;
		new_node->next = p;
	}
}
int main()
{
	Link head = NULL;//创建头指针 指向NULL 防止野指针
	Link new_node = NULL;
	int i;
	int loc;
	create_link(&head);
	for(i = 0;i<10;i++)
	{
		if( CREATE_OK == create_node(&new_node))//创建结点并判断是否成功
		{	
		new_node->num = i+1;
		insert_node_tail(&head,new_node);
		}
	}

	printf("old link\n");
	display(head);

	printf("input location\n");
	scanf("%d",&loc);

	if(CREATE_OK == create_node(&new_node))
	{
		printf("please input num:/n");
		scanf("%d",&new_node->num);
		insert_node_mid_front(&head,new_node,loc);
	}
	display(head);
	release_link(&head);//释放链表 相当于释放地址所以取& 使用二级指针
	
	return 0;
}

d)中间插法(往后插)

相比于中间往前插 更简单一点 少考虑了当链表只有一个结点的情况 往后插 不用管跟有很多结点时一样 不用改变头指针 但是往前插 当只有一个结点的时候需要改变头指针

#include<stdio.h>
#include<stdlib.h>
struct node{
	int num;
	char name[20];
	char sex;

	struct node * next;
};
	typedef struct node Node;
	typedef Node * Link;

	void create_link(Link * head){

		*head = NULL;
		
	}
	
	void create_new_node(Link * new_node){

		*new_node = (Link)malloc(sizeof(Node));

	}

	void insert_node_tail(Link * head,Node * new_node){
		Link p = NULL;
		p = *head;
		if(*head == NULL){
			*head = new_node;
			new_node->next = NULL;
		}
		else{
			while(p->next != NULL){
				p = p->next;
			}
			p->next = new_node;
			new_node->next = NULL;
		}
	}

	void insert_node_mid(Link * head,Link new_node,int loc){
		Link p = *head;
		
		if(NULL == *head){
			*head = new_node;
			new_node->next = NULL;
		}
		else{
			while(p->next != NULL && p->num != loc){
				p = p->next;
			}

			new_node->next = p->next;
			p->next = new_node;
		}
	}

	void display(Link head){
		Node * p;
		p = head;
		while(p != NULL){
			printf("%d\n",p->num);
			p = p->next;
		}
	}

	void release(Link * head){
		
		Link p = NULL;
		p = *head;
		while(p!=NULL){
			*head = (*head)->next;
			free(p);
			p = *head;
		}
		printf("link is empty\n");

	}
	

int main(){
	int i;
	int loc,num;
	Link head = NULL;
	Node * new_node = NULL;

	create_link(&head);

	for(i = 1; i < 0;i++){
		create_new_node(&new_node);

		new_node->num = i;

		insert_node_tail(&head,new_node);
	}


	printf("old link!\n");
	display(head);

	printf("input location\n");
	scanf("%d",&loc);

	create_new_node(&new_node);
	printf("input new_node->num\n");
	scanf("%d",&new_node->num);

	insert_node_mid(&head,new_node,loc);

	display(head);

	release(&head);

	return 0;
}

删除

在上面代码的基础上加入 删除函数

void delete_node(Link * head,int num){
	Link p = NULL;
	Link q= NULL;
	p = q = *head;
	if(*head == NULL){
		printf("delete:link is empty!\n");
	}
	else{
		while(p != NULL && p->num != num){
			q = p;
			p = p->next;
		}
		if(p ==NULL){
			printf("don't find need delete node\n");
		}
		else if(p == *head){
			*head = p->next;
			free(p);
		}
		else{
			q->next = p->next;
			free(p);
		}
	}
}

在主函数中加入下面主代码 使其使用删除函数

printf("please input delete num\n");     //delete
	scanf("%d",&num);
	
	delete_node(&head,num);
	printf("delete:link\n");
	display(head);

链表笔试题

将单链表中所以的结点倒叙

#include<stdio.h>
#include<stdlib.h>
struct node{
	int num;
	char name[20];
	char sex;

	struct node * next;
};
	typedef struct node Node;
	typedef Node * Link;

	void create_link(Link * head){

		*head = NULL;
		
	}
	
	void create_new_node(Link * new_node){

		*new_node = (Link)malloc(sizeof(Node));

	}

	void insert_node_tail(Link * head,Node * new_node){
		Link p = NULL;
		p = *head;
		if(*head == NULL){
			*head = new_node;
			new_node->next = NULL;
		}
		else{
			while(p->next != NULL){
				p = p->next;
			}
			p->next = new_node;
			new_node->next = NULL;
		}
	}

	void display(Link head){
		Node * p;
		p = head;
		while(p != NULL){
			printf("%d\n",p->num);
			p = p->next;
		}
	}

	void release(Link * head){
		Link p = NULL;
		p = *head;
		while(p!=NULL){
			*head = (*head)->next;
			free(p);
			p = *head;
		}
		printf("link is empty\n");

	}

void reverse(Link * head){   //使其颠倒的函数
	Link p1 = NULL;
	Link p2 = NULL;
	Link p3 = NULL;

	if(*head == NULL){
		printf("reverse:link is empty!\n");
	}
	else if((*head)->next == NULL){
		printf("reverse:link only node\n");
	}
	else{
		p1 = *head;
		p2 = p1->next;
		p3 = p2->next;

		while(p3 != NULL){
			p2->next = p1;
			p1 = p2;
			p2 = p3;
			p3 = p3->next;
		}
		p2->next = p1;
		(*head)->next = NULL;
		*head = p2; 

	}
}
	

int main(){
	int i;
	Link head = NULL;
	Node * new_node = NULL;

	create_link(&head);

	for(i = 0; i < 10;i++){
		create_new_node(&new_node);

		new_node->num = i;

		insert_node_tail(&head,new_node);
	}
	printf("old link\n");
	display(head);

	reverse(&head);//使其颠倒的函数

	printf("reverse:link\n");
	display(head);

	release(&head);

	return 0;

2、单链表(带头结点)

今天遇到一个问题 为什么在创建链表函数中 创建了头结点之后 不让head指向new_node即头结点 即 head = new_node?

int create_Hnode(Hlink * new_node){
	*new_node = (Hlink)malloc(sizeof(Hnode));
	if(*new_node == NULL){
		printf("malloc is error!\n");
		return FAIL;
	}
	else{
		return SUCCESS;
	}
}
void create_Hlink(Hlink * head){
	create_Hnode(head);
	(*head)->next = NULL;
}

 create_Hnode(&new_node)

要注意的是当带头结点时 一开始在链表中创建头结点 是在create_Hlink(Hlink * head)创建链表的函数中创建结点 最最重要的是 这时候传入的参数是head即create(Hlink head)至于为什么不传二级指针 是因为在创建链表函数中 head已经是二级指针 而主函数中创建结点函数 传入的是create_node(&new_node)所以这时候head头指针是指向这个新创建的结点即头结点 所以不用写head = new_node;

create_Hnode(head);调用时传入的是head
int create_Hnode(Hlink * new_node)函数中传入的new_node 所以这是的调用 head是指向new_node的 所以不用写 head=new_node;

整体代码:
实现功能:1.创建链表 2.创建结点 3.前插 4.后插 5.中间前插 6.中间后插 7.输出链表 8.按值删除结点 9.链表倒叙 10.按值查找结点 11.计算链表长度 12.对链表从小到大排序 13.清空链表 14.释放链表

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define SUCCESS 1
#define FAIL 0

struct hnode{
	int num;

	struct hnode *next;
};

typedef struct hnode Hnode;
typedef Hnode * Hlink;


int create_Hnode(Hlink * new_node){
	*new_node = (Hlink)malloc(sizeof(Hnode));
	if(*new_node == NULL){
		printf("malloc is error!\n");
		return FAIL;
	}
	else{
		return SUCCESS;
	}
}
void create_Hlink(Hlink * head){
	create_Hnode(head);
	(*head)->next = NULL;
}


void insert_Hnode_tail(Hlink head,Hlink new_node){
	Hlink p = NULL;
	if(head == NULL){
		printf("not this link\n");
	}
	else{
		p = head;
	}
	while(p->next != NULL){
		p = p->next;
	}
	p->next = new_node;
	new_node->next = NULL;
}

void insert_Hnode_head(Hlink head,Hlink new_node){
	Hlink p = NULL;
	if(head == NULL){
		printf("insert_Hnode_head:not this link\n");
	}
	else{
		p = head->next;
	}
	new_node->next = p;
	head->next = new_node;
}

void display(Hlink head){
	Hlink p = NULL;
	if(head == NULL){
		printf("display:not this link\n");
	}
	else{
		p = head->next;
	}
	if(p == NULL){
		printf("display:link is empty\n");
	}
	else{
		while(p != NULL){
			printf("%d\n",p->num);
			p = p->next;
		}
	}
}

void empty_link(Hlink head){
	Hlink p = NULL;
	if(head == NULL){
		printf("empty_link:not this link\n");
	}
	else{
		p = head->next;
	}
	if(head->next == NULL){
		printf("empty_link:this link don't need empty\n");
	}
	else{
		while(p != NULL){
			head->next = p->next;
			free(p);
			p = head->next;
		}
	}
}

void release(Hlink * head){
	Hlink p = NULL;
	if(*head == NULL){
		printf("release:not this link\n");
	}
	else{
		empty_link(*head);
		p = *head;
		*head = NULL;
		free(p);
	}
	printf("release:link is release\n");
}

void insert_node_mid_after(Hlink head,Hlink new_node,int num){
	Hlink p = NULL;
	if(head == NULL){
		printf("not this link\n");
	}
	else{
		p = head;
	}
	while(p->next != NULL && p->num != num){
		p = p->next;
	}
	p->next = new_node;
	new_node->next = NULL;
}

void insert_node_mid_front(Hlink head,Hlink new_node,int num){
	Hlink p = NULL;
	Hlink q = NULL;
	if(head == NULL){
		printf("insert_node_mid_front:not this link\n");
	}
	else{
		q = head;
		p = head->next;
	}
	while(p != NULL && p->num != num){
		q = p;
		p = p->next;
	}
	q->next = new_node;
	new_node->next = p; 
}

void delete(Hlink head,int num){
	Hlink p = NULL;
	Hlink q = NULL;
	if(head == NULL){
		printf("delete:not this link\n");
	}
	else{
		q = head;
		p = head->next;
	}
	if(head->next == NULL){
		printf("delete:link is empty\n");
	}
	else{
		while(p != NULL && p->num != num){
			q = p;
			p = p->next;
		}
		q->next = p->next;
		free(p);
	}
}

void reverse(Hlink head){
	Hlink p1,p2,p3;
	p1 = NULL;
	p2 = NULL;
	p3 = NULL;
	if(head == NULL){
		printf("reverse:not this link\n");
	}
	else{
		if(head->next == NULL){
			printf("reverse:link is empty\n");
		}
		else if(head->next->next == NULL){
			printf("reverse:link only one node\n");
		}
		else{
			p1 = head->next;
			p2 = p1->next;
			p3 = p2->next;
		}
		while(p3 != NULL){
			p2->next = p1;
			p1 = p2;
			p2 = p3;
			p3 = p3->next;
		}
		p2->next = p1;
		head->next->next = NULL;
		head->next = p2;
	}
}

Hlink find_node(Hlink head,int num){
	Hlink p = NULL;
	if(head == NULL){
		printf("find_node:not this link\n");
		return NULL;
	}
	else{
		if(head->next == NULL){
			printf("find_node:link is empty\n");
			return NULL;
		}
		else{
			p = head->next;
			while(p != NULL && p->num != num){
				p = p->next;
			}
			if(p == NULL){
				return NULL;
			}
			else{
				return p;
			}
		}
	}
}

int length_link(Hlink head){
	Hlink p = NULL;
	int count = 0;
	if(head == NULL){
		printf("length_link:not this link\n");
		return 0;
	}
	else if(head->next == NULL){
		printf("length_link:link is empty\n");
		return 0;
	}
	else{
		p = head->next;
		while(p != NULL){
			p = p->next;
			count++;
		}
		return count;
	}
}

void rank_link(Hlink head){ //使用冒泡排序对链表进行排序(从小到大)
	int i,j;
	Hlink p1 = NULL;
	Hlink p2 = NULL;
	Hlink p3 = NULL;

	for(i = 10;i > 0;i--){
			p1 = head;
			p2 = head->next;
			p3 = p2->next;
		for(j = 0;j < i-1;j++){	
			if(p2->num > p3->num){
				p2->next = p3->next;
				p3->next = p2;
				p1->next = p3;
				
				p1 = p3;  //这两行 当要交换时 这时候p1 p2 p3的next要注意 
				p3 = p2->next;

			}
			else{
				p1 = p2;
				p2 = p3;
				p3 = p3->next;
			}
		}
	}
}
int main(){
	Hlink head = NULL;
	Hlink new_node = NULL;
	int i,num,num1;
	Hlink find_num;
	int count;
	create_Hlink(&head);

	srand((unsigned)time(NULL));

	for(i=0;i<10;i++){
		if(FAIL == create_Hnode(&new_node)){
			printf("create node error!\n");
		}
		else{
			new_node->num = rand()%100;

			insert_Hnode_tail(head,new_node);
			
			//insert_Hnode_head(head,new_node);
		}	
	}
	
	printf("insert_node_tail:link\n");
	display(head);

	printf("please input want to insert location num\n");
	scanf("%d",&num);

	create_Hnode(&new_node);
	printf("please input you want to insert's num1\n");
	scanf("%d",&num1);
	new_node->num = num1;

	//insert_node_mid_after(head,new_node,num);
	insert_node_mid_front(head,new_node,num);

	//printf("insert_node_mid_after:link\n");
	printf("insert_node_mid_after:link\n");
	display(head);

	printf("please input want to delete num\n");
	scanf("%d",&num);

	delete(head,num);
	printf("delete:link\n");
	display(head);

	printf("please input want to find node->num\n");
	scanf("%d",&num);

	find_node(head,num);
	find_num = find_node(head,num);
	if(find_num == NULL){
		printf("find_node:not find this node\n");
	}
	else{
		printf("already find node %d\n",find_num->num);
	}

	count = length_link(head);
	printf("link length is %d\n",count);
	
	rank_link(head);
	printf("rank_link:link\n");
	display(head);
	
	reverse(head);
	printf("reverse:link\n");
	display(head);
	
	empty_link(head);
	printf("empty_link:link\n");
	display(head);

	release(&head);
	
}



静态链表

循环链表

带头结点的循环链表 与带头结点的单链表 主要区别就是 当到尾结点时,不再是指向NULL 而是指向head
具体代码实现:
功能有:1.创建循环链表 2.创建结点 3.后插 4.中间后插 5.输出链表 6.按值删除结点 7.链表倒叙 8.按值查找结点 9.计算链表长度 10.对链表从小到大排序 11.清空链表 12.释放链表

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define SUCCESS 1
#define FAIL 0

struct hnode{
	int num;

	struct hnode * next;
};

typedef struct hnode Hnode;
typedef Hnode * Hlink;

int create_node(Hlink * new_node){
	*new_node = (Hlink)malloc(sizeof(Hnode));
	if(new_node == NULL){
		printf("malloc is error!\n");
		return FAIL;
	}
	else{
		return SUCCESS;
	}


}
void create_link(Hlink * head){
	create_node(head);

	(*head)->next = *head;

}

void insert_node_tail(Hlink head,Hlink new_node){
	Hlink p = NULL;
	p = head;
	while(p->next != head){
		p = p->next;
	}
	p->next = new_node;
	new_node->next = head;
}

void display(Hlink head){
	Hlink p = NULL;
	if(head->next == head){
		printf("display:link is empty\n");
	}
	else{
		p = head->next;
		while(p != head){
			printf("%d\n",p->num);
			p = p->next;
		}
	}
}

void empty_link(Hlink head){
	Hlink p = NULL;
	if(head->next == head){
		printf("empty:link not need empty\n");
	}
	else{
		p = head->next;
		while(p != head){
			head->next = p->next;
			free(p);
			p = head->next;
		}
		printf("empty:link already empty!\n");
	}
}

void release(Hlink * head){

	empty_link(*head);
	
	(*head)->next = NULL;
	free(*head);
	*head = NULL;
	printf("release:link already release\n");
}

void insert_node_mid_after(Hlink head,Hlink new_node,int num){
	Hlink p = NULL;
	p = head->next;
	while(p->next != head && p->num != num){
		p = p->next;
	}
//	if(p->next == head){
//		p->next = new_node;
//		new_node->next = head;
//	}
//	else{
		new_node->next = p->next;
		p->next = new_node;
//	}
	
}

void delete(Hlink head,int num){
	Hlink p = NULL;
	Hlink q = NULL;
	q = head;
	p = head->next;
	while(p != head && p->num != num){
		q = p;
		p = p->next;
	}
	if(p == head){
		printf("delete:not find this node\n");
	}
	else{
		q->next = p->next;
		free(p);
	}

}

Hlink find_node(Hlink head,int num){
	Hlink p = NULL;
	p = head->next;
	while(p != head && p->num != num){
		p = p->next;
	}
	if(p == head){
		return NULL;
	}
	else{
		return p;
	}

}

void reverse(Hlink head){
	Hlink p1 = NULL;
	Hlink p2 = NULL;
	Hlink p3 = NULL;
	if(head->next == head){
		printf("reverse:link is empty\n");
	}
	else if(head->next->next == head){
		printf("reverse:link only one node\n");
	}
	else{
		p1 = head->next;
		p2 = p1->next;
		p3 = p2->next;

		while(p3 != head){
			p2->next = p1;
			p1 = p2;
			p2 = p3;
			p3 = p3->next;
		}
		p2->next = p1;
		head->next->next = head;
		head->next = p2;
	}
}
int length_link(Hlink head){
	int count = 0;
	Hlink p = NULL;
	p = head->next;
	while( p != head){
		p = p->next;
		count++;
	}
	return count;
}

void rank_link(Hlink head){
	Hlink p1 = NULL;
	Hlink p2 = NULL;
	Hlink p3 = NULL;
	int i , j;
	for(i = 10;i > 0;i--){
		p1 = head;
		p2 = p1->next;
		p3 = p2->next;
		for(j = 0;j<i-1;j++){
			if(p2->num > p3->num){
				p2->next = p3->next;
				p1->next = p3;
				p3->next = p2;
				
				p1 =p3;
				p3 = p2->next;
			}
			else{
				p1 = p2;
				p2 = p3;
				p3 = p3->next;
			}
		}
	}
}
int main(){
	Hlink head = NULL;
	Hlink rail = NULL;
	Hlink new_node = NULL;
	int i,num,num1;

	create_link(&head);
	srand((unsigned)time(NULL));
	for(i = 0;i < 10;i++){

		if(FAIL == create_node(&new_node)){
			printf("create node fail\n");
		}
		else{
			new_node->num = rand()%100;
		}

		insert_node_tail(head,new_node);
	}
	printf("insert_node_tail:link is:\n");
	display(head);

	printf("please want to insert num location\n");
	scanf("%d",&num);

	printf("please you want to insert num1\n");
	scanf("%d",&num1);

	if(create_node(&new_node) == FAIL){
		printf("create node is fail\n");
	}
	else{
		new_node->num = num1;
	
		insert_node_mid_after(head,new_node,num);
	}
	printf("insert_node_mid_after:link is:\n");
	display(head);

	printf("please input want to delete node num\n");
	scanf("%d",&num);

	delete(head,num);
	printf("delete:link is:\n");
	display(head);
	
	printf("please input want to find num\n");
	scanf("%d",&num);

	Hlink findnum = find_node(head,num);
	if(findnum == NULL){
		printf("find_node:not find this node\n");
	}
	else{
		printf("find node num is:%d\n",findnum->num);
	}

	reverse(head);
	printf("reverse:link is:\n");
	display(head);

	int count = length_link(head);
	printf("link length is:%d\n",count);

	rank_link(head);
	printf("rank_link:link is:\n");
	display(head);

	empty_link(head);
	
	release(&head);

}

双向循环链表

所以代码:
功能有:1.创建循环链表 2.创建结点 3.后插 4.中间后插 5.输出链表 6.按值删除结点 7.按值查找结点 8.计算链表长度 9.对链表从小到大排序 10.清空链表 11.释放链表
在双向循环链表建立 排序链表函数时 相比于单链表可以少使用一个指针

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

#define SUCCESS 1
#define FAIL 0

struct two_cycle_node{
	int num;

	struct two_cycle_node *front;
	struct two_cycle_node *next;
};
typedef struct two_cycle_node TCnode;
typedef TCnode * Thlink;

int create_node(Thlink * new_node){
	*new_node = (Thlink)malloc(sizeof(TCnode));
	if(*new_node == NULL){
		return FAIL;
	}
	else{
		return SUCCESS;
	}
}

void create_link(Thlink * head){
	create_node(head);

	(*head)->next = *head;
	(*head)->front = *head;
}

void insert_node_tail(Thlink * head,Thlink * new_node){
	Thlink p = NULL;
	p = (*head)->next;
	while(p->next != *head){
		p = p->next;
	}
	 p->next = *new_node;
	 (*new_node)->front = p;
	 (*new_node)->next = (*head);
	 (*head)->front = *new_node;
}

void display(Thlink head){
	Thlink p = NULL;
	p = head->next;
	if(head->next == head){
		printf("display:link is empty\n");
	}
	else{
		while(p != head){
			printf("%d\n",p->num);
			p = p->next;
		}
	}
}

void insert_node_mid_after(Thlink head,Thlink new_node,int num){
	Thlink p = NULL;
	p = head->next;
	while(p != head && p->num != num){
		p = p->next;
	}
	p->next->front = new_node;
	new_node->next = p->next;
	new_node->front = p;
	p->next = new_node;
}

void empty_link(Thlink head){
	Thlink p = NULL;
	p = head->next;
	if(head->next == head){
		printf("empty_link:link is empty\n");
	}
	else{
		while(p != head){
			head->next = p->next;
			p->next->front = p->front;
			free(p);
			p = head->next;
		}
	}
}

void release(Thlink * head){
	empty_link(*head);

	//(*head)->next = NULL;
	//(*head)->front = NULL;
	free(*head);
	*head = NULL;
	printf("release:link already release\n");
}

void delete(Thlink head,int num){
	Thlink p = NULL;
	p = head->next;
	if(head->next == head){
		printf("delete:link is empty\n");
	}
	else{
		while(p != head && p->num != num){
			p = p->next;
		}
		if(p == head){
			printf("delete:not find node\n");
		}
		else{
			p->front->next = p->next;
			p ->next->front = p->front;
			free(p);
		}
	}
}

int length_link(Thlink head){
	Thlink p = NULL;
	int count = 0;
	p = head->next;

	while(p != head){
		p = p->next;
		count++;
	}
	return count;
}

Thlink find_node(Thlink head,int num){
	Thlink p = NULL;
	p = head->next;
	if(head->next == head){
		printf("find_node:link is empty\n");
	}
	else{
		while(p != head && p->num != num){
			p = p->next;
		}
		if(p == head){
			return NULL;
		}
		else{
			return p;
		}
	}
}

void rank_link(Thlink head){
	Thlink p1,p2,p3;
	p1 = NULL;
	p2 = NULL;
	p3 = NULL;
	int i,j;
	for(i = 10;i > 0;i--){
		p1 = head->next;
		p2 = p1->next;
		for(j = 0;j < i-1;j++){
			if(p1->num > p2->num){
				p1->next = p2->next;
				p2->front = p1->front;
				p1->front->next = p2;
				//p1->front->front = p1;
				p2->next->front = p1;
				p1->front = p2;
				p2->next = p1;

				p2 = p1->next;
			}
			else{
				p1 = p2;
				p2 = p2->next;
			}
		}
	}
}
int main(){
	Thlink head = NULL;
	Thlink new_node = NULL;
	int i,num,num1;

	create_link(&head);
	
	srand((unsigned)time(NULL));

	for(i = 0;i < 10;i++){
		if(FAIL == create_node(&new_node)){
			printf("create node error!\n");
		}
		else{
			new_node->num = rand()%100;

			insert_node_tail(&head,&new_node);
		}
	}
	printf("insert_node_tail:link is:\n");
	display(head);

	printf("please input want to insert num location\n");
	scanf("%d",&num);

	printf("please input you want to insert num1\n");
	scanf("%d",&num1);
	
	if(create_node(&new_node) == FAIL){
		printf("create node error\n");
	}
	else{
		new_node->num = num1;

		insert_node_mid_after(head,new_node,num);
	}

	printf("insert_node_mid_after:link is\n");
	display(head);

	printf("please input want delete node num\n");
	scanf("%d",&num);

	delete(head,num);
	printf("delete:link is:\n");
	display(head);

	printf("please input want to find node num\n");
	scanf("%d",&num);

	Thlink findnode = find_node(head,num);
	if(findnode == NULL){
		printf("find_node:not find node\n");
	}
	else{
		printf("you want to find node num already find is:%d\n",findnode->num);
	}

	int count = length_link(head);
	printf("the link length is:%d\n",count);
	
	rank_link(head);
	printf("rank_link:link is:\n");
	display(head);

	empty_link(head);
	printf("empty_link:link already empty\n");
	
	release(&head);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex、WY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值