数据结构与算法(5)--线性表

来源:互联网 发布:sql教学视频 编辑:程序博客网 时间:2024/05/21 06:18

线性表是最基本、最简单、也是最常用的一种数据结构。线性表中数据元素之间的关系是一对一的关系,即除了第一个和最后一个数据元素之外,其它数据元素都是首尾相接的。线性表的逻辑结构简单,便于实现和操作。因此,线性表这种数据结构在实际应用中是广泛采用的一种数据结构。

#include<stdio.h>#define MaxSize 50typedef char ElemType;struct List{ ElemType list[MaxSize]; int size;}setnull(struct List *p)//置空{ p->size=0;}int length(struct List *p)//求长度{ return (p->size);}ElemType get(struct List *p,int i)//取第i歌节点{ if(i<1&&i>p->size)  printf("位置参数error!/n"); else  return(p->list[i-1]);}int locate(struct List *p,ElemType x)//定位{ int i=0; while(i<p->size&&p->list[i]!=x) i++; if(i==p->size)  return(-1); else  return(i+1);}void insert(struct List *p,ElemType x,int i)//插入结点{ int j; if(i<1||i>p->size+1)  printf("位置参数error!/n"); else {  p->size++;  for(j=p->size-1;j>=i;j--)   p->list[j]=p->list[j-1];  p->list[j]=x; }}void del(struct List *p,int i){ int j; if(i<1&&i>p->size+1)  printf("位置参数error!/n"); else {  for(j=i-1;j<p->size-1;j++)//结点前移   p->list[j]=p->list[j+1];  p->size--; }}display(struct List *p){ int j; if(p->size==0)  printf("Empty rable!/n"); else {  printf("顺序表:/n");  if(p->size==1)   printf("%c",p->list[p->size]);  else  {   for(j=0;j<p->size-1;j++)    printf("%c->",p->list[j]);   printf("%c",p->list[j]);  }  printf("/n"); }}main(){ struct List L; setnull(&L); insert(&L,'a',1); insert(&L,'s',1); insert(&L,'f',2); insert(&L,'d',1); insert(&L,'z',3); insert(&L,'c',2); display(&L); printf("值%c位置:%5d/n:",'a',locate(&L,'a')); printf("delete the second element~/n"); del(&L,2); display(&L); printf("delete the first element~/n"); del(&L,1); display(&L);}

—————————————————————————————————

本文原创自Sliencecsdn技术博客。

本博客所有原创文章请以链接形式注明出处。

欢迎关注本技术博客,本博客的文章会不定期更新。


大多数人想要改造这个世界,但却罕有人想改造自己。

世上没有绝望的处境,只有对处境绝望的人。

                                              ————By slience

—————————————————————————————————



0 0
原创粉丝点击