(二)Linix下多文档的数据结构顺序表实现

来源:互联网 发布:龙信数据有限公司 编辑:程序博客网 时间:2024/06/06 14:08

makefile代码:在makefile文件中


  1 CC = gcc                                                                    
  2 CFLAGS = -O0 -g -Wall
  3 test:test.c homework.c

                     $(CC) $(CFLAGS) -o $@ $^

  4 .PHONY:clean
  5 clean:
  6         rm -rf test


函数文件代码:在homework.c中

 #include <stdio.h>                                   
#include <stdlib.h>
#include "homework.h"    

seqlist_t * creat_seqlist(void)
{
        seqlist_t *L = NULL;
        L = (seqlist_t *) malloc(sizeof(seqlist_t));
        if(L == NULL) //malloc创建空间可能出错
        {       
                puts("no memory");
                return NULL;
        }
        L->last = -1;
        return L;
}

void clear_seqlist(seqlist_t *L)
{
        if(L == NULL)
        {
                puts("seqlist_t *L is NULL");
                return;
        }
        free(L);
        return;
}

int is_empty_seqlist(seqlist_t *L)
{
        if(L ==NULL)
        {
                puts("seqlist_t *L is NULL");
                return -1;
        }
        return (L->last == -1);
}

int is_full_seqlist(seqlist_t *L)
{
        if(L == NULL)
        {
                puts("seqlist_t *L is NULL");
                return -1;
        }
        return (L->last == MAXSIZE - 1);
}
void set_empty_seqlist(seqlist_t *L)
{
        if(L == NULL)
        {
                puts("seqlist_t *L NULL");
                return;
        }
        L->last = -1;
        return;
}                                                         

 int get_length_seqlist(seqlist_t *L)
 {
         if(L == NULL)
         {
                 puts("seqlist_t *L isNULL");
                 return -1;
         }
         return (L->last+1);
 }
 
 void show_seqlist(seqlist_t *L)
 {
         int i = 0;
         if(L == NULL)
         {
                 puts("seqlist_t *L is NULL");
                 return;
         }
         for(i = 0;i <= L->last;i++)
                 printf("L->data[%d] = %d\n",i,L->data[i]);
         return;
 
 }
 int insert_seqlist(seqlist_t *L,data_t x,int pos)
 {
         int i = 0;
         if((is_full_seqlist(L)) || (pos < 0) ||(pos> L->last + 1))
         {
                 puts("input argv is invalid");
                 return -1;
         }
         for(i = L->last;i >= pos;i--)
         {
                 L->data[i+1] = L ->data[i];
         }
         L->data[pos] = x;
         L->last++;
         return 0;
 }                                                                    
 int delete_seqlist(seqlist_t *L,int pos)
 {
         int i = 0;
         if((pos < 0)|| (pos >L->last))
         {
                 puts("input pos is invalid");
                 return -1;
         }
         for(i = pos;i< get_length_seqlist(L);i++)
         {
                 L->data[i] = L->data[i+1];
         }
         L->last--;
         return 0;
 }
 
 int change_seqlist(seqlist_t *L,data_t x,int pos)
 {
 
         if((pos < 0)|| (pos >L->last))
         {
                 puts("input pos is invalid");
                 return -1;
         }
         L->data[pos] = x;
         return 0;
 }
 
 int search_seqlist(seqlist_t *L,data_t x)
 {
         int i = 0;
         for(i = 0;i<=L->last;i++)
         {
                 if(L->data[i] == x)
                         return i;
         }
         return -1;
 }
 void Purge(seqlist_t *L)
 {
         int i = 0,j;
         int x,y;
         while(i <get_length_seqlist(L) - 1)
         {
                 x = L->data[i];
                 j = i + 1;
                 while(j < get_length_seqlist(L))
                 {
                         y = L->data[j];
                         if(x == y)
                         {
                                 delete_seqlist(L,j);
                         }
                         else
                         {
                                 j++;
                         }
                 }
                 i++;
 
         }
 }                                                              




 


头文件代码: 在homework.h文件中

 #ifndef __HOMEWORK_H__   
 #define __HOMEWORK_H__

#define MAXSIZE 100
typedef int data_t;
typedef  struct
{
        data_t data[MAXSIZE];
        int last;      
}seqlist_t;
seqlist_t * creat_seqlist(void);
void clear_seqlist(seqlist_t *L);

int is_empty_seqlist(seqlist_t *L);
int is_full_seqlist(seqlist_t *L);
void set_empty_seqlist(seqlist_t *L);
int get_length_seqlist(seqlist_t *L);
void show_seqlist(seqlist_t *L);

int insert_seqlist(seqlist_t *L,data_t x,int pos);
int delete_seqlist(seqlist_t *L,int pos);
int change_seqlist(seqlist_t *L,data_t x,int pos);
int search_seqlist(seqlist_t *L,data_t x);

void Purge(seqlist_t *L);
#endif                                                


主函数代码: 主函数在test.c文件中

#include <stdio.h>                          
#include <stdlib.h>
#include "homework.h"
int main()
{
        int n = 0;
        seqlist_t *L = NULL;
        L = creat_seqlist();
        puts("insert 10 number");
        for(int i = 0;i < 10;i++)
        {
                scanf("%d",&n);
                insert_seqlist(L,n,0);

        }
        Purge(L);
        show_seqlist(L);
        clear_seqlist(L);
        return 0;
}

                                            



  


阅读全文
0 0