一、定义单链表结构
- 代码:
typedef int linkType; ///< 定义链表结点数据域数据类型
/// @brief 链表结点定义
typedef struct t_linkNode
{
struct t_linkNode* pnext; ///< 结点指针域
linkType data; ///< 结点数据域
}myLNode;
/// @brief 链表结构定义
typedef struct t_linkList
{
myLNode* phead; ///< 链表头结点指针
}myLinkList;
二、从键盘输入元素创建单链表
1、思路
- 代码:
/// @brief 通过键盘输入元素创建单链表
/// @param plist 链表指针
/// @param flag 当从键盘输入的内容与flag相等时,结束键盘输入
void xxx_input(myLinkList* plist, linkType flag)
{
if (plist == NULL)
{
return;
}
xxx_input_(plist->phead, flag);
}
void xxx_input_(myLNode* phead, linkType flag)
{
if (phead == NULL)
{
return;
}
myLNode* p = phead;
myLNode* pnew = NULL;
linkType data;
printf("请输入链表元素序列:");
while (scanf_s ("%d", &data), data != flag)
{
pnew = (myLNode*)malloc(sizeof(myLNode));
if (pnew == NULL)
{
break;
}
pnew->data = data;
pnew->pnext = NULL;
p->pnext = pnew;
p = pnew;
}
}
标签:单链,myLNode,键盘输入,链表,flag,pnew,data,DS
From: https://www.cnblogs.com/kxwslmsps/p/17125260.html