1222双向循环链表1.0

10 篇文章 1 订阅
6 篇文章 0 订阅

这个链表是双向循环链表,程序实现功能:创建,添加(头,尾,中间),释放,显示。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define N 10
#define Mal_OK 1
#define Mal_ERR 0

struct Dou_node
{
    int number;
    int element;
    struct Dou_node *piror;
    struct Dou_node *next;
};

typedef struct Dou_node DouNode;
typedef struct Dou_node* Link;

int is_malloc(Link new_node)
{
    if(NULL == new_node)
    {
        printf("malloc err _1\n");
        return Mal_ERR;
    }
    else
    {
        return Mal_OK;
    }
}

void creat_link(Link *head)
{
    do
    {
        *head = (Link)malloc(sizeof (DouNode));
    }while(Mal_ERR == is_malloc(*head));
    (*head) -> piror = (*head) -> next = (*head); 
}

void creat_node(Link *new_node , int num , int ele)
{
    do
    {
        *new_node = (Link)malloc(sizeof (DouNode));
    }while(Mal_ERR == is_malloc(*new_node));
    (*new_node) -> number = num;
    (*new_node) -> element = ele;
}

void insert_node_tail(Link head , Link new_node)
{
    Link p = head -> piror;   
    if(head == head -> next && head == head -> piror)
    {
        printf("The link is empty_2\n"); 
    }
    p -> next = new_node;
    head -> piror = new_node;
    new_node -> next = head;
    new_node -> piror = p;
}

void insert_node_head(Link head , Link new_node)
{
    Link p = head -> next;   
    if(head == head -> next && head == head -> piror)
    {
        printf("The link is empty_3\n");
    }
    head -> next = new_node;
    p -> piror = new_node;
    new_node -> next = p;
    new_node -> piror = head;
}

void display_link(Link head)
{
    Link p = head -> next;
    if(head == p)
    {
        printf("The link is empty\n");
    }
    while(head != p)
    {
        printf("NO. %d\n" , p -> number);
        printf("The element is %d\n" , p -> element);
        p = p -> next;
    }
    printf("Display_over\n");
    return ; 
}

void del_node(Link head)
{
    int loc ;
    int ele ;
    int choice;
    int choice_rw;
    Link p = head -> next;
    if(head == p)
    {
        printf("The link is empty");
        return ; 
    }
    printf("*******************\n\n");
    printf("   1: del by No.   \n");
    printf(" 2: del by element \n");
    printf("     3: Exit        \n\n");
    printf("*********************\n");
loop:   choice = 0;   
    scanf("%d" , &choice);
    if(1 == choice)
    {
loop_1:        printf("Plz input your No. \n");
        scanf("%d" , &loc);
        while(loc != p -> number)
        {
            p = p -> next;
        }
        if(head == p -> next)
        {
            printf("Not found _4");
loop_11:            printf("If you want to input again , plz inputr 1;\n");
            printf("If you dont want to input again , plz inputr 2;\n");
            if(1 == choice_rw)
            {
                choice_rw = 0;
                goto loop_1;
            }
            else if(2 == choice_rw)
            {
                printf("Exit\n");
                return ;
            }
            else
            {
                choice_rw = 0;
                printf("Wrong input_5");
                goto loop_11;
            }
        }
        (p -> piror) -> next = p -> next;
        (p -> next) -> piror = p -> piror;
        free(p);
        printf("Delele success_6\n");
        return ;
    }

    else if(2 == choice)
    {
loop_0:        printf("Plz input your element: \n");
        scanf("%d" , &ele); 
        while(ele != p -> element)
        {
            p = p -> next;
        }
        if(head == p -> next)
        {
            printf("Not found _4");
loop_01:            printf("If you want to input again , plz inputr 1;\n");
            printf("If you dont want to input again , plz inputr 2;\n");
            if(1 == choice_rw)
            {
                choice_rw = 0;
                goto loop_0;
            }
            else if(2 == choice_rw)
            {
                printf("Exit\n");
                return ;
            }
            else
            {
                choice_rw = 0;
                printf("Wrong input_5");
                goto loop_01;
            }
        }
        (p -> piror) -> next = p -> next;
        (p -> next) -> piror = p -> piror;
        free(p);
        printf("Delele success_6\n");
        return ;
    }
    else if(3 == choice)
    {
        printf("Exit_5\n");
        return;
    }
    else 
    {
        choice = 0;
        printf("Wrong choice ,plz input again_5\n\n");
        goto loop;
    }

}

void make_empty(Link head)
{
   // printf("*****make_empty_head\n");
    Link k = NULL;
    k = head -> next ;
    printf("*****make_empty\n");
    do
    {
       // printf("*****make_while_head\n");
        head -> next = k -> next;
        (k -> next) -> piror = head;
        free(k);
        k = k -> next;
       //printf("****while\n");
    }while(head -> next != head);
    printf("Relese ok\n");
    return ;
}

void insert_node(Link head)
{
    Link new_node = NULL;
    int num;
    int ele;
    int choice;
    Link p = head -> next;
    printf("Plz input where you want to insert: \n");
    scanf("%d", &num);
    printf("Plz input what you want to insert: \n");
    scanf("%d", &ele);
loop_20:    printf("Plz input which kind you want to insert(1:front 2:behind 3:exit): \n");
    scanf("%d", &choice);
    printf("\n");
    creat_node(&new_node , num , ele);
    while(num != p -> number)
    {
        p = p -> next;
    }
    if(p -> next == head)
    {
        printf("Not found\n");
        return;
    }
    if(1 == choice)
    {
        new_node -> next = p;
        new_node -> piror = p -> piror;
        (p -> piror) -> next = new_node;
        p -> piror = new_node;
        printf("Insert complete_1\n");
    }
    else if(2 == choice)
    {
        new_node -> piror = p;
        new_node -> next = p -> next;
        (p -> next) -> piror = new_node;
        p -> next = new_node;
        printf("Insert complete_2;\n");
    }
    else if(3 == choice)
    {
        printf("Exit_3\n");
        return;
    }
    else
    {
        choice = 0;
        printf("Input error\n");
        goto loop_20;
    }
    return;

}

int main()
{
    srand(time(NULL));
    Link head = NULL;
    Link new_node = NULL;
    int i ;
    int j ;
    int num_loc;
    int num_ele;

    creat_link(&head);
    display_link(head);
    for(i = 0 ; i < N ; i++)
    {
        creat_node(&new_node , i , rand()%100);
        insert_node_tail(head , new_node);
    }
    display_link(head);
    del_node(head);
   // printf("*****main_0\n");
    display_link(head);
   // printf("******main\n");
    insert_node(head);
    display_link(head);
    make_empty(head);
    display_link(head);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值