typedef
struct
Node{
elemType
element;
Node
*next;
}Node;
void
initList(Node
**pNode)
{
*pNode
= NULL;
printf
(
"initList函数执行,初始化成功\n"
);
}
Node
*creatList(Node *pHead)
{
Node
*p1;
Node
*p2;
p1=p2=(Node
*)
malloc
(
sizeof
(Node));
if
(p1
== NULL || p2 ==NULL)
{
printf
(
"内存分配失败\n"
);
exit
(0);
}
memset
(p1,0,
sizeof
(Node));
scanf
(
"%d"
,&p1->element);
p1->next
= NULL;
while
(p1->element
> 0)
{
if
(pHead
== NULL)
{
pHead
= p1;
}
else
{
p2->next
= p1;
}
p2
= p1;
p1=(Node
*)
malloc
(
sizeof
(Node));
if
(p1
== NULL || p2 ==NULL)
{
printf
(
"内存分配失败\n"
);
exit
(0);
}
memset
(p1,0,
sizeof
(Node));
scanf
(
"%d"
,&p1->element);
p1->next
= NULL;
}
printf
(
"creatList函数执行,链表创建成功\n"
);
return
pHead;
}
void
printList(Node
*pHead)
{
if
(NULL
== pHead)
{
printf
(
"PrintList函数执行,链表为空\n"
);
}
else
{
while
(NULL
!= pHead)
{
printf
(
"%d
"
,pHead->element);
pHead
= pHead->next;
}
printf
(
"\n"
);
}
}
void
clearList(Node
*pHead)
{
Node
*pNext;
if
(pHead
== NULL)
{
printf
(
"clearList函数执行,链表为空\n"
);
return
;
}
while
(pHead->next
!= NULL)
{
pNext
= pHead->next;
free
(pHead);
pHead
= pNext;
}
printf
(
"clearList函数执行,链表已经清除\n"
);
}
int
sizeList(Node
*pHead)
{
int
size
= 0;
while
(pHead
!= NULL)
{
size++;
pHead
= pHead->next;
}
printf
(
"sizeList函数执行,链表长度
%d \n"
,size);
return
size;
}
int
isEmptyList(Node
*pHead)
{
if
(pHead
== NULL)
{
printf
(
"isEmptyList函数执行,链表为空\n"
);
return
1;
}
printf
(
"isEmptyList函数执行,链表非空\n"
);
return
0;
}
elemType
getElement(Node *pHead,
int
pos)
{
int
i=0;
if
(pos
< 1)
{
printf
(
"getElement函数执行,pos值非法\n"
);
return
0;
}
if
(pHead
== NULL)
{
printf
(
"getElement函数执行,链表为空\n"
);
return
0;
}
while
(pHead
!=NULL)
{
++i;
if
(i
== pos)
{
break
;
}
pHead
= pHead->next;
}
if
(i
< pos)
{
printf
(
"getElement函数执行,pos值超出链表长度\n"
);
return
0;
}
return
pHead->element;
}
elemType
*getElemAddr(Node *pHead, elemType x)
{
if
(NULL
== pHead)
{
printf
(
"getElemAddr函数执行,链表为空\n"
);
return
NULL;
}
if
(x
< 0)
{
printf
(
"getElemAddr函数执行,给定值X不合法\n"
);
return
NULL;
}
while
((pHead->element
!= x) && (NULL != pHead->next))
{
pHead
= pHead->next;
}
if
((pHead->element
!= x) && (pHead != NULL))
{
printf
(
"getElemAddr函数执行,在链表中未找到x值\n"
);
return
NULL;
}
if
(pHead->element
== x)
{
printf
(
"getElemAddr函数执行,元素
%d 的地址为 0x%x\n"
,x,&(pHead->element));
}
return
&(pHead->element);
}
int
modifyElem(Node
*pNode,
int
pos,elemType
x)
{
Node
*pHead;
pHead
= pNode;
int
i
= 0;
if
(NULL
== pHead)
{
printf
(
"modifyElem函数执行,链表为空\n"
);
}
if
(pos
< 1)
{
printf
(
"modifyElem函数执行,pos值非法\n"
);
return
0;
}
while
(pHead
!=NULL)
{
++i;
if
(i
== pos)
{
break
;
}
pHead
= pHead->next;
}
if
(i
< pos)
{
printf
(
"modifyElem函数执行,pos值超出链表长度\n"
);
return
0;
}
pNode
= pHead;
pNode->element
= x;
printf
(
"modifyElem函数执行\n"
);
return
1;
}
int
insertHeadList(Node
**pNode,elemType insertElem)
{
Node
*pInsert;
pInsert
= (Node *)
malloc
(
sizeof
(Node));
memset
(pInsert,0,
sizeof
(Node));
pInsert->element
= insertElem;
pInsert->next
= *pNode;
*pNode
= pInsert;
printf
(
"insertHeadList函数执行,向表头插入元素成功\n"
);
return
1;
}
int
insertLastList(Node
**pNode,elemType insertElem)
{
Node
*pInsert;
Node
*pHead;
Node
*pTmp;
pHead
= *pNode;
pTmp
= pHead;
pInsert
= (Node *)
malloc
(
sizeof
(Node));
memset
(pInsert,0,
sizeof
(Node));
pInsert->element
= insertElem;
while
(pHead->next
!= NULL)
{
pHead
= pHead->next;
}
pHead->next
= pInsert;
*pNode
= pTmp;
printf
(
"insertLastList函数执行,向表尾插入元素成功\n"
);
return
1;
}
int
main()
{
Node
*pList=NULL;
int
length
= 0;
elemType
posElem;
initList(&pList);
printList(pList);
pList=creatList(pList);
printList(pList);
sizeList(pList);
printList(pList);
isEmptyList(pList);
posElem
= getElement(pList,3);
printf
(
"getElement函数执行,位置
3 中的元素为 %d\n"
,posElem);
printList(pList);
getElemAddr(pList,5);
modifyElem(pList,4,1);
printList(pList);
insertHeadList(&pList,5);
printList(pList);
insertLastList(&pList,10);
printList(pList);
clearList(pList);
system
(
"pause"
);
}