--C implementation of sequential storage structure for linear tables

Source: Internet
Author: User
Tags data structures printf
say in front

Data structures and algorithms are the soul of program design. Frankly speaking, I am weak in this respect can be. Although working for so many years, because of various excuses, this piece of knowledge has been a sore spot for me.

Once in the interview with the audacity to say that these knowledge is seldom used in the work, so the study of things early back to school. In fact, lost the soul of programmers like me, always want to attack.

So later in the study will have some like children's note-like articles appear in my blog, please do not laugh at the Masters, to carry. definition

Linear table can be said to be the simplest data structure, it is described as: n a finite sequence of data elements.

Recorded as: l= (A1,a2,..., an)

According to the storage structure, it can be divided into sequential storage structure and chained storage structure.

The sequential storage structure of the linear table is the simplest and most commonly used data structure: A sequential address is used to store the elements of the table in sequence.

See here, we will naturally associate to the C language of the array.

What I want to achieve is that the elements in the linear table are the sequential storage structure of integer type, and its main operation: adding and deleting.

Let's start with a simple definition of the sequential storage structure for this linear table:

#define MAXLENGTH

struct sequencelist
{
	int data[maxlength];
	int length;
};
Where the data array is the main part of the linear table, the elements exist in this array, and the operation of this linear table is based on this array.

Length is a property of this linear table, indicating that the linear table contains the number of elements. Increment: insert operation of linear table

The insertion of a linear table is the insertion of the data array, and then the length increment.

Insert opration
int Insert (struct sequencelist *list,int index,int Element)
{
	int length = list-> length;
	if (Length ==0 | | Index < 0 | | Index > Length | | length >= MAXLENGTH)
		return ERROR;
	List->data[index] = element;
	for (int i = length-1;i>index;i--)
	{
		list->data[i+1] = list->data[i];
	}	
	list->length++;
	return OK;
}
Delete: Deletion of a linear table

A similar increase in the opposite operation.

Delete opration
int delete (struct sequencelist *list,int index)
{
	int length = list->length;
	if (Length ==0 | | Index < 0 | | index > LENGTH-1)
		return ERROR;
	for (int i = index;i<length-1;i++)
	{
		List->data[i] = list->data[i+1];
	}
	List->data[length-1] = ' + ';//delete the last element.
	list->length--;
	return OK;
}
check: Take element operation of linear table

Finds the value of an element with an indexed value.

Get list elements
//make sure Elemet is not NULL when calling.
int getelement (struct sequencelist list,int index,int *element)
{
	printf ("\ngetelement\n");
	int length = List.length;
	printf ("Length is%d\n", length);
	if (Length ==0 | | Index < 0 | | Index >= length)
		return ERROR;
	*element = List.data[index];
	return OK;
}
It can be seen from the program that the time complexity of adding and deleting operations is 0 (n), so both operations are not its strengths. While the time complexity of the lookup operation is O (1), the advantage of the sequential storage structure of the linear table is that it can quickly remove elements from any position.

The above 3 kinds of operations as a more basic operation, is my study notes. If the prawns find something wrong, please advise them.

Other operations of the linear table, such as finding the precursor element, seeking the elements of the drive, determining if there is an element value in the table, emptying the operation, and so on, are still waiting for the idle time to complete.


A more complete example of debugging:

2013.2//lincoln//linear list//sequence Storage Structure//#include <stdio.h> #define OK 1 #define ERROR-1
	#define TURE 1 #define FALSE 0 #define MAXLENGTH struct sequencelist {int data[maxlength];
int length;

};
Get list Elements//make sure Elemet is not NULL when calling.
	int getelement (struct sequencelist list,int index,int *element) {printf ("\ngetelement\n");
	int length = List.length;
	printf ("Length is%d\n", length);
	if (Length ==0 | | Index < 0 | | Index >= length) return ERROR;
	*element = List.data[index];
return OK;
	}//insert opration//int insert (struct sequencelist *list,int index,int Element) {printf ("\ninsert\n");
	int length = list->length;
	printf ("Length is%d\n", length);
	if (Length ==0 | | Index < 0 | | Index > Length | | length >= MAXLENGTH) return ERROR;
	List->data[index] = element;
	for (int i = length-1;i>index;i--) {List->data[i+1] = list->data[i];
	} list->length++;
return OK; }//Delete opration//int Delete (struct sequencelist *list,int index) {printf ("\ndelete\n");
	int length = list->length;
	printf ("Length is%d\n", length);
	if (Length ==0 | | Index < 0 | | index > LENGTH-1) return ERROR;
		for (int i = index;i<length-1;i++) {printf ("delete data[%d]\n", i);
	List->data[i] = list->data[i+1];
	} List->data[length-1] = ' + ';//delete the last element.
	list->length--;
return OK;

	} int main () {struct Sequencelist list = {{3,1,5,7,12,78,34}, 7};
	printf ("List length:%d\n", list.length);
	Test get int *element = 0, test = 8;
	element = &test;
	if (ok = = GetElement (list,2,element)) {printf ("List get 2:%d\n", *element);
	//test Insert if (ok = = insert (&list,7,520)) {printf ("list insert 7 ok!\n");
	} if (ok = = GetElement (list,7,element)) {printf ("list get 7:%d\n", *element);
	} if (ok = = insert (&list,3,520)) {printf ("list insert 3 ok!\n"); } if (OK = = GetElement (list,3,element)) {PrinTF ("list get 3:%d\n", *element);
	}//test Delete if (ok = = Delete (&list,3)) {printf ("list Delete 3 ok!\n");
	} if (ok = = GetElement (list,3,element)) {printf ("list get 3:%d\n", *element);
	} if (ok = = Delete (&list,6)) {printf ("list Delete 6 ok!\n");
	} if (ok = = GetElement (list,6,element)) {printf ("list get 6:%d\n", *element);
	} else {printf ("list get error!\n");

 }
}




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.