码迷,mamicode.com
首页 > 其他好文 > 详细

【c】线性表

时间:2015-03-06 14:09:09      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

数据对象集:线性表是N(>=0)个元素构成的有序序列,a1,a2,a3.....a(N-1),aN,a(N+1)

 

 线性表上的基本操作有:


⑴ 线性表初始化:Init_List(L)
初始条件:表L不存在操作结果:构造一个空的线性表

⑵ 求线性表的长度:Length_List(L)
初始条件:表L存在
操作结果:返回线性表中的所含元素的个数

⑶ 取表元:Get_List(L,i)
初始条件:表L存在且1<=i<=Length_List(L)
操作结果:返回线性表L中的第i个元素的值或地址

⑷ 按值查找:Locate_List(L,x),x是给定的一个数据元素。
初始条件:线性表L存在
操作结果:在表L中查找值为x的数据元素,其结果返回在L中首次出现的值为x的那个元素的序号或地址,称为查找成功; 否则,在L中未找到值为x的数据元素,返回一特殊值表示查找失败。

⑸ 插入操作:Insert_List(L,i,x)
初始条件:线性表L存在,插入位置正确(1<=i<=n+1,n为插入前的表长)。
操作结果:在线性表L的第i 个位置上插入一个值为x 的新元素,这样使原序号为i , i+1, ... , n 的数据元素的序号变为i+1,i+2, ... , n+1,插入后表长=原表长+1。

⑹ 删除操作:Delete_List(L,i)
初始条件:线性表L存在,1<=i<=n。
操作结果:在线性表L中删除序号为i的数据元素,删除后使序号为i+1, i+2,..., n的元素变为序号为i, i+1,...,n-1,新表长=原表长-1。

 

线性表的存储:顺序存储

 1 #include<stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 
 5 #define max 10
 6 typedef struct{
 7     int data[max];  //数组 
 8     int last;        //最后一个数据在数组中的位置 
 9 }LIST;
10 
11 //初始化 
12 LIST * makeEmpty();
13 //查找,返回数据在数组中的位置,不存在返回-1
14 int find(int,LIST *); 
15 //插入,成功1,失败-1
16 int insert(int site,int num,LIST * Ptrl); 
17 //打印数据结构
18 void dump(LIST *); 
19 //删除
20 int del(int site,LIST * Ptrl); 
21 
22 void main(void)
23 {
24     int num;
25     LIST * demo;
26     demo = makeEmpty();
27     
28     
29 }
30 
31 //初始化 
32 LIST * makeEmpty()
33 {
34     LIST * Ptrl;
35     Ptrl = (LIST*)malloc(sizeof(LIST));
36     Ptrl->last = -1;    //为空时last为1 
37     return Ptrl; 
38 }
39 //查找,返回数据在数组中的位置,不存在返回-1
40 int find(int num,LIST * Ptrl)
41 {
42     int i=0;
43     while(i<=Ptrl->last && Ptrl->data[i]!=num)
44     {
45         i++;
46     }
47     //如果i大于数据的长度,则就是没有找到 
48     if(i > Ptrl->last)
49         return -1;
50     //返回数据的位置 
51     return i; 
52 }
53 //插入,成功1,失败-1
54 int insert(int site,int num,LIST * Ptrl)
55 {
56     //1、判断是否已经满了,最后的位置=长度-1
57     if(Ptrl->last == max-1){
58         return -1;
59     } 
60     //2、判断要插入的位置是否大于等于长度 site >= max
61     if(site<0 || Ptrl->last >= max){
62         return -1;
63     } 
64     //3、从最后一个数据开始移动,下一位填充上一位的数据 ,data[n+1] = data[n],data[n]=data[n-1],直到n>=site 
65     int i;
66     for(i=Ptrl->last;i>=site;i--)
67     {
68         Ptrl->data[i+1] = Ptrl->data[i];
69     }
70     Ptrl->data[site] = num;
71     Ptrl->last += 1;
72     return 1; 
73 } 
74 
75 //打印数据结构
76 void dump(LIST * Ptrl){
77     int i=0;
78     while(i<=Ptrl->last)
79     {
80         printf("%d=>%d---%d\n",i,Ptrl->data[i],Ptrl->last);
81         i++;
82     }
83 }
84 //删除
85 int del(int site,LIST * Ptrl)
86 {
87     //检测删除的位置是否存在
88     if(site > Ptrl->last || site<0){
89         return -1;
90     } 
91     
92     int i;
93     for(i=site;i<=Ptrl->last;i++)
94     {
95         Ptrl->data[i] = Ptrl->data[i+1];
96     } 
97     Ptrl->last--;
98     return 1;
99 } 

 

线性表的存储:链式存储

 

【c】线性表

标签:

原文地址:http://www.cnblogs.com/hanyouchun/p/4318027.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!