typedef int DataType;
typedef struct Node
{
DataType data;
struct Node* next;
}LNode, *LinkList;
遍历:按序号依次输出线性表的长度(设置工作指针,工作指针后移)
void PrintList(LNode* head)
{
LNode* p = head->next;
while (p != NULL)//工作指针不能为空
{
peintf("%d ", p->data);
p = p->next;//指针后移
}
}
获得单链表的长度
int Length(LNode* head)
{
LNode* p = head->next;
int count = 0;
while (p != NULL)
{
count++;
p = p->next;
}
return count;
}
按位查找的函数(找下标为i的元素)
int Get(LNode* head, int i, DataType* ptr)
{
LNode* p = head->next;
int count = 1;
while (p != NULL && count < i)
{
count++;
p = p->next;
}
if (p == NULL)
{
printf("位置错误,查找失败\n");
return 0;
}
else
{
*ptr = p->data;
return 1;
}
}
按值查找的函数(找数据为x的元素)
int Locate(LNode* head, DataType x)
{
LNode* p = head->next;
int count = 1;
while (p != NULL)
{
if (p != NULL)
return count;
count++;
p = p->next;
}
return 0;
}