双链表

作者: 萌面大叔2 | 来源:发表于2017-02-15 13:08 被阅读0次
#include<stdio.h>

typedef struct LINK
{
    int num;
    struct LINK *pre;
    struct LINK *next;
}LINK,*pLINK;

int getNum()
{
    int num;
    printf("请输入数字:");
    scanf("%d",&num);
    return num;
}

pLINK createDoubleList(pLINK head)
{
    if(head==NULL)
    {
        head=(pLINK)malloc(sizeof(LINK));
        head->pre=NULL;
        head->next=NULL;
    }
    printf("双链表创建成功!\n");
    return head;
}
 
void headInsertData(pLINK head)
{
    if(head==NULL)
    {
        printf("没有创建双链表\n");
        return ;
    }
    if(head->next==NULL)
    {
        pLINK p=(pLINK)malloc(sizeof(LINK));
        p->num=getNum();
        p->next=NULL;
        p->pre=head;
        head->next=p;
        return ;
    }
    pLINK p=(pLINK)malloc(sizeof(LINK));
    p->num=getNum();
    p->next=head->next;
    p->pre=head;
    head->next=p;
    p->next->pre=p;
}

void tailInsertData(pLINK head)
{
    if(head==NULL)
    {
        printf("没有创建双链表\n");
         
        return ;
    }
    //找尾指针:最后一个节点的地址
    pLINK temp;
    for(temp=head;temp->next!=NULL;temp=temp->next);

    //这个循环跳出的条件:temp->next==null;
    pLINK p=(pLINK)malloc(sizeof(LINK));
    p->num=getNum(head);
    p->next=NULL;
    p->pre=temp;
    temp->next=p;
    printf("尾部数据插入成功\n");
}

void headDeleteData(pLINK head)
{
    if(head==NULL||head->next==NULL)
    {
        printf("无信息可删\n");
        return;
    }
    if(head->next->next==NULL)
    {
        free(head->next);
        head->next=NULL;
        return;
    }

    pLINK p=head->next;
    head->next=p->next;
    p->next->pre=head;
    free(p);
    p=NULL;
}

void tailDeleteData(pLINK head)
{
    if(head==NULL||head->next==NULL)
    {
        printf("无信息可删\n");
        return;
    }
    pLINK temp;
    for(temp=head;temp->next!=NULL;temp=temp->next);
    pLINK p=temp->pre;
    free(temp);
    p->next=NULL;      
}

void printData(pLINK head)//打印信息 
{
    if(head==NULL||head->next==NULL)
    {
        printf("无信息可打印\n");
        return;
        
    }
    pLINK temp;
    printf("head-->");
    for(temp=head->next;temp!=NULL;temp=temp->next)
    {
        printf("[%d]-->",temp->num);
    }
    printf("NULL\n");
} 
int main()
{
    pLINK head=NULL;
    int select;
    while (1)
    {
        printf("========\n");
        printf("1.创建链表\n");
        printf("2.头插文件\n");
        printf("3.尾插文件\n");
        printf("4.头删文件\n");             
        printf("5.尾删文件\n");
        printf("6.打印数据\n");
        printf("7.退出\n");               
        printf("========\n");       
        scanf("%d",&select);
        switch(select)
        {
            case 1:
            head=createDoubleList(head);
                break;
            case 2:
            headInsertData(head);
                break;
            case 3:
            tailInsertData(head);
                break;                              
            case 4:
            headDeleteData(head);
                break;              
            case 5:
            tailDeleteData(head);
                break;              
            case 6:
            printData(head);
                break;  
            case 7:
                
                return 0;
            default:
                break;                          
        }       
    }
}

相关文章

网友评论

      本文标题:双链表

      本文链接:https://www.haomeiwen.com/subject/nljsuttx.html