C-language implementation and basic operation of one-way linked list __c language

Source: Internet
Author: User
Tags sprintf

The main contents of this article:

C language realization of one-way linked list

Second, the basic operation of one-way linked list


C language realization of one-way linked list

As a basic data structure, the linked list is often used in the process of program development. For C language, the realization of the linked list depends mainly on the structure and the pointer, so the relevant content and program need to have the base of the pointer and structure in C language.

The linked list is a kind of linear storage data structure, the storage content is logically continuous, but not necessarily continuous in physics. One-way linked list consists of a chain header (head) and a number of linked list elements (node), the basic operation of the linked list is to add, delete, change, check.

First of all, the method of C language implementation of one-way linked list is introduced. To implement a one-way linked list, first define a struct body:

/* Define a structural body pointer
/struct LIST {
	int ID that represents the linked list;			/* Identify this element to facilitate the search * *
	char data[20];		/* The elements contained in the list
	/* struct list *next;	/* point to the next list of pointers
/};
Here's how to write a program to implement a list of basic operations. The function of the program is to first assign a number of linked list elements (node), and then the elements of these linked list to assign initial value, after the initial value of these linked list elements are added to the list, and finally the data fields of these elements are printed out in turn. (Note: This program implements the Chain header node (head) in which the data is stored; In order to ensure that the ID fields of each element are different, a global static data member list_id is defined and compiled using GCC under Linux). The procedure is as follows:

/* Contains header file/* * #include <stdio.h> #include <stdlib.h>/* Define a structure pointer/struct list {int id that represents the linked list;		/* Identify this element to facilitate the search * * char data[20];	/* The elements contained in the list/* struct list *next;

/* point to the next list of pointers/};

/* Define a linked list head/static struct list *list_head = NULL;

/* To ensure that each linked list element ID is different, deliberately define the ID as a global static variable/static int list_id = 0; /** inserts the specified element at the end of the chat table. * Head: The address of the header of the linked list to which you want to insert the element * List: Represents the element to be inserted into the list/static void List_add (struct list **head, str

	UCT list *list) {struct list *temp;
		/* To determine whether the list is empty */if (NULL = = *head) {/* is empty/* *head = list;
	(*head)->next = NULL;
		else {/* is not empty */temp = *head;
				while (temp) {if (NULL = = temp->next) {temp->next = list;
			List->next = NULL;
		\ temp = temp->next; }}/** traverses a linked list that prints the data contained in each element of the list * head: pointer to the header of the linked list to traverse/static void List_print (struct list **head) {struct LI

	St *temp;

	temp = *head;
	printf ("List information: \ n"); while (temp) {printf ("\tlist%d:%s\n", Temp->id, Temp->daTA);
	temp = temp->next;
	}/* Main function, the entry of the program/INT main (int argc, char *argv[]) {int i = 0;

	struct list *lists = NULL;
	/* Allocate 10 elements */lists = malloc (sizeof (struct list) * 10);
		if (NULL = = lists) {printf ("malloc error!\n");
	return-1;
		/* * The allocated 10 elements are populated sequentially with the data and added to the list * for (i = 0; i < i++) {lists[i].id = list_id++;

		sprintf (Lists[i].data, "Tech-pro-%d", i);
	List_add (&list_head, &lists[i]);

	/* Traverse the list, the linked list of each element of the information is printed out/List_print (&list_head);
return 0; }
The results of the program's operation are as follows:


Second, the basic operation of one-way linked list

The basic operation of the linked list is actually the operation of the elements in the linked list. The basic operation of the linked list includes the addition, deletion, modification and view of the node elements in the linked list, simply adding, deleting, changing and checking.

2.1 The elements of one-way linked list added

Add elements in a linked list first to determine whether the linked list is empty, directly to add the element to the head of the linked list, no one will traverse the list to find the next empty node, and then insert the element to the tail of the chain.

To implement the program code for a linked list insert:

/**	Inserts the specified element at the end of the chat table
  * 	head: The address of the	header of the linked list to insert the element
  *	List    : Represents the element to be inserted into the list/
static void List_add (struct list **head, struct list *list)
{
	struct list *temp;

	/* To determine whether the list is empty *
	/if (NULL = = *head)
	{/
		* is empty/*
		*head = list;
		(*head)->next = NULL;
	}
	else
	{
		/* is not empty *
		/temp = *head;
		while (temp)
		{
			if (NULL = = Temp->next)
			{
				temp->next = list;
				List->next = NULL;
			}
			temp = temp->next;
		}}}

The field code in the main function is as follows:
struct list temp_list;

/* Fill this structure and add in the list * *
	temp_list.id = list_id++;
	sprintf (Temp_list.data, "temp_list");
	List_add (&list_head, &temp_list);
The results of compiling the program are as follows:

2.2 Delete elements in a one-way list

Deleting an element node in a linked list is relatively complex in the underlying operation of the chain list. First you need to determine if the list is empty, when the list is not empty, again to determine whether the element to be deleted is the head node, if the next element is assigned directly to the header node, but not the need to traverse the entire list to find the node element to delete. (Note: The deletion of this algorithm is done through the ID field of the element node).

Delete the program code for the linked list node:

/**	Deletes the specified element from the end of the list
  *	head: The address * ID representing the header of the linked list to delete the element      : Represents the identity of the element to be deleted
  *	return value  : 0-Successful ,-1-failure
/static int List_del (struct list **head, int id)
{
	struct list *temp, *p;
	temp = *head;

	if (NULL = temp)
	{/
		* list is null
		/printf ("List is empty!\n");
		return-1;
	}
	else
	{/
		* to determine whether the matching element is the element of the head of the list * * *
		/if (id = = temp->id)/* is the head of the list/
		{
			*head = Temp->next ;
			return 0;
		}
		else					/* is not a list
		head
			/{while (Temp->next)
			{
				p = temp;
				temp = temp->next;

				if (id = = Temp->id)
				{
					P->next = temp->next;
					return 0;
				}
			}	
			Return-1
		}
	}

	return-1;
}
Add the following code to the main function:
/* Delete the starting position in the list, the middle position, the tail element * *
	List_del (&list_head, 0);
	List_del (&list_head, 5);
	List_del (&list_head, 10);
The results of compiling the program are as follows: Elements No. 0, 5, and 10th are deleted.


2.3 Modify the specified element in a one-way list

Modify the elements in the linked list, by traversing the elements in the linked list, find the elements to be modified to modify the content, and realize the modification of the linked list elements is relatively easy. (Note: The positioning of the element to be modified is done through the ID field)

The procedures for modifying elements in a linked list are as follows:

/**	modifies the contents of the element defined by the specified ID
  *	head: The address * ID representing the header of the linked list to change the element	: Indicates the identity of the      element to be changed
  *	content : Indicates content to change
  *	return value  : 0-Success,-1-failure
/static int list_chg (struct list **head, int id, char *content) c14/>{
	struct list *temp;

	temp = *head;	/* Assign the header of the list to the temporary chat variable/while

	(temp)/* To poll the list/
	{
		if (id = = Temp->id)
		{
			memset (temp- >data, 0, sizeof (temp->data));
			sprintf (Temp->data, "%s", content);
			Temp->data[strlen (content)] = ';
			return 0;
		}
		temp = temp->next;
	}
	return-1;
}
Add the following code to the main function:
/* Changing the value of the element with ID 4 is "Change!!!" * *
	List_chg (&list_head, 4, "Changes!!!");
To compile the program, the results of the run are as follows:


2.4 Query operation on the elements of one-way linked list

Query an element in the list, and then traverse the linked list according to the head of the linked list.

The program code for the query in the linked list:

/** the	contents of the element defined by the specified ID.
  *	head: The address * ID representing the header of the linked list to query elements      : Represents the Identity
  *	return value of the element to query  : 0-Success,-1-failure
/static int list_query (struct list **head, int id)
{
	struct list *temp;

	temp = *head;	/* Assign the header of the list to the temporary chat variable/while

	(temp)/* To poll the list		* *
	{
		if (id = = Temp->id)
		{
			printf ("list %d:%s\n ", Temp->id, temp->data);
			return 0;
		}
		temp = temp->next;
	}

	/* No element found
	/printf ("Not finding!\n");
	
	return-1;
}
Program Run Result:



Appendix: The basic operation of one-way linked list has been finished, and a complete procedure is attached.

/* Included header file/* #include <stdio.h> #include <stdlib.h> #include <string.h>/* Define a structure pointer that represents the linked list/struct LIS				t {int id;		/* Identify this element to facilitate the search * * char data[20];	/* The elements contained in the list/* struct list *next;

/* point to the next list of pointers/};

/* Define a linked list head/static struct list *list_head = NULL;

/* To ensure that each linked list element ID is different, deliberately define the ID as a global static variable/static int list_id = 0; /** inserts the specified element to the end of the list. * Head: The address of the header of the linked list to insert the element * List: Represents the element to be inserted into the list/static void List_add (struct list **head, str

	UCT list *list) {struct list *temp;
		/* To determine whether the list is empty */if (NULL = = *head) {/* is empty/* *head = list;
	(*head)->next = NULL;
		else {/* is not empty */temp = *head;
				while (temp) {if (NULL = = temp->next) {temp->next = list;
			List->next = NULL;
		\ temp = temp->next; }}/** traverses a linked list that prints the data contained in each element of the list * head: pointer to the header of the linked list to traverse/static void List_print (struct list **head) {struct LI

	St *temp;

	temp = *head;
	printf ("List information: \ n"); while (temp) {printf ("\tlist%d:%s\n", Temp->id, Temp->data);
	temp = temp->next; /** removes the specified element from the end of the list * head: Address * ID representing the header of the linked list to delete the element: Indicates the identity of the element to be deleted * Return value: 0-Successful,-1-failure/static int List_del
(struct list **head, int id)
	{struct list *temp, *p;

	temp = *head;
		if (NULL = temp) {/* list is null/printf ("List is empty!\n");
	return-1;
			else {/* To determine whether the matching element is the element of the linked list head */if (id = = temp->id)/* is the head of the list/{*head = temp->next;
		return 0;
				else//is not a list head */{while (temp->next) {p = temp;

				temp = temp->next;
					if (id = = temp->id) {P->next = temp->next;
				return 0;
		}} return-1;
}} return-1; /** to modify the content defined by the element of the specified ID * head: The address * ID representing the header of the linked list to change the element: Indicates the identity of the element to be changed * content: Indicates what to change * return value: 0-Successful,

	Failed/static int list_chg (struct list **head, int id, char *content) {struct list *temp;	temp = *head; /* Assign the header of the list to the temporary chat variable/while (temp)/* To poll the list/{if (id = = Temp->id) {memset (temp->data, 0, sizeof (temp->data));
			sprintf (Temp->data, "%s", content);
			Temp->data[strlen (content)] = ';
		return 0;
	\ temp = temp->next;
} return-1; /** to find the content defined by the element that specifies the ID * head: The address * ID representing the header of the linked list to query the element: Indicates the identity of the element to query * return value: 0-Successful,-1-failed/static int list

	_query (struct list **head, int id) {struct list *temp;	temp = *head;  /* Assign the header of the list to the temporary chat variable/while (temp)/* To poll the list/{if (id = = temp->id) {printf ("List%d:%s\n", Temp->id,
			Temp->data);
		return 0;
	\ temp = temp->next;
	
	/* No elements found/printf ("Not finding!\n");
return-1;
	}/* Main function, the entry of the program/INT main (int argc, char *argv[]) {int i = 0;

	struct list *lists = NULL;

	struct list temp_list;
	/* Allocate 10 elements */lists = malloc (sizeof (struct list) * 10);
		if (NULL = = lists) {printf ("malloc error!\n");
	return-1;
		/* * The allocated 10 elements are populated sequentially with the data and added to the list * for (i = 0; i < i++) {lists[i].id = list_id++;

		sprintf (Lists[i].data, "Tech-pro-%d", i); List_add (&amP;list_head, &lists[i]);
	* * Fill this structure and add the list/temp_list.id = list_id++;
	sprintf (Temp_list.data, "temp_list");

	List_add (&list_head, &temp_list);
	/* Delete the starting position in the list, the middle position, the tail element * * List_del (&list_head, 0);
	List_del (&list_head, 5);

	List_del (&list_head, 10);

	/* Changing the value of the element with ID 4 is "Change!!!" * * LIST_CHG (&list_head, 4, "Changes!!!");

	/* The content of the element node with ID 4 in the query list/* List_query (&list_head, 4);
return 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.