主函数代码(main.c)
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include "sqlist.h"
int main ( )
{ sqlist * list= NULL , * list_next= NULL ; int pos = 0 ; int i, err, value; int arr[ 10 ] = { 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 } ; int arr_next[ 10 ] = { 11 , 22 , 33 , 44 , 55 , 66 , 77 , 88 , 99 , 111 } ; list = create_sqlist ( ) ; if ( list == NULL ) { fprintf ( stderr , "create_sqlist()failed\n" ) ; } for ( i = 0 ; i < 10 ; i++ ) { add_sqlist ( list, & i) ; } for ( i = 0 ; i < sizeof ( arr) / sizeof ( * arr) ; i++ ) { if ( ( err = insert_sqlist ( list, 2 , & arr[ i] ) ) != 0 ) { if ( err = - 1 ) { fprintf ( stderr , "The arr is full\n" ) ; } else if ( err = - 2 ) { fprintf ( stderr , "The pos you want to insert is wrong\n" ) ; } else { fprintf ( stderr , "ERROR\n" ) ; } } } display_sqlist ( list) ; list_next = create_sqlist ( ) ; if ( list_next == NULL ) { fprintf ( stderr , "create_sqlist()failed\n" ) ; } for ( i = 0 ; i < 10 ; i++ ) { add_sqlist ( list_next, & i) ; } for ( i = 0 ; i < sizeof ( arr_next) / sizeof ( * arr_next) ; i++ ) { if ( ( err = insert_sqlist ( list_next, 2 , & arr_next[ i] ) ) != 0 ) { if ( err = - 1 ) { fprintf ( stderr , "The arr is full\n" ) ; } else if ( err = - 2 ) { fprintf ( stderr , "The pos you want to insert is wrong\n" ) ; } else { fprintf ( stderr , "ERROR\n" ) ; } } } display_sqlist ( list_next) ; union_sqlist ( list, list_next) ; display_sqlist ( list) ; destroy_sqlist ( list_next) ; destroy_sqlist ( list) ; }
sqlist.h文件代码(负责函数声明)
#ifndef SQLIST_H__
#define SQLIST_H_
#define MAX_LENGTH 1024
typedef int datatype; typedef struct sqlist
{ datatype data[ MAX_LENGTH] ; int size;
} sqlist;
sqlist* create_sqlist ( void ) ;
void create_sqlist1 ( sqlist * ps) ;
void display_sqlist ( sqlist * ps) ;
int add_sqlist ( sqlist * ps, datatype * num) ;
int delete_sqlist ( sqlist * ps, int pos) ;
int insert_sqlist ( sqlist * ps, int pos, datatype * num) ;
void destroy_sqlist ( sqlist * ps) ;
int find_sqlsit ( sqlist * ps, int pos) ;
int find1_sqlist ( sqlist * ps, datatype* value) ;
int setempty_sqlist ( sqlist* ps) ;
int union_sqlist ( sqlist* list1, sqlist* list2) ;
#endif
sqlist.c文件代码(负责函数定义)
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include "sqlist.h"
sqlist* create_sqlist ( void )
{ sqlist * ps; ps= ( sqlist* ) malloc ( sizeof ( sqlist) ) ; if ( ps == NULL ) { return NULL ; } else { ps-> size = 0 ; return ps; }
} void create_sqlist1 ( sqlist * ps)
{ ps = malloc ( sizeof ( sqlist) ) ; if ( ps == NULL ) { return ; } else { ps-> size = 0 ; return ; }
} void display_sqlist ( sqlist * ps)
{ if ( ps-> size == 0 ) { printf ( "数组为空\n" ) ; return ; } int i = 0 ; printf ( "数组中元素个数为%d\n" , ps-> size) ; printf ( "数组中的元素依次为:\n" ) ; for ( i = 0 ; i < ps-> size; i++ ) { printf ( "%d : %d\n" , i + 1 , ps-> data[ i] ) ; }
}
int add_sqlist ( sqlist * ps, datatype * num)
{ ps-> data[ ps-> size] = * num; ps-> size++ ; return 0 ; } int insert_sqlist ( sqlist * ps, int pos, datatype * num)
{ int j = 0 ; if ( ps-> size == MAX_LENGTH) { return - 1 ; } if ( pos < 0 || pos > ps-> size) { return - 2 ; } for ( j = ps-> size; j > pos; j-- ) { ps-> data[ j] = ps-> data[ j- 1 ] ; } ps-> data[ pos] = * num; ps-> size++ ; return 0 ; } int delete_sqlist ( sqlist * ps, int pos)
{ int i = 0 ; pos = pos - 1 ; if ( ps-> size == 0 ) { printf ( "删除失败, 顺序表为空\n" ) ; return - 1 ; } if ( pos< 0 || pos>= ps-> size) { printf ( "删除失败,pos处没有元素\n" ) ; return - 2 ; } for ( i = pos; i < ps-> size- 1 ; i++ ) { ps-> data[ i] = ps-> data[ i + 1 ] ; } ps-> size-- ; printf ( "pos处的元素删除成功\n" ) ; return 0 ;
}
int find_sqlsit ( sqlist * ps, int pos)
{ if ( ps-> size == 0 ) { printf ( "查找失败, 顺序表为空\n" ) ; return - 1 ; } if ( pos <= 0 || pos > ps-> size) { printf ( "查找失败,pos处没有元素\n" ) ; return - 2 ; } printf ( "顺序表中第%d个元素为%d" , pos, ps-> data[ pos - 1 ] ) ; printf ( "查找成功\n" ) ; return 0 ;
}
int find1_sqlist ( sqlist * ps, datatype* value)
{ int i = 0 ; if ( ps-> size == 0 ) { return - 1 ; } for ( i = 0 ; i < ps-> size; i++ ) { if ( ps-> data[ i] == * value) { return i + 1 ; } } return 0 ;
} int setempty_sqlist ( sqlist* ps)
{ ps-> size = 0 ; return 0 ;
} int union_sqlist ( sqlist* list1, sqlist* list2)
{ int i = 0 ; if ( list2 == NULL && list1 != NULL ) { printf ( "list1与list2合并成功\n" ) ; return 0 ; } if ( list1 == NULL ) { printf ( "list1与list2合并失败\n" ) ; return - 1 ; } for ( i = 0 ; i < list2-> size; i++ ) { if ( ( find1_sqlist ( list1, & list2-> data[ i] ) ) <= 0 ) { insert_sqlist ( list1, list1-> size, & list2-> data[ i] ) ; } } printf ( "list1与list2合并成功\n" ) ; return 0 ;
} void destroy_sqlist ( sqlist * ps)
{ free ( ps) ; ps = NULL ; }