上海xx 双向链表 面试题 list node

本文介绍了一个使用void*指针实现的双向链表的数据结构。通过具体代码示例展示了链表的创建过程,包括节点的尾部追加,并实现了链表的正向打印功能。文中还详细讨论了void*指针的用法及其在不同类型间转换的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<cstdio>
#include<string>
#include<malloc.h>
using namespace std;

typedef struct mylist
{
    void* head;
    void* tail;
} List,*pList;

typedef struct mynode
{
    void* next;
    void* prev;
    int data;
} Node,*pNode;

pList createlist()
{
    pList root;
    pNode temp;
    int n;
    root = (pList)malloc(sizeof(List));
    root->head = NULL;
    root->tail = NULL;
    printf("input data number:");
    scanf("%d",&n);
    if(n&&NULL==root->head)
    {
        temp = (pNode)malloc(sizeof(Node));
        printf("data1:");
        scanf("%d",&temp->data);
        temp->next = (pList)root;
        temp->prev = (pList)root;
        root->head = (pNode)temp;
        root->tail = (pNode)temp;
        n--;
    }
    for(int i=1; i<=n ; i++)
    {
        temp = (pNode)malloc(sizeof(Node));
        printf("data%2d:",i);
        scanf("%d",&temp->data);
        temp->next = (pList)root;
        temp->prev = (pNode)root->tail;
        ((pNode)(root->tail))->next = (pNode)temp;
        root->tail = (pNode)temp;
    }
    return root;
}

void printlist(pList root)
{
    pNode temp = (pNode)root->head;
    int i = 1;
    while(temp!=(pNode)root)
    {
        printf("data%2d:%d\n",i++,temp->data);
        temp = (pNode)temp->next;
    }

    /*temp = (pNode)root->tail;
    while(temp!=(pNode)root)
    {
        printf("data%2d:%d\n",--i,temp->data);
        temp = (pNode)temp->prev;
    }*/
}

int main()
{
    pList root;
    root = createlist();
    printlist(root);
}


双向链表的面试题要求list和node是不同的结构体,所以这里使用的void*指针

1声明LIst和Node结构体

2创建链表

3尾部追加

4头部删除

都很简单,我只写了创建 打印时测试用的,主要就是试试void*类型的指针用法

上面的所有都加上了显示类型转换

以下去掉了不必要的类型转换

总结一下,

1.当给void*类型指针赋值的时候不需要显示类型转换

2.void*给void*赋值不需要转换

3.当void*给其他类型的指针赋值时需要显示类型转换

4.当void*与不同类型的指针比较的时候需要显示类型转换

5.当需要链式的时候需要显示类型转换。

注释标记对应1,2,3,4,5的情况

注意:不转换不等于不需要转换,而是编译器隐式转换了

#include<iostream>
#include<cstdio>
#include<string>
#include<malloc.h>
using namespace std;

typedef struct mylist
{
    void* head;
    void* tail;
} List,*pList;

typedef struct mynode
{
    void* next;
    void* prev;
    int data;
} Node,*pNode;

pList createlist()
{
    pList root;
    pNode temp;
    int n;
    root = (pList)malloc(sizeof(List));         //malloc属于情况3
    root->head = NULL;
    root->tail = NULL;
    printf("input data number:");
    scanf("%d",&n);
    if(n&&NULL==root->head)
    {
        temp = (pNode)malloc(sizeof(Node));   //3
        printf("data1:");
        scanf("%d",&temp->data);
        temp->next = root;            //1
        temp->prev = root;            //1
        root->head = temp;           //1
        root->tail = temp;              //1
        n--;
    }
    for(int i=1; i<=n ; i++)
    {
        temp = (pNode)malloc(sizeof(Node));  //3
        printf("data%2d:",i);
        scanf("%d",&temp->data);
        temp->next = root;                   //1
        temp->prev = root->tail;          //2
        ((pNode)(root->tail))->next = temp; //5
        root->tail = temp;  //1
    }
    return root;
}

void printlist(pList root)
{
    pNode temp = (pNode)root->head;   //3
    int i = 1;
    while(temp!=(pNode)root)                  //4
    {
        printf("data%2d:%d\n",i++,temp->data);
        temp = (pNode)temp->next;          //3
    }

    /*temp = (pNode)root->tail;
    while(temp!=(pNode)root)
    {
        printf("data%2d:%d\n",--i,temp->data);
        temp = (pNode)temp->prev;
    }*/
}

int main()
{
    pList root;
    root = createlist();
    printlist(root);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值