C 언어 를 이용 하여 순서 표 의 인 스 턴 스 작업 을 실현 합 니 다.

7706 단어 순서 표c 언어
본 논문 의 사례 는 C 언어 가 순서 표(선형 표)를 실현 하 는 방법 을 서술 하 였 다.여러분 께 참고 하도록 공유 하 겠 습 니 다.구체 적 으로 는 다음 과 같 습 니 다.
1.순서 표 코드 구현

#ifndef _SEQ_LIST_H
#define _SEQ_LIST_H

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

#define ElemType float  // float         ,       int
#define INIT_SIZE 10  //          
#define LIST_INCREMENT 5  //       ,      malloc  

#define list_full(list) ((list)->size >= (list)->capacity ? 1 : 0) //     
#define list_empty(list) ((list)->size == 0 ? 1 : 0)   //  

typedef ElemType value_type;   //    
typedef value_type* value_ptr;   //      
typedef enum {false, true}bool;   // C    bool 
typedef enum {POSITION, VALUE}DELETE_MODE; //        ,             

typedef struct sequelize_list{
 ElemType *base;   //     
 int size;   //       
 int capacity;   //     
} seq_list, *list_ptr;

void init_list(list_ptr lp)  //   
{
 assert(lp != NULL);

 lp->base = (value_ptr)malloc(sizeof(value_type)*INIT_SIZE); //free
 assert(lp->base != NULL);

 memset(lp->base, 0, sizeof(value_type)*INIT_SIZE);

 lp->size = 0;
 lp->capacity = INIT_SIZE;
}

bool insert_elem(list_ptr lp, const int pos, const value_type elem)  //    
{
 assert(lp != NULL && pos >= 1 && pos <= lp->size+1);

 if(list_full(lp)){   //    ,      
 value_ptr new_base = (value_ptr)realloc(lp->base, 
 sizeof(value_type)*(lp->capacity+LIST_INCREMENT));//    realloc  ,           realloc  ,          
 assert(new_base != NULL); //  realloc                 +     ,       ,           ,             ,           ,    ,      
 
 lp->base = new_base;  //       
 lp->base[lp->capacity] = elem;
 lp->capacity += LIST_INCREMENT;
 ++lp->size;

 }
 else{    //  ,    
 for(int i=lp->size-1; i>=pos-1; --i){  //     ,    
 lp->base[i+1] = lp->base[i];
 }
 
 lp->base[pos-1] = elem;   //    
 ++lp->size;
 //}
 return true;
}

bool remove_elem_pos(list_ptr *lp, const int pos) //     
{
 assert(pos >= 1 && pos <= (*lp)->size);  
 
 for(value_ptr ptmp=&(*lp)->base[pos-1]; ptmp<&(*lp)->base[(*lp)->size-1]; ++ptmp) 
 *ptmp = *(ptmp+1);

 (*lp)->base[(*lp)->size-1] = 0;
 --(*lp)->size;
 
 return true;
}

bool remove_elem_val(list_ptr *lp, value_type elem) //    
{
 for(int i=0; i<(*lp)->size; ++i)
 if((*lp)->base[i] == elem){
 for(int j=i; j<(*lp)->size-1; ++j) //            
 (*lp)->base[j] = (*lp)->base[j+1];
 
 (*lp)->base[(*lp)->size-1] = 0;
 --(*lp)->size;
 }
 return true;
}

bool remove_elem(list_ptr lp, const void* clue, DELETE_MODE mode) //    ,                 
{
 assert(lp != NULL);

 if(list_empty(lp)){ //  ,   
 fprintf(stdout, "already empty, can not remove.
"); return false; } if(mode == POSITION){ // POSITION, remove_elem_pos(&lp, *(int *)clue); } else{ // remove_elem_val(&lp, *(value_ptr)clue); } return true; } void print_list(const seq_list sl) // { if(sl.size == 0) fprintf(stdout, "nil list."); for(int i=0; i<sl.size; ++i) printf("%f ", sl.base[i]); printf("
"); } int compare(const void *vp1, const void* vp2) // { return *(value_ptr)vp1 - *(value_ptr)vp2; } int locate_elem(const seq_list sl, const value_type elem, int (*compare)(const void *, const void *)) // { if(list_empty(&sl)){ fprintf(stdout, "list empty, con not locate.
"); } else{ for(int i=0; i<sl.size; ++i) if((*compare)(&sl.base[i], &elem) == 0) // , return i+1; } return 0; } void sort_list(list_ptr lp, int (*compare)(const void *, const void *)) // { assert(lp != NULL); qsort(lp->base, lp->size, sizeof(value_type), compare); // } void merge_list(list_ptr lpa, list_ptr lpb, list_ptr combine, int (*compare)(const void *, const void *)) // { assert(lpa != NULL && lpb != NULL && combine != NULL); combine->base = (value_ptr)malloc(sizeof(value_type) *(lpa->size+lpb->size)); // , , assert(combine->base != NULL); combine->capacity = combine->size = lpa->size+lpb->size; value_ptr it_pc = combine->base; value_ptr it_pa=lpa->base; value_ptr it_pb=lpb->base; while(it_pa<lpa->base+lpa->size && it_pb<lpb->base+lpb->size){ // if(compare(it_pa, it_pb) <= 0) *it_pc++ = *it_pa++; else *it_pc++ = *it_pb++; } while(it_pa < lpa->base+lpa->size) // while , pa ,pb ,pa , *it_pc++ = *it_pa++; while(it_pb < lpb->base+lpb->size) // , pb , , *it_pc++ = *it_pb++; } #endif /*seq_list*/
테스트 코드

      ,  int, float  。
#include "seq_list.h"

#define ARRAY_SIZE 10

int main()
{
 value_type array[] = {39.1, 32.1, 10.1, 11.1, 22.1, 5.1, 36.1, 28.1, 46.1, 32.1};
 seq_list list; //   1
 
 init_list(&list);

 for(int i=0; i<ARRAY_SIZE; ++i)
 insert_elem(&list, 1, array[i]);
 printf("list:
"); print_list(list); #if 1 int clue = 1; // , //value_type clue = 32.1; // , 32.1 , VALUE printf("after remove:
"); remove_elem(&list, &clue, POSITION); print_list(list); #endif #if 1 int found = locate_elem(list, 22.1, compare); // 22.1 if(found >= 1) fprintf(stdout, "found %f at the position %d
", list.base[found-1], found); else fprintf(stdout, "not found.
"); #endif sort_list(&list, compare); printf("list after sort:
"); print_list(list); value_type array2[] = {98.1, 65.1, 4.1, 86.1, 34.1, 21.1, 86.1, 74.1, 93.1, 46.1}; seq_list list2; init_list(&list2); for(int i=0; i<ARRAY_SIZE; ++i) insert_elem(&list2, 1, array2[i]); printf("list2:
"); print_list(list2); printf("list2 after sort:
"); sort_list(&list2, compare); print_list(list2); seq_list new_list; // init , merge_list 。 , 。 merge_list(&list, &list2, &new_list, compare); printf("new merge_list:
"); print_list(new_list); free(list.base); free(list2.base); free(new_list.base); // , return 0; }
3.테스트 결과

총화
이상 이 바로 본 고의 전체 내용 이 므 로 여러분 들 이 C 언어 를 사용 하 는 것 을 배 우 는 데 도움 이 되 기 를 바 랍 니 다.

좋은 웹페이지 즐겨찾기