线性表的动态存储及插入、删除、查找、扩大操作;

时间:2021-07-11
本文章向大家介绍线性表的动态存储及插入、删除、查找、扩大操作;,主要包括线性表的动态存储及插入、删除、查找、扩大操作;使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

有关数据结构的学习笔记,懒得整理了,直接挂上了。。。

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<iostream>
 4 #define InitSize 10
 5 using namespace std;
 6 typedef struct {//Linear_list data formation
 7  int* data;
 8  int MaxSize;
 9  int length;
10 }Sqlist;
11 void InitList(Sqlist& L)//InitList
12 {
13  L.data =/* (int*)malloc(InitSize * sizeof(int));*/new int[InitSize];
14  L.length = 0;
15  L.MaxSize = InitSize;
16  int i = 0;
17 }
18 void IncreaseSize(Sqlist& L,int Addlen)//Addlength
19 {
20  int* P = L.data;
21  int i = 0;
22  L.data = new int[InitSize + Addlen];
23  while (i < L.length)
24  {
25   L.data[i++] = P[i];
26  }
27  L.MaxSize += Addlen;
28  delete[]P;
29 }
30 bool ListDelete(Sqlist& L, int i, int& e)//Delete Elem
31 {
32  if (i<1 || i>L.length)
33   return false;
34  e = L.data[i - 1];
35  for (int j = i; j < L.length; j++)
36   L.data[j - 1] = L.data[j];
37  L.length--;//Don't forget!!
38  return true;
39 }
40 bool ListInsert(Sqlist& L, int i, int e)//Insert Elem
41 {
42  if (i<1 || i>L.length + 1)
43   return false;
44  if (L.length >= L.MaxSize)
45   return false;
46  for (int j = L.length; j >= i; j--)
47   L.data[j] = L.data[j - 1];
48  L.data[i - 1] = e;//Don't forget!!
49  L.length++;
50  return true;
51 }
52 void Getelem(Sqlist& L, int location)
53 {
54  for (int i = 0; i < L.length; i++)
55  {
56   if (location == i + 1)
57   {
58    cout << L.data[i] <<' '<< location;
59    break;
60   }
61   if (i ==L.length)
62    cout << "num doesn't exist" << endl;
63  }
64 }
65 int main()
66 {
67  
68  Sqlist L1;
69  int e = -1;//return delete elem;
70  InitList(L1);
71  for (int j = 0; j < 5; j++)
72  {
73   L1.data[j] = j;
74   L1.length++;
75  }
76  //
77  //ListDelete(L1, 2, e);
78  //IncreaseSize(L1, 5);
79  for (int j = 0; j < L1.length; j++)
80  {
81   cout << L1.data[j] << endl;
82  }
83  //cout << e << ' ' << L1.length;
84  //cout << "Elem who is Deserted is " << L1.data[1]<<endl;
85  Getelem(L1, 2);
86  return 0;
87 }

原文地址:https://www.cnblogs.com/Mexcellent/p/14999922.html