初始化链表 ,插入结点,删除结点,返回结点位置 ,遍历链表 。
2018-02-27 09:25
447 查看
#include <stdio.h> #include <stdlib.h> /*链表存储结构的定义*/ typedef struct CLinkList { int data; struct CLinkList *next; }node; /************************************************************************/ /* 操作 */ /************************************************************************/ /*初始化循环链表*/ void ds_init(node **pNode) { int item; node *temp; node *target; printf("输入结点的值,输入0完成初始化\n"); while(1) { scanf("%d", &item); // fflush(stdin); if(item == 0) return; if((*pNode) == NULL) { /*循环链表中只有一个结点*/ *pNode = (node*)malloc(sizeof(struct CLinkList)); if(!(*pNode)) exit(0); (*pNode)->data = item; (*pNode)->next = *pNode; } else { /*找到next指向第一个结点的结点*/ for(target = (*pNode); target->next != (*pNode); target = target->next); /*生成一个新的结点*/ temp = (node *)malloc(sizeof(struct CLinkList)); if(!temp) exit(0); temp->data = item; temp->next = *pNode; target->next = temp; } } } /*插入结点*/ /*参数:链表的第一个结点,插入的位置*/ void ds_insert(node **pNode , int i) { node *temp; node *target; node *p; int item; int j = 1; printf("输入要插入结点的值:"); scanf("%d", &item); if(i == 1) { //新插入的结点作为第一个结点 temp = (node *)malloc(sizeof(struct CLinkList)); if(!temp) exit(0); temp ->data = item; /*寻找到最后一个结点*/ for(target = (*pNode); target->next != (*pNode); target = target->next) ; temp->next = (*pNode); target->next = temp; *pNode = temp; } else { target = *pNode; for( ; j < (i-1); ++j ) { target=target->next; } temp = (node *)malloc(sizeof(struct CLinkList)); if(!temp) exit(0); temp ->data = item; p = target->next; target->next = temp; temp->next = p; } } /*删除结点*/ void ds_delete(node **pNode, int i) { node *target; node *temp; int j = 1; if(i == 1) { //删除的是第一个结点 /*找到最后一个结点*/ for(target = *pNode; target->next != *pNode;target = target->next) ; temp = *pNode; *pNode = (*pNode)->next; target->next = *pNode; free(temp); } else { target = *pNode; for( ; j < i-1; ++j ) { target = target->next; } temp = target->next; target->next = temp->next; free(temp); } } /*返回结点所在位置*/ int ds_search(node *pNode, int elem) { node *target; int i = 1; for(target = pNode; target->data != elem && target->next != pNode; ++i) { target = target->next; } if(target->next == pNode) /*表中不存在该元素*/ return 0; else return i; } /*遍历*/ void ds_traverse(node *pNode) { node *temp; temp = pNode; printf("***********链表中的元素******************\n"); do { printf("%4d ", temp->data); }while((temp = temp->next) != pNode); printf("\n"); } int main() { node *pHead = NULL; char opp; int find; printf("-------------------------------------------------------\n\n"); printf("1.初始化链表 \n\n2.插入结点 \n\n3.删除结点 \n\n4.返回结点位置 \n\n5.遍历链表 \n\n0.退出 \n\n请选择你的操作:"); while(opp != '0') { scanf("%c", &opp); switch(opp) { case '1': ds_init(&pHead); printf("\n"); ds_traverse(pHead); break; case '2': printf("输入需要插入结点的位置?"); scanf("%d", &find); ds_insert(&pHead, find); printf("在位置%d插入值后:\n", find); ds_traverse(pHead); printf("\n"); break; case '3': printf("输入需要删除的结点位置?"); scanf("%d", &find); ds_delete(&pHead, find); printf("删除第%d个结点后:\n", find); ds_traverse(pHead); printf("\n"); break; case '4': printf("你要查找倒数第几个结点的值?"); scanf("%d", &find); printf("元素%d所在位置:%d\n", find, ds_search(pHead, find)); //ListTraverse(L); printf("\n"); break; case '5': ds_traverse(pHead); printf("\n"); break; case '0': exit(0); } } return 0; }
相关文章推荐
- 循环链表的初始化、插入、删除、返回结点位置、遍历
- 双向链表的功能实现:初始化,插入,按位置插入,按位置删除,按值删除,遍历打印等
- 输入一组整型元素序列,使用尾插法建立一个带有头结点的单链表。 ② 实现该线性表的遍历。 ③ 在该单链表的第i个元素前插入一个整数。 ④ 删除该单链表中的第i个元素,其值通过参数将其返回。 ⑤ 建立两个
- 链表的基本操作(创建,查找指定位置元素,删除指定元素,插入,倒置,去重,求集合的差,分别交换结点与交换结点值实现的冒泡排序,将两个有序链表合并成一个有序链表)c语言实现
- 单循环链表的初始化、插入、删除、遍历、查找
- 链表的初始化、遍历、删除、插入、判断是否为空、求长度 C语言实现
- 单向循环链表创建、遍历、插入、删除、查找(按位置,按元素值)、清空、销毁
- (2)单链表的操作 ① 输入一组整型元素序列,使用尾插法建立一个带有头结点的单链表。 ② 实现该线性表的遍历。 ③ 在该单链表的第i个元素前插入一个整数。 ④ 删除该单链表中的第i个元素,其值通过参数
- 用结构体实现链表的创建、遍历、结点插入、结点删除
- 用结构体实现链表的创建、遍历、结点插入、结点删除、链表删除-----带菜单选项
- 单向链表的创建、遍历、求长、判存、判空、插入、删除、查找(按位置或元素)、合并
- 单链表的使用(插入,查找,删除,链表的倒置,删除相同结点)
- C语言中,链表的创建,插入,删除,遍历,求链表长度,排序等
- 数据结构链表创建,遍历,是否为空,求长度,插入,删除算法的演示
- 单链表中头结点的有无. 并讨论下有无头结点在单链表的创建,打印,插入,逆置,删除中的区别.
- list(链表)常用成员(顺序容器)----插入push_back,push_front,insert删除pop_back,pop_front,erase,clear遍历begin,end判空em
- Java-集合 list对象进行指定位置插入、删除、迭代、遍历输出(具体习题讲解)
- C/C++,数据结构单链表(采用C++"引用"方法)(寻找节点、在某处插入结点、删除某位置结点)
- 数组实现的单链表的初始化 插入 删除
- 双向链表的创建、结点的插入、删除与打印