程序笔记   发布时间:2022-07-05  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了线性表-TwoLinkList(双链表)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
知识点
   1:循环双链表尾结点指向头结点
typedef struct TWONode
{
    int data;
    pNode *prev,*next;
} pNode;
int main(int argc, char const *argv[])
{
    int n, element, flag;
    pNode *head, *last;
    /***************************************************************/
    printf("Please input the size of the list:");
    scanf("%d", &n);
    last = InitList(&head, n);//初始化链表并赋值,返回尾节点last
    printf("%d %d n", head->next->data, last->data); //打印为第一个元素和最后一个元素
    PrintList(head);
    /***************************************************************/
    flag = SearchList(head); //搜索某个值并删除节点
    if (flag > 0 && flag <= n)
    {
        DelNumqList(&head, flag);
        PrintList(head);
    }
    else
        printf("Element does not exist, cannot be deletedn");
    /***************************************************************/
    DeleteList(&head);//清空列表
    PrintList(head);
    return 0;
}

初始化双链表尾插法

pNode *InitList(pNode **head,int n)
{
    pNode *p,*s;
    *head=(pNode *)malloc(sizeof(pNode));
    if(*head==NUll)
    {
        return 0;
    }
    *head->next=NULL;
    *head->prev=NULL;
    p=*head;
    int i;
    for(i=0;i<n;++i)
    {
        s=(pNode *)malloc(sizeof(pNode));
        if(s==0)return 0;
        printf("Input the value of the %dth node:", i + 1);
        scanf("%d",&s->data);
        s->next=NULL;
        p->next=s;
        s->prev=p;
        p=s;
    }
    return p;

}

遍历打印

void prinList(pNode *head)
{
    pNode *p;
    p=head->next;
    if(head->next==NULL)
    {
        return 0;
    }
    while(p!=NULL)
    {
        printf("%d ",p->data);
        p=p->next;
    }
}

请空链表

void delectList(pNode **head)
{
    pNode *p;
    while(*head->next!=NULL)
    {
        p=*head;
        p->next->prev=NULL;
        *head=p->next;
        free(p);
    }
}

查找

int searchList(pNode *head)
{
    int number;
    printf("Values are about to be deleted:");
    scanf("%d",&number);
    pNode *p;
    p=head->next;
    while(p!=NULL)
    {
        if(p->data==number)
        {
            return number;
        }
        p=p->next;
    }
    return 0;
}

删除元素

void delNumList(pNode **head,int n)
{
    int i;
    pNode *p;
    p=*head->next;
    for(i=1;i<n;++i)
    {
        p=p->next;
    }
    if(p->next==NULl)//如果p是尾节点则直接将前驱节点指向NULL
    {
        p->prev->next==NULL;
        free(p);
    }
    else//令p的前驱节点和后驱节点相互指向即可
    {
        p->next->prev=p->prev;
        p->prev->next=p->next;
        free(p);
    }
}

 

大佬总结

以上是大佬教程为你收集整理的线性表-TwoLinkList(双链表)全部内容,希望文章能够帮你解决线性表-TwoLinkList(双链表)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: