線表的c語言實現

來源:互聯網
上載者:User

標籤:線表   c語言   

#include<stdio.h>

#include<stdlib.h>


#define LIST_INIT_SIZE 100

#define LIST_INCREMENT 10

#define Status int


typedef int ElemType ; 


typedef struct{

ElemType *elem;

int length;

int listsize;

}SqList;


Status InitList(SqList &L){

L.elem=(ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));

if(!L.elem) exit(1);

L.length=0;

L.listsize=LIST_INIT_SIZE;

return 1;

}

Status Destory(SqList &L){

free(L.elem);

return 1;

}

Status ClearList(SqList &L){

L.elem=NULL;

L.length=0;

return 1;

}

Status IsListEmpty(SqList &L){

if(L.length=0) return 1;

else return 0;

}

Status ListLength(SqList &L){

return L.length;

}


Status GetElem(SqList &L,int i,ElemType &e){

if(i>=1&&i<=L.length){

e=L.elem[i-1];

return 1;

}

else 

return 0;

}


//返回第一個與e相同的元素的位置,不存在返回0

Status LocateElem(SqList &L,ElemType e){

for(int i=0;i<L.length;i++){

if(L.elem[i]==e)

return (i+1);

}

return 0;

}


Status PrioElem(SqList &L,ElemType cur_e,ElemType &pre_e){

for(int i=1;i<L.length;i++){

if(L.elem[i]==cur_e){

pre_e=L.elem[i-1];

return 1;

}

}

return 0;

}


Status NextElem(SqList &L,ElemType cur_e,ElemType &next_e){

for(int i=0;i<L.length-1;i++){

if(L.elem[i]==cur_e){

next_e=L.elem[i+1];

return 1;

}

}

return 0;

}


Status ListInsert(SqList &L,int i,ElemType e){

ElemType *p,*q;

if(i<1||i>L.length+1) return 0;

if(L.length>=L.listsize){

L.elem=(ElemType *)realloc(L.elem,(L.listsize+LIST_INCREMENT)*sizeof(ElemType));

L.listsize+=LIST_INCREMENT;

}

q=&(L.elem[i-1]);

for(p=&(L.elem[L.length-1]);p>=q;p--) *(p+1)=*p;

*q=e;

L.length++;

return 1;

}


Status printList(SqList &L){

for(int i=0;i<L.length;i++)

printf("%d\n",L.elem[i]);

return 1;

}


int main(){

SqList l;

InitList(l);

ElemType e1,e2,e3,e4,e5;

e1=e2=e3=e4=e5=100;

ListInsert(l,1,e1);

ListInsert(l,2,e2);

ListInsert(l,3,e3);

ListInsert(l,4,e4);

ListInsert(l,5,e5);

printList(l);

return 0; 

}

純新手 有誤請指出

本文出自 “zjwzjw369” 部落格,謝絕轉載!

線表的c語言實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.