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

线性表

时间:2017-07-27 10:39:16      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:cat   logs   逻辑   out   cout   scanf   seq   容量   i++   

结点逻辑关系为线性的结构——线性表

现实生活中的线性关系:

①排队中的一一对应关系

②(Caesar Code)字母表的表示方法

③电话号码表的结构(姓名、电话、身份证号、地址)

线性表的定义:一个线性表是nn>=0)个同类型结点的有限序列。

线性表的逻辑结构:

 

 

线性表的存储结构:

顺序表:将线性表中的元素依次放在一个连续的存储空间中,这样的线性表叫顺序表。

方法:采用一维数组来存储线性表的各个结点,结点通过物理地址即存储相邻

体现了逻辑关系的相邻。

定义:用数组存储线性表,称作线性表的顺序存储结构或顺序映像,用这种方法

存储的线性表称作顺序表。

地址计算:LOCai)=LOC(a1)+(i-1)*L

顺序表的结构类型描述:

Typedef int Elemtype //简化一些比较复杂的类型声明,也可提高程序的可移植性

#define LIST_SIZE 1024

Typedef struct

{
Elemtype data[LIST_SIZE];

Int last;

}SequenList;

 

SequenList LPtr; //定义结构变量,分配空间大小为sizeof(SequenList)

SequenList *LPtr; //定义结构指针,分配空间大小为sizeof(int)

 

顺序表的运算:

技术分享
  1 #include<iostream>
  2 #define FALSE 0
  3 #define TRUE  1
  4 using namespace std;
  5 
  6 typedef int ElemType;
  7 #define LIST_SIZE 1024
  8 typedef struct 
  9 {
 10     ElemType data[LIST_SIZE];
 11     int last;
 12 }SequenList;
 13 
 14 SequenList *LPtr;
 15 
 16 int Insert_SqList(SequenList *LPtr, ElemType x, int k)
 17 {
 18     int j;
 19     if (LPtr->last >= LIST_SIZE - 1)
 20         return FALSE;
 21     else if (k<0 || k>(LPtr->last + 1))
 22         return FALSE;
 23     else
 24     {
 25         for (j = LPtr->last;j >= k;j--)
 26         {
 27             LPtr->data[j + 1] = LPtr->data[j];
 28         }
 29         LPtr->data[k] = x;
 30         LPtr->last = LPtr->last + 1;
 31     }
 32     return TRUE;
 33 }
 34 
 35 int main()
 36 {
 37     for (int j = 0;j < 10;++j)
 38     {
 39         cin >> LPtr->data[j];
 40     }
 41     LPtr->last = 10;
 42     Insert_SqList(LPtr,100,5);
 43     for (int k = 0;k < 11;++k)
 44     {
 45         cout << LPtr->data[k];
 46     }
 47     return 0;
 48 }
 49 
 50 
 51 
 52 //#include<stdio.h>
 53 //#include<stdlib.h>
 54 //#include<windows.h>
 55 //#define Max 100
 56 //
 57 //typedef struct
 58 //{
 59 //    int data[Max];
 60 //    int length;
 61 //}SqList;
 62 //
 63 //void CreatSqList(SqList &L, int n)
 64 //{
 65 //    int i;
 66 //    printf("please input %d datas\n", n);
 67 //    for (i = 0;i<n;i++)
 68 //    {
 69 //        scanf_s("%d", &L.data[i]);
 70 //    }
 71 //    L.length = n;
 72 //}//创建
 73 //
 74 //void PrintSqList(SqList L)
 75 //{
 76 //    int i;
 77 //    printf("output the SqList:\n");
 78 //    for (i = 0;i<L.length;i++)
 79 //    {
 80 //        printf("%d ", L.data[i]);
 81 //    }
 82 //    printf("\n");
 83 //}//输出
 84 //
 85 //void ListInsert_Sq(SqList &L, int i, int e)
 86 //{
 87 //    int j;
 88 //    int n = L.length;
 89 //    if (n >= Max)
 90 //        printf("The SqList has full!\n");
 91 //    else if ((i<1) || (i>n + 1))
 92 //        printf("The data is error!\n");
 93 //    else
 94 //    {
 95 //        for (j = n - 1;j >= i - 1;j--)
 96 //            L.data[j + 1] = L.data[j];
 97 //        L.data[i - 1] = e;
 98 //        L.length++;
 99 //    }
100 //}//插入
101 //
102 //void DelSqList(SqList &L, int i)
103 //{
104 //    int j = i - 1;
105 //    if (i>L.length || i<1)
106 //        printf("The data is error!\n");
107 //    for (j;j<L.length;j++)
108 //        L.data[j] = L.data[j + 1];
109 //    L.length--;
110 //}//删除
111 //
112 //void Inverse_one(SqList &L)
113 //{
114 //    int i = 0, j, k;
115 //    for (j = L.length - 1;i <= j;i++, j--)
116 //    {
117 //        k = L.data[i];
118 //        L.data[i] = L.data[j];
119 //        L.data[j] = k;
120 //    }
121 //}//逆置①
122 //
123 //void Inverse_two(SqList &L)
124 //{
125 //    int i;
126 //    int x;
127 //    for (i = 0;i<L.length / 2;i++)
128 //    {
129 //        x = L.data[i];
130 //        L.data[i] = L.data[L.length - i - 1];
131 //        L.data[L.length - 1 - i] = x;
132 //    }
133 //}//逆置②
134 //
135 //int main()
136 //{
137 //    int n;
138 //    SqList L;
139 //    L.length = 0;
140 //    printf("please input the length of SqList!\n");
141 //    scanf_s("%d", &n);
142 //    CreatSqList(L, n);
143 //
144 //    Inverse_one(L);
145 //    Inverse_two(L);
146 //    /*printf("Please enter to insert the location of the data and data:\n");
147 //    scanf("%d %d",&i,&e);
148 //    ListInsert_Sq(L,i,e);
149 //
150 //    printf("Please input you want to delete the location of data:\n");
151 //    scanf("%d",&k);
152 //    DelSqList(L,k);*/
153 //
154 //    PrintSqList(L);
155 //
156 //    system("pause");
157 //    return 0;
158 //}
View Code

 

顺序表存储结构

特点:逻辑上相邻的数据元素物理存储位置也相邻,并且顺序表的存储空间需要预先分配。

优点:数组容易实现;存储结构使用紧凑;按元素序号随机访问的特点;

缺点:插入、删除操作时,平均移动表一半元素,n较大的顺序表效率低;

      预先分配空间大小,过大顺序表后部大量闲置,过小造成溢出;

  表容量难以扩存。

 

线性表

标签:cat   logs   逻辑   out   cout   scanf   seq   容量   i++   

原文地址:http://www.cnblogs.com/hansichen/p/7243194.html

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