Mage??? 2016-02-20 11:27 采纳率: 40%
浏览 1518

关于链表打印问题,链表头与表头指针所指向的节点。

typedef struct Node
{
int data;
struct Node *next;
}SLIST;

SLIST *Creat_SList();
int SList_Print(SLIST *pHead);
//在结点数值为x的前面插入y
int SList_NodeInsert(SLIST *pHead, int x, int y);
//删除结点为y的链表结点
int SList_NodeDel(SLIST *pHead, int y);
int SList_Destory(SLIST *pHead);

SLIST *Creat_SList()
{
//1 创建头结点并初始化
int data = 0;
SLIST *pHead = NULL, *pM = NULL, *pCur = NULL;
pHead = (SLIST *)malloc(sizeof(SLIST));
pHead->data = 0;
pHead->next = NULL;

//2循环创建结点,结点数据域中的数值从键盘输入,
//以-1作为输入结束标志
printf("\nPlease enter the data of node(-1:quit) ");
scanf("%d", &data);

//准备环境 让pCur指向pHead
pCur = pHead;
while(data != -1)
{
    //malloc新节点 并且数据域 赋值
    pM = (SLIST *)malloc(sizeof(SLIST));
    pM->data = data;
    pM->next = NULL;

    //1新节点入链表
    pCur->next = pM;

    //2 当前结点下移(新结点变成当前结点)
    pCur = pM; // (pCur = pCur->next) //回想创建的内存模型你立刻就知道了。

    printf("\nPlease enter the data of node(-1:quit) ");
    scanf("%d", &data);
}
return pHead;

}
上面是创建链表代码,下面是打印链表代码。
int SList_Print(SLIST *pHead)
{
SLIST *p = NULL;

if (pHead == NULL)
{
    return -1;
}
//准备环境
p = pHead->next;
printf("\nBegin ");
while(p)
{
    printf("%d ", p->data);
    p = p->next;
}
printf("End ");
return 0;

}
创建链表中,头结点也记录了用户输入,打印链表的代码,我看应该不打印头结点数据,应该从第二个节点开始打印,为什么这打印代码也从头结点开始打印了?
麻烦各位看一下。

  • 写回答

1条回答

  • ysuwood 2016-02-20 11:41
    关注

    你看错了吧? 头结点没有记录输入的数据,赋了0

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog