数据结构链表操作

  1 #define  ListSize  100
  2 #include<stdio.h>
  3 // 存储结构
  4 typedef struct Seqlist
  5 {
  6     int list[ListSize];
  7     int length;
  8 };
  9 //初始化
 10 void InitList(Seqlist *L)
 11 {
 12     L->length=0;
 13 }
 14 
 15 //判断是否为空
 16 int ListEmpty(Seqlist L)
 17 {
 18     if(L.length==0)
 19         return 1;
 20      else
 21          return 0;
 22 }
 23 //序号查找
 24 int GetNum(Seqlist L,int i ,int *e)
 25 {
 26     if(i<1||i>L.length)
 27         return 0;
 28     else
 29     
 30         *e=L.list[i-1];
 31     return 1;
 32 }
 33 
 34 //按内容查找
 35 
 36 int LocateNum(Seqlist L,int e)
 37 {
 38  int i;
 39  for (int i = 0; i < L.length; i++)
 40  {
 41      if(L.length==e)
 42          return  i+1;
 43      return 1;
 44  }
 45 }
 46 
 47 int InsertList(Seqlist *L,int i,int e)
 48 {
 49    int j;
 50    if(i<i||i>L->length+1)
 51    {
 52    printf("插入位置非法!");
 53    return 0; 
 54    }
 55    else if(L->length>=ListSize)
 56    {
 57     printf("链表存储已满!");
 58     return 0;
 59    }
 60    else
 61    {
 62      for(j=L->length;j>=i;j--)
 63          L->list[j]=L->list[j-1];
 64          L->list[i-1]=e;
 65          L->length=L->length+1;
 66          return 1;
 67    }
 68 }
 69 //删除第i个元素
 70 int DeleteList( Seqlist *L,int i,int *e)
 71 {
 72   int j;
 73   if(L->length<=0)
 74   {
 75   
 76   printf("链表为空!");
 77   return 0;
 78   }
 79   else if(i<1||i>L->length)
 80   {
 81        printf("删除位置不合适!\n");
 82        return 0;
 83   }
 84   else
 85   {
 86       *e=L->list[i-1];
 87       for ( j = i; j < L->length; j++)
 88       {
 89           L->list[j-1]=L->list[j];
 90 
 91       }
 92       L->length=L->length-1;
 93       return 1;
 94   }
 95 }
 96 
 97 // 求表长
 98 
 99 int ListLength(Seqlist L)
100 {
101     return L.length;
102 }
103 
104 // 清空表
105 void ClearList(Seqlist L)
106 {
107     L.length=0;
108 }
 1 #include"Seqlist.h"
 2 #include<stdio.h>
 3 #define ListSize 100
 4 int main()
 5 {
 6     /*    InsertList  DeleteList ClearList GetNum ListEmpty ListLength LocateNum */ 
 7     int a[5]={1,3,5,7,8};
 8     int b[5]={10,12,14,16,18};
 9     Seqlist A,B;
10     int e;
11     InitList(&A);
12     InitList(&B);
13     for (int i = 0; i <sizeof(a); i++)
14     {
15         InsertList(&A ,i,a[i]);
16     }
17     for (int i = 0; i < A.length; i++)
18     {
19         if(GetNum(A,i,&e));
20         printf("A表内容是:%d\n",e);
21     }
22     
23     for (int i = 0; i <sizeof(b); i++)
24     {
25         InsertList(&B ,i,b[i]);
26     }
27     for (int i = 0; i < B.length; i++)
28     {
29         if(GetNum(B,i,&e));
30         printf("B表内容是:%d\n",e);
31     }
32     
33     return 0;
34 
35  }

 

posted @ 2016-10-25 10:36  #ifndef  阅读(299)  评论(0编辑  收藏  举报