赞
踩
- loop_p creat_head(){
- loop_p L=(loop_p)malloc(sizeof(loop_list));
- if(L==NULL){
- printf("空间申请失败\n");
- return NULL;
- }
- L->len=0;
- L->next=L;
- return L;
- }
- //创建节点
- loop_p creat_node(int data){
- //在堆区申请一个节点的空间
- loop_p new=(loop_p)malloc(sizeof(loop_list));
- if(new==NULL){
- printf("空间申请失败\n");
- return NULL;
- }
- new->data=data;
- return new;//返回申请的节点的首地址
- }
- //判空
- int loop_empty(loop_p H){
- if(H==NULL){
- printf("头节点为空\n");
- return -1;
- }
- //如果头节点指向头节点本身说明链表空
- return H->next==H?1:0;
- }
- //头插
- void insert_head(loop_p H,int data){
- if(H==NULL){
- printf("头节点为空\n");
- return;
- }
- loop_p new=creat_node(data);
- new->next=H->next;
- H->next=new;
- H->len++;
- }
- //尾插
- void insert_tail(loop_p H,int data){
- if(H==NULL){
- printf("头节点为空\n");
- return;
- }
- loop_p new=creat_node(data);
- loop_p p=H;
- while(p->next!=H){
- p=p->next;
- }
- new->next=H;
- p->next=new;
- H->len++;
- }
- //按位置插入,头节点是第0个节点
- void insert_pos(loop_p H,int pos,int data){
- if(H==NULL){
- printf("头节点为空\n");
- return;
- }
- loop_p p=H;
- //判断位置合理性
- if(pos<=0 || pos>H->len+1){
- printf("位置不合理\n");
- return;
- }
- for(int i=0;i<pos-1;i++){
- p=p->next;
- }
- loop_p new=creat_node(data);
- new->next=p->next;
- p->next=new;
- new->data=data;
- H->len++;
- }
- //尾删
- void del_tail(loop_p H){
- if(H==NULL){
- printf("头节点为空\n");
- return;
- }
- if(loop_empty(H)){
- printf("链表为空\n");
- return;
- }
- loop_p p=H;
- while(p->next->next!=H)
- p=p->next;
- loop_p del=p->next;
- p->next=p->next->next;
- free(del);
- H->len--;
- }
- //头删
- void del_head(loop_p H){
- if(H==NULL){
- printf("头节点为空\n");
- return;
- }
- if(loop_empty(H)){
- printf("链表为空\n");
- return;
- }
- loop_p p=H;
- loop_p del=p->next;
- p->next=p->next->next;
- free(del);
- H->len--;
- }
- //按位置删除
- void del_pos(loop_p H,int pos){
- if(H==NULL){
- printf("头节点为空\n");
- return;
- }
- if(loop_empty(H)){
- printf("链表为空\n");
- return;
- }
- //判断位置合理性
- if(pos<=0 || pos>H->len+1){
- printf("位置不合理\n");
- return;
- }
- loop_p p=H;
- for(int i=1;i<pos-1;i++){
- p=p->next;
- }
- loop_p del=p->next;
- p->next=p->next->next;
- free(del);
- H->len--;
- }
- //打印列表
- void out_put(loop_p H){
- if(H==NULL){
- printf("头节点为空\n");
- return;
- }
- loop_p p=H->next;
- while(p!=H){
- printf("%d ",p->data);
- p=p->next;
- }
- putchar(10);
- }
- int search_value(loop_p H,int data){
- if(H==NULL){
- printf("头节点为空\n");
- return -1;
- }
- loop_p p=H->next;
- int i=1;
- while(p->next!=H){
- if(p->data==data){
- return i;
- }
- p=p->next;
- i++;
- }
- printf("查找失败\n");
-
- }
- int search_pos(loop_p H,int pos){
- if(H==NULL){
- printf("头节点为空\n");
- return -1;
-
- }
- //判断位置合理性
- if(pos<=0 || pos>H->len){
- printf("位置不合理\n");
- return -1;
- }
- loop_p p=H->next;
- for(int i=1;i<pos;i++){
- p=p->next;
- }
- return p->data;
-
- }
- void updata_value(loop_p H,int data,int new_data){
- if(H==NULL){
- printf("头节点为空\n");
- return;
- }
- loop_p p=H->next;
- while(p!=H){
- if(p->data==data)
- p->data=new_data;
- p=p->next;
- }
- printf("查找不到该元素\n");
- }
- //循环链表逆置
- void overturn_loop(loop_p H){
- if(H==NULL){
- printf("头节点为空\n");
- return;
- }
- if(loop_empty(H)){
- printf("链表为空\n");
- return;
- }
- if(H->next->next==H){
- printf("只有一个元素,不用逆置\n");
- return;
- }
- loop_p p=H->next->next;
- H->next->next=H;
- loop_p q=p->next;
- while(p!=H){
- p->next=H->next;
- H->next=p;
- p=q;
- if(q!=H)
- q=q->next;
-
- }
-
-
- }
- #ifndef _LOOP_LIST_H__
- #define _LOOP_LIST_H__
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct loop_list{
- union{
- int len;
- int data;
- };
- struct loop_list *next;
- }loop_list,*loop_p;
-
- //创建单向循环链表
- loop_p creat_head();
- //创建节点
- loop_p creat_node(int data);
- //判空
- int loop_empty(loop_p H);
- //头插
- void insert_head(loop_p H,int data);
- //打印列表
- void out_put(loop_p H);
- //尾插
- void insert_tail(loop_p H,int data);
- //按位置插入,头节点是第0个节点
- void insert_pos(loop_p H,int pos,int data);
- //头删
- void del_tail(loop_p H);
- //尾删
- void del_head(loop_p H);
- //按位置删除
- void del_pos(loop_p H,int data);
- //按值查找,返回在链表中的位置
- int search_value(loop_p H,int data);
- //按下标查找,返回数值
- int search_pos(loop_p H,int pos);
- //按值修改元素
- void updata_value(loop_p H,int data,int new_data);
- //循环链表逆置
- void overturn_loop(loop_p H);
-
- #endif
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。