c语言-链表实现
2014-02-14 17:36
447 查看
#include <stdio.h> #include <string.h> #include <stdlib.h> void initList(struct Node **pHead); void creatList(struct Node **pHead); void insertHeadList(struct Node **pHead); void insertTailList(struct Node **pHead); void searchList(struct Node **pHead); void deleteList(struct Node **pHead); void printList(struct Node *pHead); void sortinsertList(struct Node **pHead); typedef struct Node{ int data; struct Node *next; }Node; int main() { struct Node *lxm; int id; initList(&lxm); while(1) { printf("请输入指令:\n"); printf("0、创建;1、头插入;2、尾插入;3、删除;\n"); printf("4、查询;5、排序插入;6、打印\n"); scanf("%d",&id); system("cls"); switch(id) { case 0:creatList(&lxm);break; case 1:insertHeadList(&lxm);break; case 2:insertTailList(&lxm);break; case 3:deleteList(&lxm);break; case 4:searchList(&lxm);break; case 5:sortinsertList(&lxm);break; case 6:printList(lxm); default:break; } } return 0; } void initList(struct Node **pHead) { *pHead = NULL; printf("初始化链表头成功\n"); } //创建链表 void creatList(struct Node **pHead) { struct Node *p = NULL; struct Node *q = NULL; p = q = (struct Node *)malloc(sizeof(struct Node)); if(p == NULL || q == NULL) { printf("malloc failed\n"); exit(0); } memset(p,0,sizeof(struct Node)); printf("链表创建初始化值\n"); scanf("%d",&p->data); p->next = NULL; while(p->data > 0) { if(*pHead == NULL) { *pHead = p; } else { q->next = p; q = p; } p = (struct Node *)malloc(sizeof(struct Node)); if(p == NULL) { printf("malloc failed\n"); exit(0); } memset(p,0,sizeof(struct Node)); scanf("%d",&p->data); p->next = NULL; } printf("链表创建成功\n"); } void sortinsertList(struct Node **pHead) { struct Node *p = *pHead; struct Node *h = *pHead; struct Node *temp = NULL; int num = 0; temp = (struct Node *)malloc(sizeof(struct Node)); if(temp == NULL) { printf("malloc failed\n"); exit(0); } memset(temp,0,sizeof(struct Node)); printf("请插入值\n"); scanf("%d",&temp->data); while(p->next != NULL) { if(h->data > temp->data) { temp->next = h; *pHead = temp; } else if(temp->data > p->data && temp->data < p->next->data) { temp->next = p->next; p->next = temp; } p = p->next; } if(p->next == NULL) { temp->next = NULL; p->next = temp; } } void searchList(struct Node **pHead) { struct Node *p = *pHead; int i = 1; int num = 0; printf("请输入需要查询的数值\n"); scanf("%d",&num); while(p->data != num && p->next != NULL) { i++; p = p->next; } printf("该数值在链表中的位置是: %d \n",i); } void insertHeadList(struct Node **pHead) { struct Node *p; p = (struct Node *)malloc(sizeof(struct Node)); memset(p,0,sizeof(struct Node)); printf("请输入插入的数值\n"); scanf("%d",&p->data); p->next = *pHead; *pHead = p; printf("向表头插入元素成功\n"); } void insertTailList(struct Node **pHead) { struct Node *p = NULL; struct Node *temp = *pHead; p = (struct Node *)malloc(sizeof(struct Node)); memset(p,0,sizeof(struct Node)); printf("请输入插入的数值\n"); scanf("%d",&p->data); while(temp->next != NULL) { temp = temp->next; } temp->next = p; printf("向表头插入元素成功\n"); } void deleteList(struct Node **pHead) { int num = 0; struct Node *h = *pHead; struct Node *p = *pHead; struct Node *n = h->next; printf("请输入需要删除的数值\n"); scanf("%d",&num); while(p->next != NULL) { if(num == h->data) { struct Node *temp1 = h; p = h = h->next; n = n->next; *pHead = h; free(temp1); break; } else if(num == n->data && num != h->data) { struct Node *temp2 = n; n = n->next; p->next = n; free(temp2); break; } p = p->next; n = p->next; } printf("删除成功\n"); } void printList(struct Node *pHead) { struct Node *p = pHead; if(NULL == p) //链表为空 { printf("链表为空\n"); } else { printf("链表元素为: "); while(NULL != p) { printf("%d ",p->data); p = p->next; } printf("\n"); } }
相关文章推荐
- c语言实现单链表&二级指针在单链表的应用
- 【回忆c语言】从指针的定义初始化到链表的插入删除排序实现code
- 单链表 C语言实现
- 没有头结点的单链表[c语言实现]
- 单向链表装载用户自定义数据的c语言实现 linklistpackagenode.h 和 linklistpackagenode.c
- 单链表的基本操作C语言实现
- 多项式加法利用链表实现(c语言)
- C语言实现链表的基本操作
- 链表的C语言实现(一)
- 双链表的C语言实现
- C语言链表实现的简易学生成绩管理系统
- c语言单链表创建(函数传地址实现)
- C语言实现单链表(不带头结点)节点的插入
- C语言单链表实现19个功能完全详解
- C语言实现贪吃蛇(三)----结构+链表实现
- 【小镇的技术天梯】从头写数据结构,C语言实现双向链表
- c语言实现循环链表的基本操作
- C语言实现单链表之具体实现
- C语言数据结构之单向链表(已经调试可以实现相应的功能了,可是还是有几个问题现在还是不大理解,希望大家能够一起探讨)
- 单循环链表各种操作的C语言实现