Sequential implementation of linear tables

Source: Internet
Author: User

SeqLsit.h#ifndef _seqlist_h_#define _SEQLIST_H_typedef voidSeqlist;//is used for encapsulation, not to be changed by the outside worldtypedef voidSeqlistnode; seqlist* Creat_seqlist (intcapacity); seqlist* Get_node_seqlist (seqlist*List,intPOS); seqlist* Delet_node_seqlist (seqlist*List,intPOS);intGet_cpacity_seqlist (seqlist*List);intGet_length_seqlist (seqlist*List);intClear_seqlist (seqlist*List);intAdd_seqlist (seqlist*List, seqlistnode* node,intPOS);voidDestory_seqlist (seqlist*List);#endif
Seqlist.c#include <stdio.h>#include <stdlib.h>#include <malloc.h>#include "SeqList.h"typedef unsignedinttseqlistnode;typedef struct _tar_seqlist{intcapacity;int length; tseqlistnode* node;} Tseqlist; seqlist* Creat_seqlist (intcapacity) {tseqlist* ret = NULL;if(Capacity >=0) {ret = (tseqlist*)malloc (sizeof (tseqlist) + sizeof (Tseqlistnode)*capacity));if(NULL! = ret)            {ret->capacity = capacity; Ret->length=0; Ret->node = (Tseqlistnode*)(ret +1); }    }Else{ret = NULL; }return(seqlist*)RET;}intAdd_seqlist (seqlist* list, seqlistnode* node,int POS){inti =0; tseqlist* Tlist = (tseqlist*)Listintret = (NULL!=tlist); RET = RET && (POS>=0); RET = ret && (tlist->length+1<= tlist->capacity);if(1= = ret) {if(POS> tlist->length)        {POS= tlist->length; } for(i = tlist->length; i >POS; i--) {Tlist->node[i] = tlist->node[i-1];printf("tlist->node[i] = %p\ n", Tlist->node[i]); } Tlist->node[i] = (tseqlistnode) node;//      printf("tlist->node[i] = %p\ n", Tlist->node[i]);//The requested memory area is contiguous, but disjointed with the structure's memory tlist->length++;//      printf("%d\ n", tlist->length); }Else{ret =0; }returnRET;}intGet_cpacity_seqlist (seqlist* list) {tseqlist* Tlist = (tseqlist*)ListintRET =0;if(Tlist! = NULL)    {ret = tlist->capacity; }Else{ret =-1; }returnRET;}intGet_length_seqlist (seqlist* list) {tseqlist* Tlist = (tseqlist*)ListintRET =0;if(Tlist! = NULL) {ret = tlist->length; }Else{ret =-1; }returnRET;}intClear_seqlist (seqlist* list) {tseqlist* Tlist = (tseqlist*)ListintRET =0;if(Tlist! = NULL) {ret = tlist->length=0; }Else{ret =-1; }returnRET;} void Destory_seqlist (seqlist* list) {free (list);} seqlist* get_node_seqlist (seqlist* list,int POS) {tseqlist* Tlist = (tseqlist*)List seqlistnode* ret = NULL;if((NULL! = Tlist) && (POS>=0) && (POS<= tlist->length) {ret = (seqlistnode*)tlist->node[POS]; }returnRET;} seqlist* delet_node_seqlist (seqlist* list,int POS) {tseqlist* Tlist = (tseqlist*)List seqlistnode* ret = NULL;inti =0;if((NULL! = Tlist) && (POS>=0) && (POS< tlist->length) {ret = (seqlistnode*)tlist->node[POS]; for(i =POS+1; I < tlist->length; i++) {tlist->node[i-1] = tlist->node[i]; } tlist->length--; }returnRET;}
Main.c#include <stdio.h>#include <stdlib.h>#include "SeqList.h"intMain (void) {intCapacity =5;int* num = NULL;intA[] = {1,2,3,4,5}; seqlist* my_seqlist = NULL;//  printf("a[] = %p\ n", &a); My_seqlist = Creat_seqlist (5);//Create a linear tableif(NULL! = my_seqlist) {add_seqlist (my_seqlist, &a[0],0); Add_seqlist (My_seqlist, &a[1],1); Add_seqlist (My_seqlist, &a[2],2); Add_seqlist (My_seqlist, &a[3],3); Add_seqlist (My_seqlist, &a[4],4); }//view linear table capacityprintf("created linear table capacity = %d\ n", Get_cpacity_seqlist (my_seqlist));//View Linear table capacityprintf("created linear table length = %d\ n", Get_length_seqlist (my_seqlist));//View the value at the specified position num = get_node_seqlist (my_seqlist,3);printf("The value at the specified location is = %d\ n",*num); num = Delet_node_seqlist (my_seqlist,1);printf("The value at the specified location for deletion is = %d\ n",*num);system("Pause");return 0;}

Sequential implementation of linear tables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.