无头结点链表实现线性表
来源:互联网 发布:大连知润和新芯智能化 编辑:程序博客网 时间:2024/09/21 08:56
[web@localhost d2]$ gcc --version
gcc (GCC) 4.4.4 20100726 (Red Hat 4.4.4-13)
gcc (GCC) 4.4.4 20100726 (Red Hat 4.4.4-13)
Copyright (C) 2010 Free Software Foundation, Inc.
#include <stdlib.h>#include <stdio.h>#include <stdbool.h>struct node{int value;struct node *next;};typedef struct node LNode;typedef struct node *SeqList;void SeqListInit(SeqList *L){*L=NULL;}int SeqListLength(SeqList L){int l;l=0;//SeqList *t;//t=&L;while(L!=NULL){l++;L=L->next;}//L=*t;return l;}int SeqListGet(SeqList L,int i){int l;l=SeqListLength(L);if(l==0 || i>l) return -1;int h;h=1;while(h!=i){L=L->next;h++;}return L->value;}int SeqListLocate(SeqList L,int i){int l;l=SeqListLength(L);if(l==0) return -1;int h=1;while(L!=NULL){if(L->value==i) return h;L=L->next;h++;}return -1;}int SeqListPrior(SeqList L,int i){int l;l=SeqListLocate(L,i);if(l>1){return SeqListGet(L,l-1);}else{return -1;}}int SeqListNrior(SeqList L,int i){int l,r;l=SeqListLocate(L,i);r=(l>1 && l<SeqListLength(L))?SeqListGet(L,l+1):-1;return r;}void SeqListInsert(SeqList *L,int i,int j){LNode *n;printf("in%X\n",*L);SeqList lt;lt=&(**L);printf("in%X\n",&(**L));printf("in%X\n",lt);printf("in%X\n",*L);n = (LNode*)malloc(sizeof(LNode));if(n==NULL) exit(0);n -> value = i;n -> next = NULL;if(*L==NULL){*L=n;}else{int l;l=SeqListLength(*L);if(j>l){printf("%d>%d\n",j,l);while((**L).next!=NULL){printf("1");*L=(**L).next;}(**L).next=n;*L=lt;}else if(j>0 && j<=l){printf("%d<=%d\n",j,l);int h=1;if(j==1){n->next=lt;*L=n;}else if(j>0 && j!=1){while(h<j-1){*L=(**L).next;h++;}n->next=(**L).next; (**L).next=n; *L=lt;}}}printf("in%X\n",lt);printf("out%X\n",*L);}void SeqListDel(SeqList *L,int i){int l=SeqListLength(*L);SeqList lt;lt=&(**L);if(i>0 && l>0 && i<=l){int h;if(i==1){LNode *t;t=lt;*L=(**L).next;free(t);}else{for(h=1;h<i;h++){*L=(**L).next;}LNode *t;t=(**L).next;(**L).next=t->next;*L=lt;free(t);}}}bool SeqListIsEmpty(SeqList L){bool r;r=(L==NULL)?true:false;return r;}void SeqEmpty(SeqList *L){*L=NULL;}void SeqListTraverse(SeqList L){while(L!=NULL){printf("addr[%X]value[%d]->",&L,L->value);L=L->next;}}void main(){SeqList L,h;printf("init:");SeqListInit(&L);printf("L\n");printf("length:%d\n",SeqListLength(L));printf("add iterm 1\n");SeqListInsert(&L,1,1);printf("length:%d\n",SeqListLength(L));SeqListTraverse(L);printf("\n");printf("add iterm 2\n");SeqListInsert(&L,2,2);printf("length:%d\n",SeqListLength(L));SeqListTraverse(L);printf("\n");printf("add iterm 3\n");SeqListInsert(&L,3,3);printf("length:%d\n",SeqListLength(L));SeqListTraverse(L);printf("\n");printf("add iterm 4\n");SeqListInsert(&L,4,4);printf("length:%d\n",SeqListLength(L));SeqListTraverse(L);SeqListInsert(&L,5,1);printf("\n");SeqListTraverse(L);printf("\n");SeqListInsert(&L,6,2);printf("\n");SeqListTraverse(L);printf("\n");SeqListInsert(&L,8,8);printf("\n");SeqListTraverse(L);printf("\n");printf("the last value of 3 is %d\n",SeqListPrior(L,3));printf("the last value of 5 is %d\n",SeqListPrior(L,5));printf("the next value of 2 is %d\n",SeqListNrior(L,2));printf("the next value of 4 is %d\n",SeqListNrior(L,4));printf("is L empty ?%d\n",SeqListIsEmpty(L));SeqListTraverse(L);printf("\n");SeqListDel(&L,1);printf("is L empty ?%d\n",SeqListIsEmpty(L));SeqListTraverse(L);printf("\n");SeqListDel(&L,1);printf("is L empty ?%d\n",SeqListIsEmpty(L));SeqListTraverse(L);printf("\n");SeqListDel(&L,1);printf("is L empty ?%d\n",SeqListIsEmpty(L));SeqListTraverse(L);printf("\n");SeqListDel(&L,1);printf("is L empty ?%d\n",SeqListIsEmpty(L));SeqListTraverse(L);printf("\n");SeqListDel(&L,1);printf("is L empty ?%d\n",SeqListIsEmpty(L));SeqListTraverse(L);printf("\n");SeqListDel(&L,1);printf("is L empty ?%d\n",SeqListIsEmpty(L));SeqListTraverse(L);printf("\n");SeqListDel(&L,1);printf("is L empty ?%d\n",SeqListIsEmpty(L));SeqListTraverse(L);printf("\n");}
- 无头结点链表实现线性表
- 数据结构试验二 线性表实现(无头结点)
- 在无头结点的动态单链表上实现线性表操作Delete(L,i)
- 在无头结点的动态单链表上实现线性表操作Insert(L,i,b)
- 线性表的链式存储实现(无头结点)(陈越数据结构)
- C++实现无头结点的双向链表
- 数据结构:头结点线性链表
- 线性链表中的头结点和头指针
- 数据结构-线性表-头指针&头结点
- 数据结构-线性表-头指针&头结点
- 线性表的链式存储实现(有头结点)
- 线性表的基本操作实现(基于链表,有头结点)
- 数据结构之—线性表之—浅谈单链表有头结点和无头节点
- 两种实现 无头结点单向链表 的插入、查找、删除
- C语言实现,无头结点不带环的单向链表的基本操作
- 无头结点链表的增、删、改、查
- 插入排序的链表,无头结点
- 数据结构-双向循环链表(无头结点)相关操作
- 设计模式之策略模式
- OCI学习笔记三
- SSH异常和日志处理方案
- JavaWEB开发中MVC的一点哲学思考
- Google的图相处理也有看走言的时候,(续),实际街景是这样的
- 无头结点链表实现线性表
- 一篇关于bootloader的好文章
- 为什么2.6.29内核支持的无线网卡的芯片找起来比找老公还难?
- SQLite学习笔记(一)
- 使用MyEclipse6.0 工具Create a new connection driver (SQL SERVER 2005)
- ucOS 时钟中断(ISR)
- 软件测试也是个不错的职业
- (总结)Swing组件的使用---下拉式菜单(menu),弹出式菜单(JPopupMenu),选项卡窗体(JTabbedPane)
- 停更一天