单链表的建立
来源:互联网 发布:m1136mfp扫描软件 编辑:程序博客网 时间:2024/05/16 19:43
#include <stdio.h>#include <stdlib.h>struct node{ int data; struct node *next;};typedef struct node LNode, *LinkList;void CreateList(LinkList head);void PrintList(LinkList head);void ListDelete(LinkList head, int i, int *e);void ListInsert(LinkList head, int i, int e);int main(){ LinkList head; int i, e, select; head = (LinkList) malloc (sizeof(LNode)); head -> next = NULL; while(1) { printf("1: Create a list.\n"); printf("2: Insert some elements in the list.\n"); printf("3: Delete some elements.\n"); printf("4: Print the list.\n"); printf("0: Quit!\n"); scanf("%d", &select); switch(select) { case 1: CreateList(head); break; case 2: printf("Input position and name of the element.\n"); scanf("%d%d", &i, &e); ListInsert(head, i, e); break; case 3: printf("Input the position of the element.\n"); scanf("%d", &i); ListDelete(head, i, &e); case 4: PrintList(head); printf("\n"); break; case 0: exit(0); } } return 0;}//void CreateList()//头插法//{// LinkList p;// int num;//// while(scanf("%d", &num), num!=-1)// {// p = (LinkList)malloc(sizeof(LNode));// p ->data = num;// p ->next = head ->next;// head ->next = p;// }//}void CreateList(LinkList head)//尾插法{ LinkList p, rear = head; int num; while(scanf("%d", &num), num!=-1) { p = (LinkList)malloc(sizeof(LNode)); p ->data = num; rear ->next = p; rear = p; } rear ->next = NULL;}void PrintList(LinkList head){ LinkList p = head ->next; while(p) { printf("%d\n", p ->data); p = p ->next; }}void ListInsert(LinkList head, int i, int e)//变相的头插法{ LinkList p, pre = head; int j = 0; while(pre && j<i+1) { pre = pre ->next; j++; } if(!pre || i<1) { printf("Input i error\n"); return; } p = (LinkList)malloc(sizeof(LNode)); p ->data = e; p ->next = pre->next; pre ->next = p;}void ListDelete(LinkList head, int i, int *e){ LinkList p, pre; int j=0; while(pre && j<i-1) { pre = pre ->next; j++; } if(!pre || i<1) { printf("Input i error\n"); return; } p = pre ->next; pre ->next = p ->next; *e = p->data; free(p);}
0 0
- 单链表的建立
- 单链表的建立
- 单链表的建立
- 单链表的建立
- 循环单链表的建立
- 单链表的建立程序
- 单链表的建立
- 单链表的建立
- 建立排序的单链表
- 单链表的建立
- 有序单链表的建立
- 单链表的建立
- 单链表的建立
- 单链表的建立
- 单链表的建立
- 单链表的建立
- 单链表的建立
- 单链表(单链表的建立)
- 修路方案 【最小生成树变形】-- 次小生成树
- 浅谈TCP协议的端口(port)
- Essential C++学习笔记 第一章 C++编程基础
- jQuery系列2---DOM操作和事件
- NULL 值与索引
- 单链表的建立
- 给你一个网站你是如何来渗透测试的?
- Java AIO-异步通信
- 所有控件实现下拉刷新上拉加载功能
- PHP->函数
- Lock和synchronized比较详解
- 【java 构造函数】为什么子类一定要访问父类中的构造函数
- Elasticsearch中setting,mapping,分片查询方式
- TextView属性android:ellipsize="marquee"不生效的解决办法