【数据结构】链式存储单链表
来源:互联网 发布:Java 线程池 newsingle 编辑:程序博客网 时间:2024/05/29 08:40
数据结构之单链表的链式存储实现
//====================================================================== // // Copyright (C) 2014-2015 SCOTT // All rights reserved // // filename: List.c// description: a demo to display SeqList// // created by SCOTT at 01/28/2015 // http://blog.csdn.net/scottly1// //====================================================================== #include <stdio.h>#include <string.h>#include <malloc.h>#define TRUE1#define FALSE1typedef int Status;typedef int ElementType;typedef struct _tag_Node{ElementType data;struct _tag_Node *next;}NODE, *PNODE;PNODE initList(){int i, j = 0;int nLen;PNODE pHead = NULL;PNODE pNode = NULL;printf("Input The Length:");scanf("%d", &nLen);pHead = (PNODE)malloc(sizeof(NODE));pHead->data = nLen; // 头结点数据域可用于存储如链表长度等信息。pHead->next = NULL;for(i=0; i<nLen; ++i, ++j){pNode = (PNODE)malloc(sizeof(NODE));printf("Input The %d val:", nLen-j);scanf("%d", &(pNode->data));pNode->next = pHead->next; // 注意链表的生成技巧!pHead->next = pNode;}return pHead;}Status traverseList(PNODE list){int i = 1;PNODE p = list->next;ElementType data;while(p){data = p->data;printf("The %d Data is:%d\n", i++, data);p = p->next;}printf("The Length of List is %d\n\n", list->data);return TRUE;}Status deleteNode(PNODE list, int pos, PNODE ret){int i = 0;PNODE pHead = list;PNODE pTmp = NULL;if(!pHead || pos <=0 || pos >pHead->data){return FALSE;}while(pHead->next && i<pos-1){pHead = pHead->next;i++;}if(!pHead || i>pos-1){return FALSE;}pTmp = pHead->next;pHead->next = pTmp->next;free(pTmp);--list->data;return TRUE;}Status insertNode(PNODE list, int pos, PNODE node){int i = 0;PNODE pHead = list;if(!pHead || pos <=0) //pos >pHead->data{return FALSE;}while(pHead->next && i<pos-1){pHead = pHead->next;++i;}node->next = pHead->next;pHead->next = node;++list->data;return TRUE;}int main(){PNODE list;PNODE ret;PNODE node;list = initList();traverseList(list);// 删除测试deleteNode(list, 2, ret);traverseList(list);// 插入测试node = (PNODE)malloc(sizeof(NODE));node->data = 100;insertNode(list, 0, node); // not successinsertNode(list, 1000, node); // insert in lasttraverseList(list);return 0;}
注:原创文章,转载请注明出处:http://blog.csdn.net/scottly1/article/details/43247465
1 0
- 【数据结构】链式存储单链表
- 数据结构 链式存储
- 数据结构 链式存储源码
- 数据结构 链式存储结构
- 数据结构---栈---链式存储
- 数据结构---队列---链式存储
- 数据结构--链式存储结构
- 数据结构《链式栈的存储》
- 数据结构的链式存储结构
- 数据结构 - 栈的链式存储
- 数据结构的链式存储结构
- 数据结构-链式存储-基本运算
- 数据结构—链表-链式存储
- 数据结构-栈的链式存储
- 【数据结构基础】线性表的链式存储结构--单链表
- 【数据结构复习】线性表的链式存储--单链表
- 数据结构:线性表的链式存储结构_单链表
- 【数据结构】线性表的链式存储结构--单链表
- 基于链表的四位存储大数计算
- 嵌入式学习方法及概要
- 简易解说拉格朗日对偶(Lagrange duality)
- 让数据库变快的10个建议
- opencv背景建模mog2源码剖析
- 【数据结构】链式存储单链表
- 解决thinkphp display输出问题
- Perl Pack写的一个数据报表程序
- struts2中改变struts.xml默认路径
- android系统之sensor学习
- 多线程的几种实现方法详解
- 06-1. 简单计算器(20)
- iOS之UIView常用方法总结
- 结构知识点总结 & Demo