zoukankan      html  css  js  c++  java
  • 【c】线性表

    数据对象集:线性表是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
    ",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 } 

    线性表的存储:链式存储

       存放数据元素信息的称为数据域,存放其后继地址的称为指针域。因此n个元素的线性表通过每个结点的指针域拉成了一个“链子”,称之为链表。因为每个结点中只有一个指向后继的指针,所以称其为单链表。

      链表是由一个个结点构成的,结点定义如下:
      typedef struct node{

        datatype data;
        struct node *next;
      } LNode,*LinkList;

      定义头指针变量:
      LinkList H;

      

      1 #include<stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 
      5 #define max 10
      6 typedef struct list{
      7     int data;
      8     struct list * next;
      9 }LIST; 
     10 
     11 //初始化
     12 LIST * initialize();
     13 //按key查找
     14 LIST * findByKey(int key,LIST * Ptrl);
     15 //按value查找
     16 LIST * findByVal(int value,LIST * Ptrl);
     17 //插入 
     18 int insert(int key,int val,LIST * Ptrl); 
     19 //删除
     20 int delete(int key,LIST * Ptrl);
     21 //表长
     22 int length(LIST * Ptrl);
     23 //打印
     24 void dump(LIST * Ptrl);
     25 
     26  void main(void)
     27  {
     28      LIST * demo;
     29      demo = initialize();
     30      insert(1,22,demo);
     31      insert(2,222,demo);
     32      LIST * find = findByVal(22,demo);
     33     //demo = initialize();
     34     dump(demo);
     35      //printf("%d",length(demo));
     36  }
     37  
     38 //初始化
     39 LIST * initialize()
     40 {
     41     LIST * Ptrl;
     42     Ptrl = (LIST *)malloc(sizeof(LIST));
     43     if(Ptrl==NULL){
     44         puts("molloc fail in line 38
    ");
     45         exit;
     46     }
     47     Ptrl->next = NULL;
     48     Ptrl->data = 0; 
     49     return Ptrl;
     50 } 
     51 //按key查找
     52 LIST * findByKey(int key,LIST * Ptrl)
     53 {
     54     if(key==0)
     55     {
     56         return Ptrl;
     57     }
     58     
     59     LIST * p;
     60     p = Ptrl;
     61     int i=0;
     62     
     63     while(p!=NULL && i<key)
     64     {
     65         p = p->next;
     66         i++;
     67     }
     68     if(i==key){
     69         return p;
     70     }
     71     return NULL; 
     72 }
     73 //按value查找
     74 LIST * findByVal(int value,LIST * Ptrl)
     75 {
     76     LIST * p;
     77     p = Ptrl;
     78     while(p!=NULL && p->data!=value)
     79     {
     80         p=p->next;
     81     }        
     82     if(p != NULL){
     83         return p;
     84     }else{
     85         return NULL;
     86     }
     87 }
     88 
     89  //插入 
     90 int insert(int key,int val,LIST * Ptrl)
     91 {
     92     LIST * p,* find;
     93     
     94     p = (LIST *)malloc(sizeof(LIST));
     95     if(Ptrl==NULL){
     96         puts("molloc fail in line 86
    ");
     97         exit;
     98     }
     99     
    100 
    101     find = findByKey(key-1,Ptrl);
    102 
    103     p->data = val;
    104     p->next = find->next;
    105     find->next = p;
    106     
    107     return 1;
    108 }
    109 //删除
    110 int delete(int key,LIST * Ptrl)
    111 {
    112     //检查长度的合法性
    113     
    114     //删除
    115     LIST * p,* find;
    116     p = findByKey(key,Ptrl);
    117     find = findByKey(key-1,Ptrl);
    118     find->next = p->next;
    119     free(p);
    120 }
    121 //表长
    122 int length(LIST * Ptrl)
    123 {
    124     int i=0;
    125     LIST * p = Ptrl;
    126     while(p!=NULL)
    127     {
    128         p = p->next;
    129         i++;
    130     }
    131     return i;
    132 }
    133 //打印
    134 void dump(LIST * Ptrl)
    135 {
    136     LIST * p = Ptrl;
    137     while(p!=NULL)
    138     {
    139         printf("%d
    ",p->data);
    140         p = p->next;
    141     }
    142 }

    栈的存储

        顺序存储

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #define max 10
      5 
      6 typedef struct list{
      7     int data[max];
      8     int top;
      9 }LIST;
     10 
     11 //初始化
     12 LIST * initialize(); 
     13 //判断是否为空
     14 int isEmpty(LIST *);
     15 //判断是否满了
     16 int isFull(LIST *);
     17 //出栈 
     18 int pop(LIST *); 
     19 //入栈 
     20 int push(int value,LIST * Ptrl);
     21 //打印
     22 void dump(LIST *); 
     23 
     24 void main(void)
     25 {
     26     LIST * demo = initialize();
     27     push(3,demo);
     28     push(6,demo);
     29     push(9,demo);
     30     pop(demo);
     31     dump(demo);
     32 }
     33 
     34 //初始化
     35 LIST * initialize()
     36 {
     37     LIST * Ptrl;
     38     Ptrl = (LIST *)malloc(sizeof(LIST));
     39     if(Ptrl == NULL)
     40     {
     41         puts("malloc error!!!
    ");
     42         exit;
     43     }
     44     Ptrl->top=-1;
     45     return Ptrl;
     46 }
     47 //判断是否为空
     48 int isEmpty(LIST * Ptrl)
     49 {
     50     if(Ptrl->top == -1)
     51     {
     52         return 0;
     53     }
     54     return 1;
     55 }
     56 
     57 //判断是否满了
     58 int isFull(LIST * Ptrl)
     59 {
     60     if(Ptrl->top >= max-1)
     61     {
     62         return 0;
     63     }
     64     return 1;
     65 }
     66 //出栈 
     67 int pop(LIST * Ptrl)
     68 {
     69     if(isEmpty(Ptrl) == 0)
     70     {
     71         puts("is empty");
     72         exit;
     73     }
     74     
     75     int temp =  Ptrl->data[Ptrl->top];
     76     Ptrl->top--;
     77     return temp;
     78 }
     79 //入栈 
     80 int push(int value,LIST * Ptrl)
     81 {
     82     if(isFull(Ptrl) == 0)
     83     {
     84         puts("is full");
     85         exit;
     86     }
     87     Ptrl->top++;
     88     Ptrl->data[Ptrl->top] = value;
     89     return 1;
     90 } 
     91 //打印
     92 void dump(LIST * Ptrl)
     93 {
     94     if(isEmpty(Ptrl) == 0)
     95     {
     96         puts("is empty");
     97         exit;
     98     }
     99     
    100     int i;
    101     for(i=Ptrl->top;i>=0;i--)
    102     {
    103         printf("%d
    ",Ptrl->data[i]); 
    104     }
    105 }

     链式村粗

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 #define max 10
     5 
     6 typedef struct list{
     7     int data;
     8     struct list * next;
     9 }LIST;
    10 
    11 //初始化
    12 LIST * initialize(); 
    13 //判断是否为空
    14 int isEmpty(LIST *);
    15 //判断是否满了
    16 int isFull(LIST *);
    17 //出栈 
    18 int pop(LIST *); 
    19 //入栈 
    20 int push(int value,LIST * Ptrl);
    21 //打印
    22 void dump(LIST *); 
    23 
    24 void main(void)
    25 {
    26     LIST * demo = initialize();
    27     push(2,demo);
    28     push(5,demo);
    29     push(6,demo);
    30     push(8,demo);
    31     pop(demo);
    32     dump(demo);
    33 }
    34 
    35 
    36 //初始化
    37 LIST * initialize()
    38 {
    39     LIST * Ptrl;
    40     Ptrl = (LIST *)malloc(sizeof(LIST));
    41     Ptrl->next = NULL;
    42     //Ptrl->data = -1;
    43     return Ptrl;
    44 }
    45 //判断是否为空
    46 int isEmpty(LIST * Ptrl)
    47 {
    48     if(Ptrl->next == NULL)
    49     {
    50         return 0;
    51     }
    52     return 1;
    53 }
    54 
    55 //判断是否满了
    56 int isFull(LIST * Ptrl)
    57 {
    58     
    59 }
    60 //出栈 
    61 int pop(LIST * Ptrl)
    62 {
    63     if(isEmpty(Ptrl)==0){
    64         puts("is empty");
    65         exit;
    66     }
    67     LIST * temp = Ptrl->next;
    68     int data = temp->data;
    69     Ptrl->next = temp->next;
    70     free(temp);
    71     return data;
    72 }
    73 //入栈 
    74 int push(int value,LIST * Ptrl)
    75 {
    76     LIST * temp = (LIST *)malloc(sizeof(LIST));
    77     temp->next = Ptrl->next;
    78     temp->data = value;
    79     Ptrl->next = temp;
    80     return 1;
    81 } 
    82 //打印
    83 void dump(LIST * Ptrl)
    84 {
    85     if(isEmpty(Ptrl)==0){
    86         puts("is empty");
    87         exit;
    88     }
    89     LIST * p = Ptrl->next;
    90     while(p != NULL)
    91     {
    92         printf("%d
    ",p->data);
    93         p = p->next;
    94     }
    95     
    96 }
  • 相关阅读:
    临时表空间占用大量空间(新建)
    学习总结
    sql:表关联方式
    11gR2 Clusterware 和 Grid Home
    sql分析常用查询
    通过 SSH 实现 TCP / IP 隧道(端口转发):使用 OpenSSH 可能的 8 种场景
    Fabric部署环境初始化(Centos 7)
    Fabric 学习路线
    代币智能合约(go)
    springboot切面编程范例
  • 原文地址:https://www.cnblogs.com/hanyouchun/p/4318027.html
Copyright © 2011-2022 走看看