This File Define the DS
of Sequential Linear List's
basic operation, It includes
Linear list insert,delete,initial,
and sort operations*/
/*Coded by csenior 2005-7-25*/
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100
#define FALSE 0
#define TRUE 1
typedef int ElemType;
typedef int BOOL;
typedef struct
{
ElemType value[MAXSIZE];
int cursize;
}SList;
void InitSList(SList* sl) /*Initial the SList*/
{
sl->cursize=0;
}
BOOL InsertSListEnd(SList* sl,ElemType val) /*Insert a Element at the end of the list*/
{
if(sl->cursize<MAXSIZE)
{
sl->value[sl->cursize]=val;
sl->cursize++;
return TRUE;
}
else
return FALSE;
}
BOOL InsertSListAt(SList* sl,ElemType val,int pos)
{
int i;
if(pos<1||pos>sl->cursize)
{
i=sl->cursize+1;
for(;i>pos;i--)
{
sl->value[i-1]=sl->value[i-2];
}
sl->value[i-1]=val;
sl->cursize++;
return TRUE;
}
else
return FALSE;
}
BOOL DeleteSList(SList* sl,ElemType val) /*Delete The first element which is equal to value*/
{
int i,pos;
for(i=1;i<=sl->cursize;i++)
{
if(sl->value[i-1]==val)
{
pos=i;
break;
}
}
if(i<=sl->cursize)
{
for(;i<sl->cursize-1;i++)
{
sl->value[i-1]=sl->value[i];
}
sl->cursize--;
return TRUE;
}
else
return FALSE;
}
BOOL DeleteSListAt(SList* sl,int i)
{
int pos;
if(i<1||i>sl->cursize)
return FALSE;
else
{
for(pos=1;pos<sl->cursize-1;pos++)
{
sl->value[pos-1]=sl->value[pos];
}
sl->cursize--;
return TRUE;
}
}
int FindSList(SList* sl,ElemType val)
{
int i;
for(i=1;i<=sl->cursize;i++)
{
if(sl->value[i-1]==val)
return i;
}
return 0;
}
int CountSList(SList* sl,ElemType val)
{
int i,cnt;
cnt=0;
for(i=1;i<=sl->cursize;i++)
{
if(sl->value[i-1]==val)
{
cnt++;
return i;
}
}
return cnt;
}
BOOL IsEmptySList(SList* sl)
{
if(sl->cursize==0)
return TRUE;
else
return FALSE;
}
void PrintSList(SList* sl)
{
int i;
for(i=1;i<=sl->cursize;i++)
{
printf("%d,",sl->value[i-1]);
}
printf("/n");
}
void main()
{
SList mysl;
int i;
ElemType val;
clrscr();
InitSList(&mysl);
for(i=1;i<=50;i++)
{
val=i;
InsertSListEnd(&mysl,val);
}
PrintSList(&mysl);
getch();
}