数据结构 链表|简单链表
来源:互联网 发布:交大医学院网络教育 编辑:程序博客网 时间:2024/05/02 05:43
Description
链表是使用指针把一系列结点连接起来的数据结构。它由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。每个结点里的指针指向下一个结点,而链表的入口(head指针)指向第一个结点
现在请你实现一个简单的链表,具体请看头文件
Input
第一行是整数 n
接下来有n行,每行两个整数,代表插入的位置和插入的数据
Output
输入每次插入后的结果
Sample Input
30 11 2-1 3
Sample Output
11 2position is not valid
Hint
插入的position是以0计数的,比如当前是 1 2 3
- 如果输入0 4,则在0号位置插入4,变为 4 1 2 3
- 如果输入3 5,则在3号位置插入5,变为 1 2 3 5
- 如果输入不在 [0, size] 区间(size为当前个数),则位置不合法
main.c
#include <stdio.h>#include "linkedList.h"void print() { int i; for (i = 0; i < size; ++i) { printf("%d ", get(i)); } printf("\n");}int main() { head = NULL; size = 0; int n, i, position, value; scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d%d", &position, &value); if (insert(position, value)) { print(); } else { printf("position is not valid\n"); } } clear(); return 0;}
#ifndef LINKEDLIST_H#define LINKEDLIST_H#include <stdbool.h>typedef struct node { int value; struct node* next;} node ;int size; // the size of linked listnode* head; // the head of linkedlist//insert the value to the right position//if the position is not valid, return false//if insert successfully, return truebool insert(int position, int value);// return the value in the given positionint get(int position);//clear the linkedlist, remember to free the memory you allocatedvoid clear();#endif
linkedList.c
#include <stdio.h>#include <stdlib.h>#include <stdbool.h>typedef struct node { int value; struct node* next;} node ;int size; // the size of linked listnode* head; // the head of linkedlist//insert the value to the right position//if the position is not valid, return false//if insert successfully, return truebool insert(int position, int value);// return the value in the given positionint get(int position);//clear the linkedlist, remember to free the memory you allocatedvoid clear();bool insert(int position, int value){ if (position > size || position < 0) return 0; if (size == 0) { head = (node *)malloc(sizeof(node)); head -> value = value; head -> next = NULL; size ++; } else if (position == 0) { node * ph = (node*)malloc(sizeof(node)); ph -> value = value; ph -> next = head; head = ph; size ++; } else { node * p = head; node * q = NULL; int i = 0; for (i = 0; i < position - 1; ++ i) { p = p -> next; } node * ins = (node *)malloc(sizeof(node)); q = p -> next; p -> next = ins; ins -> next = q; ins -> value = value; size ++; } return 1;}int get(int position){ node * temp = head; while (position --) { temp = temp -> next; } return temp -> value;}void clear(){ node * pHead = head; node * pNext; if (pHead != NULL) { while (pHead != NULL) { pNext = pHead -> next; free(pHead); pHead = pNext; } }}
0 0
- 数据结构 链表|简单链表
- 数据结构代码-简单链表
- 经典数据结构--简单链表
- 简单数据结构--链表
- 链表数据结构的简单理解
- 数据结构:链表的简单操作
- 数据结构实训三简单的链表
- 数据结构:打印简单的链表
- 数据结构-简单的链表结构
- 数据结构链表的简单处理
- 数据结构之链表简单操作
- 数据结构之简单线性表
- (一)数据结构之线性表的简单实现:链表
- 数据结构-有序链表(基于简单链表)
- 数据结构-约瑟夫环的简单实现(循环链表)
- 队列模拟(简单链表数据结构的处理)
- 数据结构之简单链表(尾部插入数据)
- 数据结构之链表实现简单的信息系统
- SSH框架中怎么使用Hibernate查询一个对象
- JavaWeb宝典零碎笔记(Servlet&JSP)
- 程序员励志语录
- 内部类
- BFC 神奇背后的原理
- 数据结构 链表|简单链表
- C++第3次实验(提高班)-数组
- 深入浅出DNS系列(八)- subnet
- POJ2289-Jamie's Contact Groups(二分图多重匹配)
- Linux 平台搭建C语言集成开发环境(Ubantu16.04)
- C指针基础(三)
- 【求职之路】(1)利用二分法查找一个数字在数组中的位置
- 解读 SysNativeCompressFile
- FABS函数