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 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)