C language realization of linked list and its basic Operation __c language

Source: Internet
Author: User
Tags assert int size

Introduced

A linked list is a discontinuous, non sequential storage structure on a physical storage unit, and the logical order of data elements is realized by the sequence of pointers in the linked list. A list consists of a series of nodes (each of which is called a node), which can be generated dynamically at runtime. Each node consists of two parts: one is the data field where the data element is stored, and the other is the pointer field that stores the next node address.


and the Order table difference:

     Sequential tables use arrays to store linear elements that are characterized by random access, but because logically adjacent elements are physically adjacent, inserting deletes requires moving elements. The linked list uses a pointer chain to represent the logical relationship of the linear table elements, and insertions and deletions simply modify the pointer and cannot be accessed randomly.

Code implementation

typedef int DATATYPE;
The typedef struct NODE
{//structure realizes
	DataType data;
	struct node* next;   
Node, *pnode;


void Initlist (pnode* phead)//Initialize {assert (phead);
*phead = NULL;
	Pnode Byenode (DataType data)//Request a Node {pnode newNode = NULL;
	NewNode = (pnode) malloc (sizeof (Node));
		if (NULL = = NewNode) {printf ("Out of memory.\n");
	Exit (1);
		else {newnode->data = data;
	Newnode->next = NULL;
return newNode;
	} void Popback (pnode* phead)/tail Delete {assert (phead);
	if (NULL = = *phead) {return;
		else if (NULL = = (*phead)->next) {Pnode tempnode = *phead;
		Free (Tempnode);
		Tempnode = NULL;
	*phead = NULL;
		else {Pnode pcur = *phead;
		while (pcur->next->next) {pcur = pcur->next;
	} pcur->next = NULL;
	} void Pushback (pnode* phead, DataType data)//trailing interpolation {assert (Phead);
	if (NULL = = *phead) {*phead = Byenode (data);
		else {Pnode pcur = NULL;
		Pcur = *phead;
		while (pcur->next) {pcur = pcur->next;
	} Pcur->next = Byenode (data);
	} void Pushfront (Pnode *phead, DataType data)//header {assert (Phead); Pnode PreNode = NULL;
	Pnode Node = byenode (data);
	Prenode = *phead;
	Node->next = Prenode;
*phead = Node;
	} void Popfront (Pnode *phead)//head deletion {assert (phead);
	Pnode Prenode = *phead;
	if (NULL = = *phead) {return;
	else if (null = = *phead)->next {*phead = null;
		else {*phead = prenode->next;
		Free (Prenode);
	Prenode = NULL; }
}


Pnode Find (pnode* phead, DataType data)//lookup
{
	assert (phead);
	Pnode pcur = *phead;
	while (Pcur)
	{
		if (data = = Pcur->data) break
			;
		Pcur = pcur->next;
	}
	return pcur;
}

void Destroy (pnode* phead)//Destroy
{
	assert (phead);
	Pnode pcur = *phead;
	while (Pcur->next)
	{
		Pnode dnode = pcur;
		Pcur = pcur->next;
		Free (dnode);
		Dnode = NULL;
	}
}

int Empty (Pnode phead)//null
(
	null = = phead) return
		0;
	else return
		1;
}

int Size (Pnode phead)//Find the number of nodes in the list
{ 
	pnode node = phead;
	DataType num = 0;
	while (Node)
	{
		num++;
		Node = node->next;
	}
	return num;
}

void Printlist (pnode* phead)/print single linked list
{
	Pnode pcur = *phead;
	ASSERT (Phead);
	while (Pcur)
	{
		printf ("%d->", pcur->data);
		Pcur = pcur->next;
	}
	printf ("null\n");
}

void Insert (Pnode pos, DataType data)//Insert node
{
	Pnode newNode = byenode (data) after data;
	Pnode prenode = pos;
	Newnode->next = prenode->next;
	Prenode->next = NewNode;
}




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.