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

数据结构之顺序表

时间:2019-07-25 23:29:28      阅读:90      评论:0      收藏:0      [点我收藏+]

标签:insert   返回   clu   fine   data   lis   在线   oid   i++   

  1 #include<stdio.h>
  2 #include<stdlib.h>
  3 #define ElementType int
  4 #define MAXSIZE 100
  5 
  6 struct LNode{
  7     ElementType Data[MAXSIZE];
  8     int Last;
  9 };
 10 typedef struct LNode *List;
 11 struct LNode L;
 12 
 13 List MakeEmpty();
 14 int Find(ElementType x,List PtrL);
 15 void Insert(ElementType x,List PtrL,int i);
 16 void Delete(int i,List PtrL);
 17 
 18 int main()
 19 {
 20     List L1 = MakeEmpty();
 21     Insert(10,L1,1);
 22     Insert(11,L1,2);
 23     Insert(12,L1,3);
 24     Insert(13,L1,4);
 25     Insert(14,L1,5);
 26     int k;
 27     for(k=0;k<=L1->Last;k++){
 28         printf("%d %d\n",L1->Data[k],L1->Last);
 29     }
 30     Delete(2,L1);
 31     for(k=0;k<=L1->Last;k++){
 32         printf("%d %d\n",L1->Data[k],L1->Last);
 33     }
 34     int i = Find(1999,L1);
 35     int j = Find(14,L1);
 36     printf("%d\n",i);
 37     printf("%d",j);
 38     return 0;
 39  }
 40   
 41  List MakeEmpty() //初始化,建立空的线性表 
 42  {
 43      List PtrL;
 44      PtrL=(List)malloc(sizeof(struct LNode));
 45      PtrL->Last = -1;
 46      return PtrL; 
 47  }
 48  
 49  
 50  int Find(ElementType x,List PtrL)  //在线性表中查找x ,返回x所在位置 
 51 {
 52     int i=0;
 53     while(i<=PtrL->Last&&PtrL->Data[i] != x){
 54         i++;
 55     }
 56     if(i>PtrL->Last){
 57         return -1;
 58     }
 59     else return i;
 60 }
 61 
 62 
 63 void Insert(ElementType x,List PtrL,int i) //将x插入n位置处 
 64 {
 65     
 66     int j;
 67     if(PtrL->Last==MAXSIZE-1)
 68     {
 69         printf("表满!");
 70         return;
 71     }
 72     if(i<=0 || i>PtrL->Last+2)
 73     {
 74         printf("插入非法!");
 75         return;
 76     }
 77     for(j=PtrL->Last+1;j>i-1;j--) //将i到MAXSIZE位置的s所有元素后移一个单位 
 78     {                          
 79         PtrL->Data[j+1] = PtrL->Data[j];
 80     }
 81     PtrL->Data[i-1]=x;
 82     PtrL->Last+=1;
 83  } 
 84  
 85 
 86 void Delete(int i,List PtrL)
 87 {
 88     int j;
 89     if(i>PtrL->Last || i<1)
 90     {
 91         printf("不存在第%d个元素!",i);    
 92         return;
 93     }
 94     for(j=i;j<=PtrL->Last;j++)
 95     {
 96         PtrL->Data[j-1] = PtrL->Data[j];
 97     }
 98     PtrL->Last--;
 99     return;
100 }

 

数据结构之顺序表

标签:insert   返回   clu   fine   data   lis   在线   oid   i++   

原文地址:https://www.cnblogs.com/cnblogs-xiaoqi/p/11247446.html

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