|
单链表比较简单,没什么好说的,直接上代码:
#include
#include
#include
#define NUM 10
typedef struct _chbList
{
int data;
struct _chbList *next;
}chbList;
/**
* pList:链表添加至末尾
* 时间:2009.7.29
*/
int AddTail( chbList *pList, chbList *pTail )
{
if( pList == NULL || pTail == NULL )
return 0;
while( pList->next )
{
pList = pList->next;
}
pTail->next = NULL;
pList->next = pTail;
return 1;
}
/**
* pList:链表根据位置插入到链表位置
* 时间:2009.7.29
*/
int InsertValue( chbList *pList, chbList *insert, int seq )
{
if( pList == NULL || insert == NULL )
return 0;
int i = 0;
while( pList && i next;
i++;
}
insert->next = pList->next;
pList->next = insert;
return 1;
}
/**
* pList:链表根据位置删除链表
* 时间:2009.7.29
*/
int DeleteValue( chbList *pList, int seq )
{
if( pList == NULL )
return 0;
chbList *pT;
int i = 0;
while( pList && i next;
i++;
}
pT = pList->next;
pList->next = pT->next;
free( pT );
return 1;
}
int main( void )
{
chbList *pList,*pTemp;
int i;
pList = ( chbList * )malloc( sizeof( chbList ) );
pList->next = NULL;
pList->data = 0;
for( i = 0; i data = i+1;
}
chbList *insert = ( chbList * )malloc( sizeof( chbList ) );
insert->data = -1;
InsertValue( pList, insert, 4 );
DeleteValue( pList, 2 );
while( pList )
{
printf( "%d ", pList->data );
pList = pList->next;
}
while( pList )
{
free( pList );
pList = pList->next;
}
return 0;
} |
|