Data structure of the cycle of single linked list (C language implementation)

Source: Internet
Author: User

Single linked list of data structure (C language Implementation)

This cycle of single linked list, circular single linked list and ordinary single linked list no big changes, only the last node's pointer field points to the head node, so there is a difference in the operation of the circular single linked list, which is mainly to determine whether the linked list arrives at the end node and becomes the address of the head node.


Circular single linked list features and ordinary single linked list is no different, the last blog has been written in detail, here will not repeat.



Then we introduce the common operations of three kinds of lists from three aspects:

1. The initialization of the linked list

2. Apply for a linked list node

3. The head Insert method of the linked list

4. The trailing interpolation method of the linked list

5. Get the linked list length

6. Delete Linked list node

7. Find the node for the specified value

8. Destroy the linked list (free the memory space of all nodes in the list)

9. Output single linked list (output single linked list all node data field)


Note: The realization of the following single linked list is the data field with integer type as an example, and with a head node.


One, ordinary single linked list

1. The structure of single linked list

typedef struct _CNODE//C as Circle

{

int data;//data field

struct _node* next;//pointer field

}cnode, *clist;


2. The operation of the linked list

(1). Initialization of the linked list (lead node)

Initialization here refers to the pointer field of the initialization header node

void Initlist (List plist)
{
	if (NULL = = plist) return
		;
	Plist->next = plist;
}


(2). Apply for a linked list node

Request a node from the heap, note that this is the memory requested from the heap and can only be explicitly freed by free (p). Even a local variable, the memory is not freed as the function call completes.

static node* buynode (int val)
{
	node* ptmp = (node*) malloc (sizeof (Node));
	Ptmp->next = ptmp;
	Ptmp->data = val;
	return ptmp;
}


(3). Linked-List head interpolation method

The list here has a header node, so each newly inserted node should be inserted behind the head node.

void Inserthead (CList plist, int val)
{
	Node *ptmp = Buynode (val);
	Ptmp->next = plist->next;
	Plist->next = ptmp;
}


(4). The tail-connection method of the linked list

Each time the newly inserted node is inserted behind the last node, the tail node is first found when inserting the node with the tail method

void Inserttail (CList plist, int val)
{
	Node *ptmp = plist;
	while (Ptmp->next!= plist)
	{
		ptmp = ptmp->next;
	}
	Node *pcur = Buynode (val);
	Pcur->next = ptmp->next;
	Ptmp->next = pcur;
}


(5). Get the length of the linked list

The list is traversed, each node is traversed, the counter plus one.

int Getlistlen (List plist)
{
	Node *pcur = plist->next;
	int icount = 0;
	while (plist!= pcur)
	{
		++icount;
		Pcur = pcur->next;
	}
	return icount;
}


(6). Delete Linked list node

When you delete a linked list node for a specified value, you need to traverse the list, find the corresponding node, and then want to delete the node, you must know the node's predecessor, so that the node can be deleted correctly.

BOOL Delete (CList plist, int key)
{
	Node *ppre = plist;
	Node *pcur = plist->next;


	while (Pcur!= plist)
	{
		if (pcur->data!= key)
		{			
			ppre = pcur;
			Pcur = pcur->next;
		}
		else
		{
			Ppre->next = pcur->next;
			Free (pcur);
			return true;
		}
	return false;
}


(7). Find the node for the specified value

The node looking for the specified value also needs to traverse the list from beginning to end, returning the node if found, and returning null if it is not found.

node* Search (CList plist, int key)
{
	Node *pcur = plist->next;
	while (Pcur!= plist)
	{
		if (Pcur->data = = key)
		{return
			pcur;
		}
		Pcur = pcur->next;
	}
	return NULL;
}


(8). Destruction Linked List

Destroying a linked list is the memory that frees all the nodes in the list.

void Destroy (CList plist)
{
	Node *pcur = plist->next;
	while (Pcur!= plist)
	{
		plist = pcur->next;
		Free (pcur);
		Pcur = plist;
	}


(9). Output single Linked list

Output single linked list operation is also relatively simple, from beginning to end traversing a single linked list, each traversal of a node to output the pointer field of the node


void Showlist (CList plist)
{
	Node *p = plist->next;
	while (P!= plist)
	{
		printf ("%5d", p->data);
		p = p->next;
	}
	printf ("\ n");
}




Finally attach the complete code and run the results:

Link.h
#include <stdio.h>
#include <stdlib.h>

typedef struct _NODE
{
	int data;
	struct _node* next;
Node, *list;

void Initlist (List plist);
void Inserthead (List plist, int val);
void Inserttail (List plist, int val);
BOOL Delete (List plist, int val);
node* Search (List plist, int val);
int Getlistlen (List plist);
void Destroy (List plist);
static node* buynode (int val);
void Show (List plist);

LINK.C #include "test.h" int main () {Node head;
	Initlist (&head);
	for (int i = 0; i < ++i) {Inserttail (&head, I);//Trailing Insert method} showlist (&head);
	
	printf ("Linked list length:%d\n", Getlistlen (&head));
	for (int i = 0; i < ++i) {Inserthead (&head, I);//Header method} showlist (&head);
	printf ("Linked list length:%d\n", Getlistlen (&head));
	printf ("Search 12:\n");	Node *p = Search (&head, 12);
	Lookup node if (P!= NULL) {printf ("%d\n", p->data);
	else {printf ("not found\n");	printf ("Delete node 12:\n");
	Deletes the node if (delete (&head)) {showlist (&head);
	else {printf ("No this node in the list \ n");		} Destroy (&head);
Destruction linked list return 0;
	} void Initlist (List plist) {if (NULL = = plist) return;
Plist->next = NULL;
	} void Inserthead (List plist, int val) {Node *ptmp = Buynode (val);
	Ptmp->next = plist->next;
Plist->next = ptmp;
	} void Inserttail (List plist, int val) {Node *ptmp = Buynode (val);
	Node *pcur; for (pcur = plist; NULL!= pcur->next;		Pcur = Pcur->next) {;
Empty statement} Pcur->next = ptmp;
	BOOL Delete (List plist, int val) {node* ppre = plist;
	node* pcur = plist->next;
			while (NULL!= pcur) {if (Pcur->data!= val) {ppre = Pcur;
		Pcur = pcur->next;
			else {Ppre->next = pcur->next;
			Free (pcur);
		return true;
return false;
	} node* Search (List plist, int val) {Node *pcur = plist->next;
		while (NULL!= pcur) {if (Pcur->data = val) {return pcur;
	} pcur = pcur->next;
return NULL;
	int Getlistlen (List plist) {Node *ptmp = plist->next;
	int icount = 0;
		while (NULL!= ptmp) {++icount;
	Ptmp = ptmp->next;
Return icount;
	} void Destroy (List plist) {node* pcur = plist->next;//Note the memory space of the back node of the destroyed list is still present, that is, the empty list is a single linked table with only one header node (NULL!= pcur)
		{plist = pcur->next;
		Free (pcur);
	Pcur = plist->next;
	} static node* Buynode (int val) {node* ptmp = (node*) malloc (sizeof (Node)); Ptmp->next = NULL;
	Ptmp->data = val;
return ptmp;
	} void Showlist (List plist) {node* pcur = plist->next;
		while (NULL!= pcur) {printf ("%5d", pcur->data);
	Pcur = pcur->next;
printf ("\ n"); }



Run Result:


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.