Linear data structure--sequential table

Source: Internet
Author: User
Tags assert data structures

Linear data structures can be divided into:


1. Sequential table: Continuous space, implemented by array (seqlist);


2. Linked list: Storage space is discontinuous, divided into: Unidirectional linked list, two-way linked list, circular chain list (linklist);

Comparison of sequential tables with linked lists:
1. Sequential table storage space is contiguous, easy to access specific elements, the storage space of the linked list is not contiguous, the array elements are connected with pointers, each data element
The element can only access one of the surrounding elements (one-way linked list);
2. List to access specific elements, only from the beginning of the linked list, until the traversal to the calcium element;
3. Add in order table, delete element time complexity is O (n), when inserting delete element in linked list, the movement of other elements is not involved, so the time complexity is O (1);

First, we introduce the implementation of sequential tables: dynamically open up memory space, create sequential tables


The specific code is as follows:

#ifndef _seqlist_d_h__//prevents header files from being redefined #define _SEQLIST_D_H__ #include//referenced library functions #include #include #include #define default_s Z 3//The default size of the dynamic sequential table #define DEFAULT_INC 2//default capacity size typedef int DATATYPE;//data type definition typedef struct SEQLIST//Define Order TABLE element {Data
   Type* P_data;
   int sz;
int capacity;
}seqlist,*pseqlist;
void Initseqlist (pseqlist PS);//Initialize Order table void pushback (pseqlist ps,datatype x);//tail delete void Popback (pseqlist PS);//Trailing element deletion void Pushfront (pseqlist ps,datatype x);//header insert void Popfront (pseqlist PS);//Header delete void sort (pseqlist PS);//Sort void reserve ( Pseqlist PS);//Reverse int binarysearch (pseqlist ps,int left,int right,int key);//binary lookup of sorted order table void Insert (Pseqlist ps,int p
Os,datatype x);//Specify the location to insert void Remove (pseqlist ps,datatype x);//specify element to delete void RemoveAll (pseqlist ps,datatype x);//Specify all elements to delete void Erase (Pseqlist ps,int POS);//specify location to delete int find (const pseqlist ps,datatype x,int start);//find specified element void Display (const PSEQ list PS);//print void Destory (pseqlist PS) for sequential table, or//Destroy order table #endif//_seqlist_d_h__#include "SeqliSt_d.h "void Initseqlist (Pseqlist PS) {ps->p_data= (DataType *) malloc (default_sz*sizeof (DataType));
		if (ps->p_data==null) {perror ("Initseqlist ():: malloc");
	Exit (1);
	} ps->sz=0;
	ps->capacity=default_sz;
memset (ps->p_data,0,ps->capacity*sizeof (DataType));
	} void Checkcapacity (Pseqlist PS) {assert (PS);
	   if (ps->sz==ps->capacity) {DataType *p=realloc (Ps->p_data, (default_sz+default_inc) *sizeof (DataType));
		   if (p!=null) {ps->p_data=p;
	       ps->capacity+=default_inc;

	   memset (ps->p_data+ps->sz,0,default_inc*sizeof (DataType));
		   } else {perror ("checkcapacity ():: ReAlloc");
	   Exit (1);
	}}} void Pushback (Pseqlist ps,datatype x)//trailing Insert {ASSERT (PS);
	Checkcapacity (PS);
	ps->p_data[ps->sz]=x;
ps->sz++;
	} void Popback (Pseqlist PS) {if (ps->sz==0) {return;
} ps->sz--;
	} void Pushfront (pseqlist ps,datatype x)//header insert {int i=0;
	ASSERT (PS);
	Checkcapacity (PS); for (i=ps->sz;i>0;i--) {ps->p_data[i]=ps->p_data[i-1];
	}//memset (Ps->p_data+1,ps->p_data,ps->sz*sizeof (DataType)) ps->p_data[0]=x;
Ps->sz + +;
	} void Popfront (pseqlist PS)//head Delete {int i=0;
	ASSERT (PS);
	if (ps->sz==0) {return;
	} for (i=0;isz;i++) {ps->p_data[i]=ps->p_data[i+1];
    
} ps->sz--;
	} void Sort (pseqlist PS)//Sort {int i=0;
	int j=0;
	ASSERT (PS);
	if ((Ps->sz) <2) {return; } for (i=0;isz-1;i++) {for (j=0;jsz-1-i;j++) {if (ps->p_data[j]>ps->p_data[j+1]) {DataType Tmp=ps
				->p_data[j];
				ps->p_data[j]=ps->p_data[j+1];
			ps->p_data[j+1]=tmp;
	}}}} int BinarySearch (pseqlist ps,int left,int right,datatype key)//binary search for sorted order table {assert (PS);
		while (left<=right) {int mid=left+ ((right-left) >>1);
        if (Ps->p_data[mid]>key) {right=mid-1;
		} else if (ps->p_data[mid]sz;i++) {if (ps->p_data[i]==x) {return i;
}} return-1; } void Insert (Pseqlist ps,int Pos,datatype x)//Specify position insert {int i=0;
	ASSERT (PS);
	Checkcapacity (PS);
	for (i=ps->sz;i>pos;i--) {ps->p_data[i]=ps->p_data[i-1];
	} ps->p_data[pos]=x;
ps->sz++;
	} void Erase (Pseqlist ps,int POS)//Specify location to delete {int i=0;
	ASSERT (PS);
	if (ps->sz==0) {return; } if ((pos<0) && (Pos>ps->sz)) {printf ("The specified location is incorrect.
	\ n ");
	} for (i=pos;isz;i++) {ps->p_data[i]=ps->p_data[i+1];

} ps->sz--;
	} void Remove (pseqlist ps,datatype x)//Specify element to delete {int pos=0;
	ASSERT (PS);
	Pos=find (ps,x,0); if (pos==-1) {printf ("The specified element does not exist.
	\ n ");
		} else {int i=0;
		for (i=pos;isz;i++) {ps->p_data[i]=ps->p_data[i+1];
	} ps->sz--;
	
	}} void RemoveAll (pseqlist ps,datatype x)//Specify all elements to delete {int pos=0;
	int start=0;
	ASSERT (PS);
	    while (Startsz) {pos=find (Ps,x,start); if (pos==-1) {printf ("The specified element does not exist.
			\ n ");
		Return
			} else {start=pos;
			Pos=find (Ps,x,start);
          Erase (Ps,pos); }}} void Reserve (PSEqlist PS)//reverse {int left=0;
	int right=ps->sz-1;
	ASSERT (PS);
        while (Leftp_data[left];
		ps->p_data[left]=ps->p_data[right];
		ps->p_data[right]=tmp;
		left++;
	right--;
	}} void Display (const pseqlist PS) {int i=0;
	ASSERT (PS);
	if (ps->sz==0) {return;
	} for (i=0;isz;i++) {printf ("%d", ps->p_data[i]);

} printf ("\ n");
	} void Destory (Pseqlist PS) {free (ps->p_data);
	ps->p_data=null;
	ps->capacity=0;
ps->sz=0;
 }
#include "seqlist_d.h" void Initseqlist (pseqlist PS)//initialization of the sequential table {ps->p_data= (DataType *) malloc (default_sz*sizeof (
	DataType));
		if (ps->p_data==null) {perror ("Initseqlist ():: malloc");
	Exit (1);
	} ps->sz=0;
	ps->capacity=default_sz;
memset (ps->p_data,0,ps->capacity*sizeof (DataType));
	} void Checkcapacity (pseqlist PS)//detection capacity {ASSERT (PS);
	   if (ps->sz==ps->capacity) {DataType *p=realloc (Ps->p_data, (default_sz+default_inc) *sizeof (DataType));
		   if (p!=null) {ps->p_data=p;
	       ps->capacity+=default_inc;

	   memset (ps->p_data+ps->sz,0,default_inc*sizeof (DataType));
		   } else {perror ("checkcapacity ():: ReAlloc");
	   Exit (1);
	}}} void Pushback (Pseqlist ps,datatype x)//trailing Insert {ASSERT (PS);
	Checkcapacity (PS);
	ps->p_data[ps->sz]=x;
ps->sz++;
	} void Popback (Pseqlist PS) {if (ps->sz==0) {return;
} ps->sz--;
	} void Pushfront (pseqlist ps,datatype x)//header insert {int i=0;
	ASSERT (PS); ChecKcapacity (PS);
	for (i=ps->sz;i>0;i--) {ps->p_data[i]=ps->p_data[i-1];
	}//memset (Ps->p_data+1,ps->p_data,ps->sz*sizeof (DataType)) ps->p_data[0]=x;
Ps->sz + +;
	} void Popfront (pseqlist PS)//head Delete {int i=0;
	ASSERT (PS);
	if (ps->sz==0) {return;
	} for (i=0;isz;i++) {ps->p_data[i]=ps->p_data[i+1];
    
} ps->sz--;
	} void Sort (pseqlist PS)//Sort {int i=0;
	int j=0;
	ASSERT (PS);
	if ((Ps->sz) <2) {return; } for (i=0;isz-1;i++) {for (j=0;jsz-1-i;j++) {if (ps->p_data[j]>ps->p_data[j+1]) {DataType Tmp=ps
				->p_data[j];
				ps->p_data[j]=ps->p_data[j+1];
			ps->p_data[j+1]=tmp;
	}}}} int BinarySearch (pseqlist ps,int left,int right,datatype key)//binary search for sorted order table {assert (PS);
		while (left<=right) {int mid=left+ ((right-left) >>1);
        if (Ps->p_data[mid]>key) {right=mid-1;
		} else if (ps->p_data[mid]sz;i++) {if (ps->p_data[i]==x) {return i; }
	} return-1;
	} void Insert (Pseqlist ps,int pos,datatype x)//Specify position insert {int i=0;
	ASSERT (PS);
	Checkcapacity (PS);
	for (i=ps->sz;i>pos;i--) {ps->p_data[i]=ps->p_data[i-1];
	} ps->p_data[pos]=x;
ps->sz++;
	} void Erase (Pseqlist ps,int POS)//Specify location to delete {int i=0;
	ASSERT (PS);
	if (ps->sz==0) {return; } if ((pos<0) && (Pos>ps->sz)) {printf ("The specified location is incorrect.
	\ n ");
	} for (i=pos;isz;i++) {ps->p_data[i]=ps->p_data[i+1];

} ps->sz--;
	} void Remove (pseqlist ps,datatype x)//Specify element to delete {int pos=0;
	ASSERT (PS);
	Pos=find (ps,x,0); if (pos==-1) {printf ("The specified element does not exist.
	\ n ");
		} else {int i=0;
		for (i=pos;isz;i++) {ps->p_data[i]=ps->p_data[i+1];
	} ps->sz--;
	
	}} void RemoveAll (pseqlist ps,datatype x)//Specify all elements to delete {int pos=0;
	int start=0;
	ASSERT (PS);
	    while (Startsz) {pos=find (Ps,x,start); if (pos==-1) {printf ("The specified element does not exist.
			\ n ");
		Return
			} else {start=pos;
			Pos=find (Ps,x,start);
   Erase (Ps,pos);       }}} void Reserve (Pseqlist PS)//reverse {int left=0;
	int right=ps->sz-1;
	ASSERT (PS);
        while (Leftp_data[left];
		ps->p_data[left]=ps->p_data[right];
		ps->p_data[right]=tmp;
		left++;
	right--;
	}} void Display (const pseqlist PS) {int i=0;
	ASSERT (PS);
	if (ps->sz==0) {return;
	} for (i=0;isz;i++) {printf ("%d", ps->p_data[i]);

} printf ("\ n");
	} void Destory (Pseqlist PS) {free (ps->p_data);
	ps->p_data=null;
	ps->capacity=0;
ps->sz=0;
 }







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.