Linked list creation, deletion, insertion of basic operations

Source: Internet
Author: User
Tags data structures printf

In the data structure, the linked list is undoubtedly the most basic, but also in most IT companies interview written tests to examine the most; with a solid basis for dealing with lists, it is very helpful and necessary to learn more complex types of data structures in the future, so in the leisure time, again rewrite the basic operation of the list, Also refer to some information, here to share the code, hoping to help beginners.


In the beginning, for the establishment of the linked list, will write this way, basically will use two times the malloc function call. The code is roughly as follows.

Create a single-linked list of length n
node *create (int n)
{
	node *head,*precious,*current;
	int cnt;
	int label = 1;
	for (cnt = 0;cnt < n;cnt++)
	{
		if (label = = 1)
		{
			Precious = (node *) malloc (sizeof (node
			)); printf ("Input The information just like:name ID gender age \ n");
			scanf ("%s%s%s%d", precious->name,precious->id,precious->s,&precious->age);
			Precious->link = NULL;
			head = precious;
			label = 0;
		}
		else
		{Current
			= (node*) malloc (sizeof (node));
			printf ("Input The information just like:name ID gender age \ n");
			scanf ("%s%s%s%d", current->name,current->id,current->s,¤t->age);			current->link = NULL;
			Precious->link = current;
			Precious = current;
		}		
	}
	Precious->link = NULL;
	return head;
}


Later read some information, simplified a lot, the following writing is worth advocating:

Create a linked list
node *creatlink (int n)
{
	node *head,*p,*q;

	for (int i=1; i<n+1; i++)
	{
		p = (node*) malloc (sizeof (node));
		printf ("Input a number:");
		scanf ("%d", &p->data);

		if (i = = 1) head = p;
		else Q->next = p;

		Q = p;
		P->next = NULL; 
	}


	return head;
}


And then to mention is the insertion and deletion of the operation: there are two points to note: First of all, whether inserting or deleting the first thing to do is to find the location to operate, and secondly, the position of the operation is divided into three kinds of situations, namely, the head node, the middle node, the tail node.

The following code refers to some information (such as the programmer interview, rectification book)

Inserting nodes

Node *insert (node *head,int num)
{
	node *p,*q,*s;
	s = (node*) malloc (sizeof (node));
	printf ("Input a number:");
	scanf ("%d", &s->data);
	p = head;
	First find the appropriate location to process, assuming that the data is sorted in ascending order while
	(P->data < s->data && P->next! = NULL)
	{
		q = p; p = p->n ext;
	}

	if (p = = head) {head = s; p = s->next;}
	else if (P->next = = null) {P->next = s; s->next = null;}
	else {q->next = s; s->next = P;}

	return head;
}

Delete a node: (You can combine the tail node with the middle node step).

Node *delete (node *head,int num)
{
	node *p,*q;
	p = head;
	First find and then delete
	while (p->data! = num && p->next! = NULL)
	{
		q = p; p = p->next;
	}

	if (num = = p->data)
	{
		if (p = = head) {head = p->next; free (p);}
		else {q->next = p->next; free (p);}
	}
	else
		printf ("%d could not been found", num);

	return head;
}

Finally, the simple introduction of the list of list sorting, list sorting, focus on the order, the pointer to the operation of less, and the order is nothing more than a few classic sorting algorithms, such as bubbling, direct, select and so on. Judging by my personal experience, sorting is also a key point in the interview test, and can almost be asked.

The following is a list of the sort that is written in bubbling notation

Node *sort (node* head,int length)
{
	node *p;
	

	for (int i = 1; i < length; i++)
	{
		p = head;
		for (int j = 0; J < Length-i; J + +)
		{
			if (P->data > P->next->data)
			{
				int tmp = p->data;< C10/>p->data = p->next->data;
				P->next->data = tmp;
			}
			p = p->next;
		}
	}

		return head;
}

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.