#include "stdafx.h"
#include "stdio.h"
#include <stdlib.h>
#include "string.h"
typedef
int
elemType ;
typedef
struct
Node{
elemType element;
Node *next;
}Node;
void
initList(Node **pNode)
{
*pNode = NULL;
printf
(
"initList函數執(zhí)行,初始化成功\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函數執(zhí)行,鏈表創(chuàng)建成功\n"
);
return
pHead;
}
void
printList(Node *pHead)
{
if
(NULL == pHead)
{
printf
(
"PrintList函數執(zhí)行,鏈表為空\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函數執(zhí)行,鏈表為空\n"
);
return
;
}
while
(pHead->next != NULL)
{
pNext = pHead->next;
free
(pHead);
pHead = pNext;
}
printf
(
"clearList函數執(zhí)行,鏈表已經清除\n"
);
}
int
sizeList(Node *pHead)
{
int
size = 0;
while
(pHead != NULL)
{
size++;
pHead = pHead->next;
}
printf
(
"sizeList函數執(zhí)行,鏈表長度 %d \n"
,size);
return
size;
}
int
isEmptyList(Node *pHead)
{
if
(pHead == NULL)
{
printf
(
"isEmptyList函數執(zhí)行,鏈表為空\n"
);
return
1;
}
printf
(
"isEmptyList函數執(zhí)行,鏈表非空\n"
);
return
0;
}
elemType getElement(Node *pHead,
int
pos)
{
int
i=0;
if
(pos < 1)
{
printf
(
"getElement函數執(zhí)行,pos值非法\n"
);
return
0;
}
if
(pHead == NULL)
{
printf
(
"getElement函數執(zhí)行,鏈表為空\n"
);
return
0;
}
while
(pHead !=NULL)
{
++i;
if
(i == pos)
{
break
;
}
pHead = pHead->next;
}
if
(i < pos)
{
printf
(
"getElement函數執(zhí)行,pos值超出鏈表長度\n"
);
return
0;
}
return
pHead->element;
}
elemType *getElemAddr(Node *pHead, elemType x)
{
if
(NULL == pHead)
{
printf
(
"getElemAddr函數執(zhí)行,鏈表為空\n"
);
return
NULL;
}
if
(x < 0)
{
printf
(
"getElemAddr函數執(zhí)行,給定值X不合法\n"
);
return
NULL;
}
while
((pHead->element != x) && (NULL != pHead->next))
{
pHead = pHead->next;
}
if
((pHead->element != x) && (pHead != NULL))
{
printf
(
"getElemAddr函數執(zhí)行,在鏈表中未找到x值\n"
);
return
NULL;
}
if
(pHead->element == x)
{
printf
(
"getElemAddr函數執(zhí)行,元素 %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函數執(zhí)行,鏈表為空\n"
);
}
if
(pos < 1)
{
printf
(
"modifyElem函數執(zhí)行,pos值非法\n"
);
return
0;
}
while
(pHead !=NULL)
{
++i;
if
(i == pos)
{
break
;
}
pHead = pHead->next;
}
if
(i < pos)
{
printf
(
"modifyElem函數執(zhí)行,pos值超出鏈表長度\n"
);
return
0;
}
pNode = pHead;
pNode->element = x;
printf
(
"modifyElem函數執(zhí)行\(zhòng)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函數執(zhí)行,向表頭插入元素成功\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函數執(zhí)行,向表尾插入元素成功\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函數執(zhí)行,位置 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"
);
}