(续)线性表之双向链表(C语言实现)

在前文实现单向链表的基本操作下,本文实现

双向链表的基本操作.


双向链表与单链表差异,是双向链表结点中有前向指针和后向指针.

所以在插入和删除新结点元素时候不见要考虑后向指针还要考虑

前向指针.



以下是双向链表的C代码:

#include<stdio.h>

typedef struct node
{
    int data;
    struct node *next;
    struct node *prior
}Node;

//链表的初始化
Node* InitList(int number)
{
    int i;
    Node *pHead=(Node *)malloc(sizeof(Node));
    Node *TempHead=pHead;
    Node *Head=pHead;
    Head->prior=NULL;
    int data;
    for(i=0;i<number;i++)
    {
        pHead=(Node *)malloc(sizeof(Node));
        printf("Please input the %dst node data:\n",i+1);
        scanf("%d",&data);
        pHead->data=data;
        pHead->next=NULL;
        pHead->prior=TempHead;
        TempHead->next=pHead;
        TempHead=pHead;
    }
    return Head;
}

//显示链表
void ShowList(Node *Head)
{

    Head=Head->next;
    while(Head->next!=NULL)
    {
        printf("%d ",Head->data);
        Head=Head->next;
    }
    printf("%d",Head->data);
    printf("\n");
}

//输出链表某个值的位置
int ListLocation(Node *Head,int data,int number)
{
    Node *TempNode=Head;
    int location=1;
    TempNode=TempNode->next;
    while(TempNode->next!=NULL)
    {
        if(TempNode->data==data)
            {
                return location;
            }
            location++;
            TempNode=TempNode->next;
    }
    if(location>=number)
        printf("Not found!");
}

//输出链表某个位置的值
int ListData(Node *Head,int location,int number)
{
    if(location>number)
        printf("Not found!");

    Node *TempNode=Head;
    TempNode=TempNode->next;
    int i;
    for(i=1;i<=number;i++)
    {
        if(location==i)
            return TempNode->data;
        TempNode=TempNode->next;
    }
}

//头入法插入元素
void HeadInsertData(Node *Head,int data)
{
    Node *InsertNode=(Node *)malloc(sizeof(Node));
    InsertNode->data=data;
    InsertNode->next=Head->next;
    Head->next->prior=InsertNode;
    Head->next=InsertNode;
    InsertNode->prior=Head;
}

//尾入插入除元素
void TailInsertData(Node *Head,int data)
{
    Node *TempNode=Head;
    Node *DeleteNode=(Node *)malloc(sizeof(Node));
    DeleteNode->data=data;
    while(TempNode->next!=NULL)
        TempNode=TempNode->next;

    TempNode->next=DeleteNode;
    DeleteNode->next=NULL;
    DeleteNode->prior=TempNode;

    free(DeleteNode);
}



//删除头结点
void HeadDeleteData(Node *Head)
{
    Head->next=Head->next->next;
    Head->next->prior=Head;
}


//删除尾结点
void TailDeleteData(Node *Head)
{
    Node *TempNode=Head;
    while(Head->next->next!=NULL)
        Head=Head->next;

    Head->next=NULL;
    Head->next->prior=NULL;
}

int main()
{
    Node *Head;
    int number;
    printf("Please input the node number:\n");
    scanf("%d",&number);
    Head=InitList(number);
    printf("The initital list is:\n");
    ShowList(Head);

    int flag;
    printf("\n\n");
    printf("**********************Your Choice********************\n");
    printf("****************1-输出链表某个值的位置***************\n");
    printf("****************2-输出链表某个位置的值***************\n");
    printf("****************3-头入法插入元素*********************\n");
    printf("****************4-尾入法插入元素*********************\n");
    printf("****************5-删除头结点*************************\n");
    printf("****************6-删除尾结点*************************\n");
    printf("****************0-退出*******************************\n");
    printf("\n\n");
    printf("Please input flag:\n");
    scanf("%d",&flag);

    switch(flag)
    {
        case 1:
        {
            int data;
            printf("Please input the data you want locate:\n");
            scanf("%d",&data);
            int location;
            location=ListLocation(Head,data,number);
            printf("The data's location is: %d",location);
            break;
        }
        case 2:
        {
            int location;
            printf("Please input the location you want  data:\n");
            scanf("%d",&location);
            int data;
            data=ListData(Head,location,number);
            printf("The location's data is: %d\n",data);
            break;
        }
        case 3:
        {
            int data;
            printf("Please input the data you want insert in head:\n");
            scanf("%d",&data);
            HeadInsertData(Head,data);
            ShowList(Head);
            break;
        }
        case 4:
        {
            int data;
            printf("Please input the data you want insert in tail:\n");
            scanf("%d",&data);
            TailInsertData(Head,data);
            ShowList(Head);
            break;
        }
        case 5:
        {
            HeadDeleteData(Head);
            ShowList(Head);
            break;
        }
        case 6:
        {
            TailDeleteData(Head);
            ShowList(Head);
            break;
        }
        case 7:
        {
           printf("You choose to exit.\n");
           break;
        }
    }
    return 0;
}

结果图:




转载请注明作者:小刘

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值