C語言雙向鏈表的基本操作實現


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

int len;

//定義雙向鏈表的節點 
typedef struct Node
{
    int data;
    struct Node *prior;
    struct Node *next;
}Node;

//初始化一個鏈表的節點、
Node* create_node(void)
{
    Node *p;
    p = (Node*)malloc(sizeof(Node));
    if(p == NULL)
    {
        printf("動態內存分配失敗!\n");
        exit(0);
    }
    scanf("%d",&(p->data));
    p->prior = NULL;
    p->next = NULL;
    return (p);
} 

//建立含有N個結點的雙向鏈表
Node* create_list(int n)
{
    Node *p,*new1,*head;
    int i;
    if(n >= 1)                    //結點的個數 >= 1 的時候,先生成第一個結點 
    {
        new1 = create_node();
        head = new1;
        p = new1; 
    }
    for(i = 2;i <= n;i++)    //生成第一個結點以后的結點,並建立雙向鏈表的關系 
    {
        new1 = create_node();
        p->next = new1;
        new1->prior = p;
        p = new1;
    }
    len = n;
    if(n >= 1)
        return (head);
    else
        return 0;    
} 



//鏈表的長度
int len_list(int len)
{
    return len;    
}
 
//定位到鏈表的任意位置
Node* pos_list(Node *head,int n)
{
    int i = 1;
    Node *p;
    if(i <= n)
    {
        p = head;
        for(i = 2;i <= n;i++)
            p = p->next;
    }
    return p;
} 

//正向遍歷一個鏈表
void out_front_list(Node *head)
{
    if(head == NULL)
    {
        printf("輸入的鏈表信息有誤,鏈表不存在!\n");
    }
    else
    {
        Node *p;
        p = head;
        while(p != NULL)
        {
            printf("%d  ",p->data);
            p = p->next;
        }
    }
} 
 
//反向遍歷一個鏈表
void out_reverse_list(Node *head)
{
    if(head == NULL)
    {
        printf("輸入的鏈表信息有誤,鏈表不存在!\n");
    }
    else
    {
        int n;
        n = len_list(len);
        Node *p;
        p = pos_list(head,n);
        while(p != NULL)
        {
            printf("%d  ",p->data);
            p = p->prior;
        }
    }
} 

//在鏈表的頭部插入結點
Node* start_insert_list(Node *head)
{
    Node *p;
    p = create_node();
    p->next = head;
    head->prior = p;
    head = p;
    len++; 
    return (p);
} 
 
//在鏈表的尾部插入結點
Node* end_insert_list(Node *head)
{
    int n;
    n = len_list(len);
    Node *p,*new1;
    new1 = create_node();
    p = pos_list(head,n);
    p->next = new1;
    new1->prior = p;
    len++;
    return (head);
} 

//插入到任意位置之前
Node* insert_befor_list(Node *head)
{
    int a,newlen;
    Node *pos,*p;
    printf("請輸入要插入結點的位置:");
    scanf("%d",&a);
    printf("請輸入要插入的結點的值:"); 
    newlen = len_list(len);
    if(a > newlen)
    {
        head = end_insert_list(head);
    }
    else
    {
        if(a <= 1)
        {
            head = start_insert_list(head);
        }
        else
        {
            pos = pos_list(head,a);
            p = create_node();
            pos->prior->next = p;
            p->prior = pos->prior;
            p->next = pos;
            pos->prior = p; 
        }
    }
    len++;
    return (head);    
}

//插入到任意位置之后 
Node* insert_after_list(Node *head)
{
    int a,newlen;
    Node *pos,*p;
    printf("請輸入要插入結點的位置:");
    scanf("%d",&a);
    printf("請輸入要插入的結點的值:"); 
    newlen = len_list(len);
    if(a >= newlen)
    {
        head = end_insert_list(head);
    }
    else
    {
        if(a < 1)
        {
            head = start_insert_list(head);
        }
        else
        {
            pos = pos_list(head,a);
            p = create_node();
            p->next = pos->next;
            pos->next->prior = p; 
            pos->next = p;
            p->prior = pos;
        }
    }
    len++;
    return (head);    
}
//刪除頭結點
Node* delect_start_list(Node *head)
{
    Node *pos;
    pos = head;
    head = head->next;
    head->prior = NULL;
    free(pos);
    len--; 
    return(head);
} 

//刪除尾結點
Node* delect_end_list(Node *head)
{
    Node *p,*pos;
    int newlen;
    newlen = len_list(len);
    pos = pos_list(head,newlen);
    p = pos;
    p = p->prior;
    p->next = NULL;
    free(pos);
    len--;
    return (head); 
} 

//刪除指定位置的節點
Node* delect_list(Node *head)
{
    int newlen,i;
    Node *pos;
    newlen = len_list(len);
    printf("請輸入眼刪除結點的位置:\n");
    scanf("%d",&i);
    if(i <= 1)
        head = delect_start_list(head);
    else if(i >=newlen)
        head = delect_end_list(head);
    else
    {
        pos =pos_list(head,i);
        pos->prior->next = pos->next;
        pos->next->prior = pos->prior;
        free(pos); 
    }
    len--; 
    return(head);
} 

int main()
{
//函數的聲明 
    Node* create_node(void);               //定義雙向鏈表的節點 
    Node* create_list(int n);              //建立含有N個結點的雙向鏈表
    int len_list(int len);                 //鏈表的長度 
    Node* pos_list(Node *head,int n);      //定位到鏈表的任意位置 
    Node* tail_list(Node *head);           //將指針定位在鏈表的尾部
    void out_front_list(Node *head);       //正向遍歷一個鏈表
    void out_reverse_list(Node *head);     //反向遍歷一個鏈表
    Node* start_insert_list(Node *head);   //在鏈表的頭部插入結點
    Node* end_insert_list(Node *head);     //在鏈表的尾部插入結點
    Node* insert_befor_list(Node *head);   //插入到任意位置之前
    Node* insert_after_list(Node *head);   //插入到任意位置之后 
    Node* delect_start_list(Node *head);   //刪除頭結點
    Node* delect_end_list(Node *head);     //刪除尾結點
    Node* delect_list(Node *head);         //刪除指定位置的節點

    //int newlen;
    Node *head;
    
    printf("請輸入要建立雙向鏈表的長度:\n");
    scanf("%d",&len);
    printf("請為雙向鏈表賦值:\n");
    head = create_list(len);
    printf("鏈表的長度為:%d\n",len = len_list(len));
    printf("正向遍歷雙向鏈表:\n");
    out_front_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    printf("\n反向遍歷雙向鏈表:\n");
    out_reverse_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    
    printf("\n請輸入在鏈表頭部插入結點的值:\n");
    head = start_insert_list(head);
    printf("鏈表的長度為:%d",len = len_list(len));
    printf("\n正向遍歷雙向鏈表:\n");
    out_front_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    printf("\n反向遍歷雙向鏈表:\n");
    out_reverse_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    
    printf("\n請輸入在鏈表尾部插入結點的值:\n");
    head = end_insert_list(head);
    printf("鏈表的長度為:%d",len = len_list(len));
    printf("\n正向遍歷雙向鏈表:\n");
    out_front_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    printf("\n反向遍歷雙向鏈表:\n");
    out_reverse_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    
    printf("\n插入到任意位置之前:\n");
    head = insert_befor_list(head);
    printf("鏈表的長度為:%d",len = len_list(len));
    printf("\n正向遍歷雙向鏈表:\n");
    out_front_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    printf("\n反向遍歷雙向鏈表:\n");
    out_reverse_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    
    printf("\n插入到任意位置之后:\n");
    head = insert_after_list(head);
    printf("鏈表的長度為:%d",len = len_list(len));
    printf("\n正向遍歷雙向鏈表:\n");
    out_front_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    printf("\n反向遍歷雙向鏈表:\n");
    out_reverse_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    
    printf("\n刪除頭結點:\n");
    head = delect_start_list(head);
    printf("鏈表的長度為:%d",len = len_list(len));
    printf("\n正向遍歷雙向鏈表:\n");
    out_front_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    printf("\n反向遍歷雙向鏈表:\n");
    out_reverse_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    
    printf("\n刪除尾結點:\n");
    head = delect_end_list(head);
    printf("鏈表的長度為:%d",len = len_list(len));
    printf("\n正向遍歷雙向鏈表:\n");
    out_front_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    printf("\n反向遍歷雙向鏈表:\n");
    out_reverse_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    
    printf("\n刪除指定位置的結點:\n");
    head = delect_list(head);
    printf("鏈表的長度為:%d",len = len_list(len));
    printf("\n正向遍歷雙向鏈表:\n");
    out_front_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    printf("\n反向遍歷雙向鏈表:\n");
    out_reverse_list(head);
    printf("\n鏈表的長度為:%d",len = len_list(len));
    
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM