顺序表的实现(包含插入,删除,及查找)

来源:互联网 发布:紫砂壶淘宝店推荐 编辑:程序博客网 时间:2024/05/22 12:37
      #include <stdio.h>
      #define MAX 1000
     int isfull(int n);//judge if the list is full or not
     void insert(int *p,int len,int i,int elem);//*p represent the first element'id of list,i represent what location do you want to insert? elem represent what     elementdo you want to insert?
     void delete(int *pi,int len,int i);//*p represent the first element'id of list,i represent what location do you want to delete?
     int lookelem(int *p,int n,int elem);//what element do you want to look for in the list?;n represent the length of the list.
     main(){
       int i,j,k;
       int loca,elem;
        int n
        int s[MAX];
        printf("please input  list:")
           for(i = 0;i < MAX;i++){
                scanf("%d",s[i] != '\n');
           }
              printf("please choose: ");
        printf("0 represent you want to insert element.\n");
        printf("1 represent you want to delete element.\n");
        printf("2 represent you want to look element.\n");
               scanf("%d",&n);
          switch(n){
            printf("please choose:")
        case 'o':
            print("please input the location and element:");
                scanf("%d,%d",&loca,&elem);
              insert(s,i,loca,elem);
            case'1':
              print("please input the location and element:");
                    scanf("%d",&loca);
                  delete(s,i,loca);
            case'2':
              print("please input element:");
                scanf("%d",&elem);
               k = lookelem(s,i,);
               if(j == 0 )
                 printf("the list have no element you want look\n");
               else
                 printf("the list is %d",j);
              default:
                return -1;
          }
          printf("the last list is:\n");
        for(k = 0;k < MAX;k++){
          printf("%d",s[k]);
          if(s[k] == '\0')
             break;
        }
      }
    
      int isfull(int n){
         if(n > MAX)
           printf(the list is full);
         else
          return 0;
      }
     
      void insert(int *p,int len,int i,int elem){
          int j,k;
            for(k = len;k > i;k--)
              *(p+k) = *(p+k);
           *(p+k) = elem;
           isfull(len + 1);
           printf("the list is:\n");
           for(j = 0;j < len+2;j++)
             printf("%d",*(p+j));
      }
    
      void delete(int *pi,int len,int i){
      int j,k;
        for(j = len;j < i;j--)
          *(p+j) = *(p+j-1);
        printf("now the list is:\n");
         for(k = 0;k < len;k++)
          printf("%d",*(p+k));
      }
    
      int lookelem(int *p,int n,int elem){
        int k;
        for(k = 0;k < n+1;k++){
          if(elem = *(p+k))
            return p;
          else
            return 0;
        }
    }


原创粉丝点击