#include <stdio.h>
#include <stdlib.h>
typedef int ElemType;
/*
typedef struct Link {
ElemType *pElem;
struct Link* next;
}List;
*/
typedef struct Link {
ElemType elem;
struct Link *next;
}List;
/*
void InitList(List *L)
{
L->pElem = malloc(sizeof(ElemType));
L->next = 0;
}
*/
/*
void InitList(List **L)
{
*L = malloc(sizeof(List));
(*L)->next = NULL;
}
*/
void InitList(List **L, ElemType elem)
{
if ( L == NULL )
{
printf("Initial list fail!/n");
return ;
}
*L = malloc(sizeof(List));
(*L)->next = NULL;
(*L)->elem = elem;
printf("Initial list succeed!/n");
}
/*
void DestroyLink(List *L)
{
List *p = 0;
while ( L )
{
p = L;
L = L->next;
free(p);
p = NULL;
}
}
*/
/*
void DestroyList(List *L)
{
if ( L->next )
{
if ( L->pElem )
{
free(L->pElem);
L->pElem = NULL;
}
free(L);
L = L->next;
}
free(L);
}
*/
/* List *L */
void DestroyList(List **L)
{
List *p = 0;
while ( *L )
{
p = *L;
*L = (*L)->next;
free(p);
p = NULL;
}
}
void Insert(List *L,int nPos,ElemType elem)
{
int i;
List *pNewNode = 0;
if ( nPos < 1 )
return ;
pNewNode = malloc(sizeof(List));
pNewNode->elem = elem;
if ( nPos == 1 )
{
/* pNewNode->next = L;
pNewNode = L;
InitList(&L,elem);
L->next = pNewNode; */
pNewNode->elem = L->elem;
L->elem = elem;
pNewNode->next = L->next;
L->next = pNewNode;
printf("Insert first!/n");
return ;
}
for ( i=1; i<nPos-1; i++ )
{
L = L->next;
}
if ( L == 0 )
return ;
pNewNode->next = L->next;
L->next = pNewNode;
printf("Insert elem succeed!/n");
}
void DelElemByPos(List *L,int nPos)
{
int i;
List *p = NULL;
if ( nPos < 1 )
return ;
if ( nPos == 1 )
{
/* p = L;
L = L->next;
free(p);
p = NULL; */
p = L->next;
L->elem = L->next->elem;
L->next = L->next->next;
free(p);
p = NULL;
return ;
}
for (i=1; i<nPos; i++)
{
p = L;
L = L->next;
}
if ( L == NULL )
return ;
p->next = L->next;
free(L);
L = NULL;
}
int FindByValue(List *L,ElemType elem)
{
int i = 0;
do
{
i++;
if ( L->elem == elem )
{
break;
}
L = L->next;
}while(L);
if ( L )
return i;
else
return -1;
}
ElemType FindByPos(List *L,int nPos)
{
int i = 0;
if ( nPos < 1 )
exit(1);
for ( i=1; i<nPos; i++ )
{
L = L->next;
}
if ( L )
return L->elem;
}
void DelElemByValue(List *L,ElemType elem)
{
List *p = 0;
if ( L->elem == elem )
{
/* p = L;
L = L->next;
free(p);
p = NULL; */
p = L->next;
L->elem = p->elem;
L->next = p->next;
free(p);
p = NULL;
return ;
}
else
{
while ( L )
{
p = L;
L = L->next;
if ( L->elem == elem )
{
p->next = L->next;
free(L);
L = NULL;
break;
}
}
}
}
void TranverseList(List *L)
{
while ( L )
{
printf("%d/n",L->elem);
L = L->next;
}
}
main()
{
int a = 0;
ElemType elem;
List *L;
clrscr();
InitList(&L,12);
TranverseList(L);
Insert(L,1,8);
Insert(L,2,7);
Insert(L,2,2);
Insert(L,3,3);
Insert(L,1,1);
Insert(L,6,9);
a = FindByValue(L,2);
elem = FindByPos(L,3);
printf("%d,%d/n",a,elem);
TranverseList(L);
DelElemByPos(L,1);
DelElemByValue(L,7);
printf("/n");
TranverseList(L);
DestroyList(&L);
TranverseList(L);
getch();
}
在实现中需要注意的几个地方:
1.传值与传址:在需要传址的时候使用了传值,导致函数调用后,链表并没有发生改变。
2.for循环的起始值和结束值设置不正确。
3.注意对链表的位置1作特别处理。例如:在第一个位置插入、删除等等。
4.在什麽情况需要free掉分配的内存空间。
下面是一些错误的函数:
void Insert(List *L,int nPos,ElemType elem)
{
int i;
List *pNewNode = 0;
if ( nPos < 1 )
return ;
pNewNode = malloc(sizeof(List));
pNewNode->pElem = &elem;
if ( nPos == 1 )
{
pNewNode->next = L;
return ;
}
for ( i=1; i<nPos-1; i++ )
{
L = L->next;
}
if ( L == 0 )
return ;
pNewNode->next = L->next;
L->next = pNewNode;
}
void Insert(List *L,int nPos,ElemType elem)
{
int i;
List *pNewNode = 0;
if ( nPos < 1 )
return ;
pNewNode = malloc(sizeof(List));
pNewNode->elem = elem;
if ( nPos == 1 )
{
pNewNode = L;
InitList(&L,elem);
L->next = pNewNode;
printf("Insert first!/n");
return ;
}
for ( i=1; i<nPos-1; i++ )
{
L = L->next;
}
if ( L == 0 )
return ;
pNewNode->next = L->next;
L->next = pNewNode;
printf("Insert elem succeed!/n");
}
void DelElemByPos(List *L,int nPos)
{
int i;
List *p = NULL;
if ( nPos < 1 )
return ;
if ( nPos == 1 )
{
p = L;
L = L->next; /* ??? */
free(p);
p = NULL;
}
for (i=1; i<nPos; i++)
{
p = L;
L = L->next;
}
if ( L == NULL )
return ;
p->next = L->next;
free(L);
L = NULL;
}
void DelElemByValue(List *L,ElemType elem)
{
List *p = 0;
if ( L->elem == elem )
{
p = L;
L = L->next; /* ??? */
free(p);
p = NULL;
}
else
{
while ( L )
{
p = L;
L = L->next;
if ( L->elem == elem )
{
p->next = L->next;
free(L);
L = NULL;
break;
}
}
}
}