⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 slist.h

📁 一些基本的数据结构
💻 H
字号:
/*--------------------------------------------------------------------------------*/
//线性表的顺序存储的各种操作 制作者 蔡世玉 时间2008年2月6日//
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <process.h>
/*--------------------------函数状态表--------------------------------------------*/
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define OVERFLOW 0

typedef int Status;
typedef int ElemType;

/*------------------动态分配存储结构----------------------------------------------*/
#define Sqlist_init_size  100
#define LISTINGREMENT     10

typedef struct
        {
          ElemType *base;                          /*存储基址*/
          int length;                              /*当前长度*/
          int listsize;                            /*分配容量*/
        }Sqlist;  

/*--------------------线性表基本操作----------------------------------------------*/
/*----------------------创建线性表---------------------*/
Status InitList_Sq( Sqlist *L )
  {
   (*L).base = ( ElemType* )malloc( Sqlist_init_size * sizeof( ElemType ) );
   if( !(*L).base )       exit( OVERFLOW );
   (*L).length = 0;
   (*L).listsize = Sqlist_init_size;
   return OK;
  } 


/*------------------线性表的插入--------------------*/
Status ListInsert_Sq( Sqlist *L, int i, ElemType e )
 {
  int j;
  ElemType *newbase;
  if( i<1 || i>(*L).length + 2 )              
      return ERROR ;

  if( (*L).length>=(*L).listsize )
   {
     newbase = (ElemType* )realloc( (*L).base, ( (*L).listsize + LISTINGREMENT ) * sizeof( ElemType) );
     if( !newbase )                        
         exit( OVERFLOW );

     (*L).base = newbase;
     (*L).listsize += LISTINGREMENT;
   }
  if( i == (*L).length + 1 )           (*L).base[i-1] = e;

  else
   {
     for( j = ( (*L).length ) ; j >= i; j-- )
     (*L).base[j] = (*L).base[j-1];
     (*L).base[i-1] = e;
   }
  (*L).length++;
  return OK;
 }

/*----------------线性表删除----------------*/
Status ListDelete_Sq( Sqlist *L, int i, ElemType *e )
{
 int j;
 if( i<1 || i>(*L).length )
  return ERROR;
 *e = (*L).base[i-1];
 for( j = i-1; j<=( (*L).length - 2 ); j++ )
  (*L).base[j] = (*L).base[j+1];
 (*L).length--;
 return OK;
}

/*---------------线性表的查找---------------*/
Status ListLocateElem_Sq( Sqlist L, ElemType e )
 {
  int i;
  ElemType  c;
  printf( "%d\n", e );
  for(i=1;i<=L.length;i++)
    {

    c=*(L.base + i-1 );
    if(e==c)    break;
     }
  if( i<=L.length )
   return i;
  else
   return FALSE;
 }

/*----------------------------------------------------------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -