完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
本帖最后由 snowmay001 于 2016-5-22 15:57 编辑
lianbiao.cpp /* 练习使用链表:创建链表、遍历链表、查找节点、添加节点、删除节点 */ #include "stdio.h" #include "string.h" #include "assert.h" #include "stdlib.h" #include "windows.h" #define COUNT 3 //定义一个节点结构体 struct NODE { unsigned long uID; char strName[16]; //用指针的话会出访问冲突异常 struct NODE *next; }; //创建一个具有n个节点的链表,从键盘输入数据将其初始化,并返回链表的首节点指针 struct NODE *createNode ( int n ) { struct NODE *pHead, //首节点指针 *pRear, //尾节点指针 *pNew; //新节点指针 int i; int ret; char *strName = ( char * ) malloc ( 16 ); for ( i = 0; i < n; i++ ) { pNew = ( struct NODE * ) malloc ( sizeof ( struct NODE ) ); do { printf ( "请输入ID和名称: " ); ret = scanf ( "%d %s", &pNew->uID, strName ); if ( ret != 2 ) { rewind ( stdin ); //清空缓冲区 printf ( "input i errorrn" ); continue; } if ( strlen ( strName ) > 16 ) { printf ( "rn输入名称长度超出范围,请重新输入: rn" ); continue; } break; } while ( 1 ); strcpy ( pNew->strName, strName ); if ( 0 == i ) { pRear = pHead = pNew; } else { pRear->next = pNew; } pNew->next = NULL; pRear = pNew; } free ( strName ); return pHead; } //打印链表中所有节点的数据 void printNode ( struct NODE *pHead ) { struct NODE *pTemp = pHead; assert ( pTemp != NULL ); while ( pTemp != NULL ) { printf ( "%d %s ", pTemp->uID, pTemp->strName ); pTemp = pTemp->next; } printf ( "rn" ); } //查询链表中具有指定ID的节点,并返回此节点指针 NODE *searchNode ( struct NODE *pHead, unsigned long uID ) { struct NODE *pDest = pHead; assert ( pDest != NULL ); while ( pDest->next != NULL && pDest->uID != uID ) { pDest = pDest->next; } if ( pDest->uID == uID ) { return pDest; } else { printf ( "搜索失败,未找到找定ID的节点! rn" ); return NULL; } } //删除指定ID的节点 struct NODE *deleteNode ( struct NODE *pHead, unsigned long uID ) { struct NODE *pDest, //要删除的节点 *pBefore; //前一个节点 前节点 --> 要删除节点 --> 后节点 pDest = pHead; assert ( pDest != NULL ); while ( pDest->next != NULL && pDest->uID != uID ) { pBefore = pDest; //保存前节点 pDest = pDest->next;//移动节点 } if ( pDest->uID == uID ) { //判断是否要删除头节点,如果是则要保存新的头节点 if ( pDest == pHead ) { pHead = pDest->next; } else { //前节点 --> 要删除节点 --> 后节点 ,中间节点删除后,把前节点和后节点连接 pBefore->next = pDest->next; } free ( pDest ); printf ( "节点已被删除! rn" ); } else { printf ( "未找到指定节点,无法将其删除! rn" ); } return pHead; } //在指定ID的节点后插入一个新的节点 NODE *insertNode ( struct NODE *pHead, unsigned long uID ) { struct NODE *pDest, *pNew; int ret; pDest = pHead; assert ( pDest != NULL ); while ( pDest->next != NULL && pDest->uID != uID ) { pDest = pDest->next; } if ( pDest->uID == uID ) { pNew = ( struct NODE * ) malloc ( sizeof ( struct NODE ) ); do { printf ( "请输入新节点的ID和名称: " ); ret = scanf ( "%d %s", &pNew->uID, pNew->strName ); if ( ret != 2 ) { rewind ( stdin ); //清空缓冲区 printf ( "格式错误,请重新输入!rn" ); continue; } if ( strlen ( pNew->strName ) > 16 ) { printf ( "rn输入名称长度超出范围,请重新输入! rn" ); continue; } break; } while ( 1 ); //保存目标节点的下一个节点保存为新节点的下一个 pNew->next = pDest->next; //保存新节点为目标节点的下一个 pDest->next = pNew; printf ( "节点插入完成! rn" ); } else { printf ( "未找到指定节点! rn" ); } return pHead; } int main ( void ) { struct NODE *pHead, *pDest; int nSelect; bool isExit = false; unsigned long uID; int ret; printf ( "请根据提示输入数据初始化链表: rn" ); pHead = createNode ( COUNT ); assert ( pHead != NULL ); printf ( "链表创建成功!存储数据如下: rn" ); printNode ( pHead ); while ( !isExit ) { printf ( "rn-------------------------- rn" ); printf ( "请选择要进行的操作: rn" ); printf ( "1.打印出链表所有节点数据: rn" ); printf ( "2.输入ID查询名称。 rn" ); printf ( "3.删除指定ID的节点。 rn" ); printf ( "4.在指定ID的节点后插入一个新的节点。 rn" ); printf ( "5.退出。 rn" ); printf ( "--------------------------- rn" ); ret = scanf ( "%d", &nSelect ); if ( ret == 1 ) { printf ( "你输入了:%drn", nSelect ); } else { rewind ( stdin ); //清空缓冲区 printf ( "输入错误,请重新选择rn" ); continue; } switch ( nSelect ) { case 1: printf ( "链表所有节点数据如下: rn" ); printNode ( pHead ); break; case 2: printf ( "请输入要查询的节点的ID: rn" ); scanf ( "%d", &uID ); pDest = searchNode ( pHead, uID ); if ( pDest != NULL ) { printf ( "已找到节点,名字为:%s rn", pDest->strName ); } break; case 3: printf ( "请输入要删除的节点的ID:" ); scanf ( "%d", &uID ); pHead = deleteNode ( pHead, uID ); break; case 4: printf ( "请输入要删除的节点的ID: " ); scanf ( "%d", &uID ); pHead = insertNode ( pHead, uID ); break; case 5: isExit = true; printf ( "程序已退出! rn" ); break; default: printf ( "按键错误,请重新选择! rn" ); break; } } return 0; } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
12133 浏览 2 评论
4468 浏览 3 评论
3702 浏览 5 评论
9530 浏览 47 评论
4505 浏览 9 评论
706浏览 0评论
507浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 15:17 , Processed in 0.587705 second(s), Total 81, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号