数据结构 单链表的创建 插入 删除
2009-07-29 13:27
423 查看
单链表比较简单,没什么好说的,直接上代码:
#include <stdio.h> #include <stdlib.h> #include <string.h> #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 < seq-1 ) { pList = pList->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 < seq-1 ) { pList = pList->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 < NUM-1; i++ ) { pTemp = ( chbList * )malloc( sizeof( chbList ) ); AddTail( pList, pTemp ); pTemp->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; }
相关文章推荐
- 数据结构单链表插入\整表删除\整表创建\
- 数据结构----单链表的创建、插入、删除、读取、遍历
- 单链表的创建、插入、删除、查找
- 单链表的创建,删除,插入,排序
- 数据结构_单链表的插入与删除_C语言源代码
- 单链表的创建, 删除, 插入, 输出
- 数据结构之单链表——带有节点的单链表的创建、插入和删除(C/C++)
- 单链表的创建,插入,删除以及逆序
- c 单链表的创建、插入、删除、倒置操作
- java实现单链表的初始化,创建,删除,插入,查找,排序,同项删除,退出等功能
- 数据结构:线性表(顺序存储)顺序表类(实现顺序表的创建,输出,插入,删除功能)
- 数据结构 二叉排序树的创建,查找,插入,删除
- 单链表的创建、插入删除等操作
- 单链表的创建、插入、删除
- C语言单链表的创建、插入、查找、删除、求长、排序、遍历
- C++单链表的操作(创建,删除,打印,遍历,插入)
- 链表初解(一)——单链表的创建、删除、插入、测长、排序、逆置
- 单链表的创建、插入、删除、倒置操作
- 单链表的创建,插入删除等操作
- 数据结构_线性表_顺序表 的创建,插入,删除,查找