1) 链表--顺序存储结构

  1 #include <iostream>
  2 using namespace std;
  3 
  4 typedef void Seqlist;
  5 typedef void SeqlistNode;
  6 
  7 typedef struct
  8 {
  9     int length;
 10     int capacity;
 11     unsigned int *node;// 为了动态化节点。
 12 }TSeqList;
 13 
 14 Seqlist* SqList_Create(int capacity)//分配空间
 15 {
 16     int ret=0;
 17     TSeqList *tmp=NULL;
 18 
 19     tmp=(TSeqList*)malloc(sizeof(TSeqList));//分配空间
 20     if(tmp=NULL)
 21     {
 22         ret=-1;
 23         cout<<"func SqList_Create() err: "<<ret<<endl;
 24         return NULL;
 25     }
 26 
 27     memset(tmp,0,sizeof(TSeqList));
 28 
 29     //根据capacity的大小分配节点的空间
 30     tmp->node=(unsigned int*) malloc(sizeof(unsigned int*)*capacity);
 31     if(tmp=NULL)
 32     {
 33         ret=-2;
 34         cout<<"func SqList_Create() : malloc err  "<< ret<<endl;
 35         return NULL;
 36     }
 37     return tmp;
 38 }
 39 
 40 void SeqList_Destory (Seqlist* list)//释放内存空间
 41 {
 42     TSeqList *tList=NULL;
 43     if(list=NULL)
 44         return ;
 45     tList=(TSeqList *) list;
 46 
 47     if(tList->node!=NULL)
 48     {
 49         free (tList->node);
 50     }
 51     free(tList);
 52     return;
 53 }
 54 
 55 //清空链表---回到初始化状态  链表长度==0
 56 void SeqList_Clear(Seqlist *list)
 57 {
 58     TSeqList *tList=NULL;
 59     if(list=NULL)
 60         return ;
 61     tList=(TSeqList *) list;
 62 
 63     tList->length=0;
 64     return;
 65 }
 66 
 67 int SeqList_Length(Seqlist *list)//链表的实际长度
 68 {
 69     TSeqList *tList=NULL;
 70     if(list=NULL)
 71         return -1 ;
 72     tList=(TSeqList *) list;
 73 
 74     return tList->length;
 75 }
 76 
 77 int SeqList_Capacity(Seqlist *list)//链表的容量
 78 {
 79     TSeqList *tList=NULL;
 80     if(list=NULL)
 81         return -1 ;
 82     tList=(TSeqList *) list;
 83 
 84     return tList->capacity;
 85 }
 86 
 87 int  SeqList_Insert(Seqlist *list,SeqlistNode *node,int pos)
 88 {
 89     int ret=-1;
 90     int i=0;
 91     TSeqList *tList=NULL;
 92     if(list==NULL||node==NULL||pos<0)
 93     {
 94         cout<<"func SqList_Create() : malloc err  "<< ret<<endl;
 95         return ret;
 96     }
 97 
 98     tList=(TSeqList *)list;
 99 
100     //判断是不是满了
101     if(tList->length>=tList->capacity)
102     {
103         ret=-2;
104         cout<<"func SqList_Create() : malloc err  "<< ret<<endl;
105         return ret;
106     }
107 
108     //容错修正 6个长度 容量20;用户pos10位置插入
109     if(pos>=tList->length)
110     {
111         pos=tList->length;
112     }
113 
114     //元素后移
115     for( i=tList->length;i>pos;i--)
116     {
117         tList->node[i]=tList->node[i-1];
118     }
119     //插入元素
120     tList->node[i]=(unsigned int) node;
121     return 0;
122 }
123 
124 int Seqlist_get(Seqlist* list,int pos)
125 {
126     int ret=-1;
127     int i=0;
128     TSeqList *tList=NULL;
129     if(list==NULL||pos<0)
130     {
131         cout<<"func SqList_Create() : malloc err  "<< ret<<endl;
132         return -1;
133     }
134     tList=(TSeqList *)list;
135     tList->length++;
136 
137     return (unsigned)tList->node[pos];
138 }

 

posted @ 2016-08-22 22:13  蜗牛在奔跑  阅读(559)  评论(0编辑  收藏  举报