The realization of single linked list in data structure + the realization of C language of single chain list and the source code

Source: Internet
Author: User

Linear table (list): A finite sequence of 0 or more data elements. The abstract data type of the linear table.

Sequential storage of linear tables:

Advantage: There is no need for additional storage space to represent the logical relationship of elements in a table, and you can quickly fetch elements from anywhere in the table.

Disadvantage: Insert and delete operations need to transfer a large number of elements, the length of the linear table is large, it is difficult to determine the storage space capacity, resulting in storage space "fragmentation."

Chain Storage for linear tables:

In order to represent the logical relationship between each data element A1 and its direct back-level data element ai+1, for the data element A1, in addition to storing its own information, it is necessary to store a signal that directs its immediate successor (that is, the immediate successor storage location). The domain where data element information is stored is called the data field, and the domain where the immediate successor is stored is called the pointer field. The information stored in the pointer field is called a pointer or chain. These two parts are composed of data element Ai's storage image, called node (node).

The difference between the head pointer and the head node:

1, the head pointer is to point to the first node of the list of pointers, if the linked list has all nodes, then point to the head node pointer. The head pointer has a marking function, so the common head pointer is called the list name. The head pointer is a necessary condition for a linked list, regardless of whether the linked list is empty and the head pointer is not empty.

2, the head node is for the operation of uniform and convenient settings, placed in the first element of the node before the data field is generally meaningless. With a head node, the insertion of the delete eraser to the first element position is consistent with the operation of the other nodes. The head node is not a necessary element of the list.

A pointer with a head node;

Empty linked list:

C language uses structure pointers to represent nodes:

typedef struct NODE
{	
	int data;
	struct Node *next;
} Node, *pnode,*linklist;

The abstract data structure of a single linked list is the operation:

void Initlist (linklist *list);//initialization operation, establish empty single link table
void clearlist (linklist *list);/purge single linked list.

void Listinsert (linklist *list,int i, int e);//The first position of the single linked list is inserted after the variable e
void Destorylist (linklist *list);//Destroy
list BOOL Listempty (linklist list)//To determine whether a single linked list is empty, return a true
void Getelem (linklist, int &e, int i);//Return the value of position I in a single linked list to the variable E
void Listdelete (linklist *list, int i, int &e);//delete single list table I position element
void Listtraverse (linklist list);/Traverse Linear table
int listlength (linklist list)//Returns the length of the linear table
void recursion (linklist list);//inverse recursive output single linked list

Various functions of the implementation of the source code:

void Initlist (linklist *list)
{
	*list = (linklist) malloc (sizeof (Node));
	(*list)->next = NULL;
	(*list)->data = 0;
}
void Listinsert (linklist *list, int i, int e)/(i) after the first element inserts e
{	
	linklist p = *list;
	
	if (i = = 0)
		{
			Pnode q = (linklist) malloc (sizeof (Node));
			Q->next = NULL;
			Q->data = e;
			P->next = q;
		}
	else
		{	
			Pnode p = (*list)->next;
			int j = 1;

			while (P && i > J)
			{
				p = p->next;
				++j;
			}

			Pnode q = (linklist) malloc (sizeof (Node));
			Q->next = p->next;
			Q->data = e;
			P->next = q;
		}
	
}
void Listtraverse (linklist list)
{
	Pnode p = list;
	if (P!= NULL)
	{
		p = p->next;
	}	
	Else 	
	{
		Return
	}
	while (p) {printf ("%d\t", p->data);
	p = p->next;
	Recursion (linklist list)//recursive method reverse Output list {if (null==list) {return;
	 } if (List->next!=null) {recursion (list->next);
printf ("%d\t", list->data);

	BOOL Listempty (linklist list) {Pnode p = list;
	if (NULL = = List->next) return true;
else return false;
	
	} void Getelem (linklist list, int &e, int i) {pnode p = list;
	if (I < 0 | | | i > listlength (list)) return;
	p = p->next;
	int j = 1;
			while (J < i) {p = p->next;
		j + +;
} e = p->data;
	} void Listdelete (linklist *list, int i, int &e) {Pnode p = *list;
	if (I < 0 | | | i > listlength (*list)) return;
	Pnode q = p;
	p = q->next;
	int j = 1;
			while (J < i) {q = p;
			p = p->next;
		j + +;
	} p->data = e;
	Q->next = p->next;
	

	
Free (p);
	int listlength (linklist list) {Pnode p = list;
	int i = 0;
	p = p->next;
			while (p) {p = p->next;
		i++;
return i;} void Clearlist (Linklist *list) {pnode p = *list;
	if (P!= NULL) p = p->next;
	Pnode q;
		while (p) {q = p;
		p = p->next;
	Free (q);
	} void Destorylist (Linklist *list) {pnode p = *list;
	if (P!= NULL) p = p->next;
	Pnode q;
		while (p) {q = p;
		p = p->next;
	Free (q);
Free (*list);
 }

To test the function source code:

int main ()
{
	linklist list;

	Initlist (&list);
	
	Listinsert (&list, 0, 1);
	Listinsert (&list, 1, 2);
	Listinsert (&list, 2, 3);
	Listinsert (&list, 1, 4);
	Listinsert (&list, 1, 5);
	Listinsert (&list, 5, 6);
	Listinsert (&list, 6, 7);
	Listinsert (&list, 7, 8);
	Listinsert (&list, 8, 9);
	Listinsert (&list, 9);

	printf ("\ n recursive call function reversion:\n");
	linklist p = list->next;
	Recursion (p);

	printf ("\ n Traversal list listtranverse:\n");
	Listtraverse (list);


	Int j = listlength (list);
	printf ("\ n the length of the linked list Listlength:%d\t", j);

	int e;
	Getelem (list, E,  3);
	printf ("Take the list-specific element Getelem:%d\n", e);

	Listdelete (&list, 1, e);
	printf ("\ n delete elements from a specific location in a list:%d\n", e);
	Listtraverse (list);

	Clearlist (&list);
	printf ("\ n list empty clearlist: \ n");
	Listinsert (&list, 0, 1);
	Listinsert (&list, 1, 2);
	Listinsert (&list, 2, 3);
	Listtraverse (list);


	Destorylist (&list);

	return 0;
}


Reference: Dahua data structure, data structure (C language Edition) Min edition



Related Article

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.