数据结构与算法 (1) - 线性表

线性表

数组存储(顺序存储)


#ifndef  __ARRAYLIST_H
#define  __ARRAYLIST_H

typedef int  ElemType ;
struct  ArrayList {
  ElemType *Data;
  int size;
  int MaxSize;
};
typedef struct ArrayList *  List;

void InitList(List PtrL,int size);
void ClearList(List PtrL);
int Insert(List PtrL,int position,ElemType X);
int Delete(List PtrL,int position);
#endif




#include "ArrayList.h"
#include "stdio.h"
#include "stdlib.h"
void InitList(List PtrL,int size)
{
  if(size<1)
  {
    printf("Size Error");
    return ;

  } 
 ElemType * X=(ElemType *)(malloc(size*sizeof( ElemType)));
if(!X)
 {
printf("Malloc Error");
}
  PtrL->Data=X;
  PtrL->MaxSize=size;
  PtrL->size=0;
}
void ClearList(List PtrL)
{
  if(PtrL->Data !=NULL)
  {
    free(PtrL->Data);
    PtrL->Data=NULL;
   PtrL->size=PtrL->MaxSize=0; 
  }
}
 int Insert(List PtrL,int position,ElemType X)
{
  int i;
if(position<1||position>PtrL->size+1)  
{
  printf("Position Error");
  return 0; 
}
if(PtrL->size>=PtrL->MaxSize)
{
  printf("So small to save");
  return 0; 
}
for(i=PtrL->size-1;i>=position-1;i--)
{
  PtrL->Data[i+1]=PtrL->Data[i];
}
PtrL->Data[position-1]=X;
PtrL->size++;
 return 1; 
}
int Delete(List PtrL,int position)
{
  int j;
if(position<1||position>PtrL->size)  
{
  printf("Position Error");
  return 0; 
}
for(j=position-1;j<=PtrL->size-1;j++)
{
  PtrL->Data[j]=PtrL->Data[j+1];
}
PtrL->size--;
return  1;
}

insert函数

for(i=PtrL->size-1;i>=position-1;i–)//从最后一个往后依次移一个 ,防止从前面往后移破环数据
{
PtrL->Data[i+1]=PtrL->Data[i];
}
PtrL->Data[position-1]=X;
PtrL->size++;

delete函数

for(j=position-1;j<=PtrL->size-1;j++)//从被删除的后一个往前移 覆盖之前的数据
{
PtrL->Data[j]=PtrL->Data[j+1];
}

链式存储

单(双)向链表

插入操作

#ifndef  __CHAINLIST_H
#define  __CHAINLIST_H
#include  <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#define  ENABLECIRCLE   1   //decide   if we need  circle
struct  Node;
typedef  struct Node  * PtrNode;
typedef  int  ElemType;
struct  Node  {
  ElemType  Elem;
  PtrNode  Next;
};
int  ListSize;

PtrNode  InitchainList();
int IsEmpty(PtrNode L);
PtrNode  Find(ElemType X,PtrNode L);
int Delete(ElemType  X,PtrNode L);
PtrNode  FindPervious(ElemType X,PtrNode L);
int Insert(ElemType X,PtrNode L,int theIndex);
void  DeleteList(PtrNode L);

#endif








#include  "chainList.h"


PtrNode  InitchainList()
{
  PtrNode   L;
   L=malloc(sizeof(struct Node ));
   if(!L)
   {
     printf("malloc error");
   }
   if(ENABLECIRCLE)
   {
   L->Next=L;
   }
   else
   {
   L->Next=NULL; //create a chainList  and init
   }
   ListSize++;
   return   L;  //must as return value   to create  chainList  

}

int IsEmpty(PtrNode L)
{
  if(ENABLECIRCLE)
  {
  return  L->Next ==L; 
  }
  else
  {
  return  L->Next ==NULL;
  }  
}

PtrNode  Find(ElemType X,PtrNode L)
{
  PtrNode  NowPos ;
  NowPos =L->Next;
  if(ENABLECIRCLE)
  {
  while(NowPos !=L&&NowPos->Elem!=X)//when find it or  in the end  
    NowPos=NowPos->Next;
  }
  else 
  {
  while(NowPos !=NULL&&NowPos->Elem!=X)//when find it or  in the end  
    NowPos=NowPos->Next;
  }
   return  NowPos;
}

int Delete(ElemType  X,PtrNode L)
{
  PtrNode NowPos ,temp;
  NowPos=FindPervious(X,L);
  if(ENABLECIRCLE)
  {
  if(NowPos->Next!=L)
  {
    temp=NowPos->Next;//when  delete a member   ,should  save   it  at first  ,after that you can delete it
    NowPos->Next=temp->Next;
    free(temp);
    ListSize--;
    return 1;
  }
  return 0;
  }
  else
  {
  if(NowPos->Next!=NULL)
  {
    temp=NowPos->Next;//when  delete a member   ,should  save   it  at first  ,after that you can delete it
    NowPos->Next=temp->Next;
    free(temp);
    ListSize--;
    return 1;
  }
  return 0;
  }
}




PtrNode  FindPervious(ElemType X,PtrNode L)
{
  PtrNode  Perv;
  Perv=L;
  if(ENABLECIRCLE)
  {
  while(Perv->Next!=L&&Perv->Next->Elem!=X)
    Perv=Perv->Next;
  }
  else
  {
  while(Perv->Next!=NULL&&Perv->Next->Elem!=X)
    Perv=Perv->Next;
  }
  return  Perv;
 
}
int Insert(ElemType X,PtrNode L,int theIndex)
{
  PtrNode  temp ,NowPos;
  NowPos=L;
  if(theIndex<=0||theIndex>ListSize)//Let the Position  current
  {
    printf("The Position  Error!");
    return 0;
  }
  temp=malloc(sizeof(struct Node));
   if(!temp)
   {
     printf("malloc error");
   }
  if(theIndex==1)
  {
      temp->Elem=X;
      temp->Next=L->Next;
      L->Next=temp;
  }
  else
  {
  for(int i=0;i<theIndex-1;i++)  
  {
  NowPos=NowPos->Next;  //find  the previous  member   
  }
  temp->Next=NowPos->Next;
  NowPos->Next=temp;
  temp->Elem=X;
  }
  ListSize++;
  return 1;
}

void   DeleteList(PtrNode L)
{
  PtrNode  temp ,NowPos;
  NowPos=L->Next;
  if(ENABLECIRCLE)
  {
  while(NowPos!=L)
  {
  temp =NowPos->Next;
  free(NowPos);
  NowPos=temp; 
  }
  ListSize=0;
  }
  else
  {
  while(NowPos!=NULL)
  {
  temp =NowPos->Next;
  free(NowPos);
  NowPos=temp; 
  }
  ListSize=0;
  }
 
}


双向循环链表

插入
删除


#ifndef   __DOUBLECHAINLIST_H
#define   __DOUBLECHAINLIST_H
#include  <stdio.h>
#include <stdlib.h>
#include <stddef.h>
typedef  int ElemType;

struct  dNode  ;
typedef  struct dNode * PtrdNode;
struct  dNode  {
PtrdNode  next;
PtrdNode  perv;
ElemType  data;
};
int ListSize;
PtrdNode InitList();
int ClearList(PtrdNode L );
int IsEmpty(PtrdNode L);
int Insert(PtrdNode L,ElemType X,int Index);
int Delete(PtrdNode L,int Index);
PtrdNode GetPosition(PtrdNode L,int Index);
ElemType   IndexOf(int Index,PtrdNode L);
int  Find(ElemType  X,PtrdNode L,int order);
#endif







#include "doublechainList.h"

PtrdNode InitList()
{
  PtrdNode L=malloc(sizeof( struct dNode));
  if(!L)
  {
    printf("malloc error");
    return NULL;
  }
  L->next=L->perv=L;
  ListSize++;
  return L;
}
int ClearList(PtrdNode L )
{
  PtrdNode temp,NowPos=L->next;
  while(NowPos!=L)
  {
    temp =NowPos->next;
    free(NowPos);
    NowPos=temp;
    ListSize=0;
  }
  return 1;
}
int IsEmpty(PtrdNode L)
{
  if(L->next==L&&L->perv==L)
    return 1;
  else 
    return 0;
}
PtrdNode GetPosition(PtrdNode L,int Index)
{
  int j;
  PtrdNode temp=L;
  for(j=1;j<=Index;j++)
    temp=temp->next;
  return temp;
}
int Insert(PtrdNode L,ElemType X,int Index)
{
  PtrdNode temp,NowPos=L;
  if(Index<1||Index>ListSize)
  {
    printf("Position Error!");
    return 0;
  }
  temp=malloc(sizeof(struct dNode));
  NowPos=GetPosition(L,Index-1);
  temp->data=X;
  temp->perv=NowPos;
  temp->next=NowPos->next;
  NowPos->next->perv=temp;
  NowPos->next=temp;
  ListSize++;
  return 1;
}
int Delete(PtrdNode L,int Index)
{
  PtrdNode  NowPos;
  if(Index<1||Index>ListSize)
  {
    printf("Position Error!");
    return 0;
  }
  NowPos=GetPosition(L,Index);
  NowPos->perv->next=NowPos->next;
  NowPos->next->perv=NowPos->perv;
  free(NowPos);
  ListSize--;
  return 1;
}

ElemType   IndexOf(int Index,PtrdNode L)
{
  PtrdNode  NowPos;
  if(Index<1||Index>ListSize)
  {
    printf("position error");
    return 0;
  }
  NowPos=GetPosition(L,Index);
  return NowPos->data;
}
int  Find(ElemType  X,PtrdNode L,int order)
{
  int Index=0;
  PtrdNode  NowPos;
  if(order)
    NowPos=L->next;
  else
    NowPos=L->perv;
  while(NowPos!=L&&NowPos->data!=X)
  {
    if(order)
      NowPos=NowPos->next;
    else
      NowPos=NowPos->perv;
    Index++;
  }  
  if(NowPos==L)
    return -1;
  else
    return Index+1;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值