A method of inserting a new node in a sequential single linked list

Source: Internet
Author: User

When we learn the list, we all have contact with the problem of inserting a new node into a single linked list. One of them is to insert a new node in the sequential list, and keep the list of incremental or decreasing properties.

A recent reading of "C" and "pointers" refers to a method that I personally feel is good and thought very well.

This is the most common thinking:

sll_node.h

typedef struct node
{
	int value;
	Node *next;
} Node;
#include "sll_node.h"
#include <stdlib.h>

#define	TRUE	1
#define	FALSE	0

/ /Insertnode: Inserts the value of the newvalue into the sorted list, returns true correctly, error returns false
//ROOTP is the head pointer of the linked list.
int Insertnode (Node **rootp, int newvalue)
{node *newnode;//new node pointer node *previous/////Current pointer node
	*current;//
	current pointer

	= *ROOTP;//early Initiation
	previous = NULL;

	Find where to insert while
	(current!= NULL && current->value < NewValue)
	{
		previous = current;
		Current = current->next;
	}

	Allocate space to new nodes
	NewNode = (node *) malloc (sizeof (node));
	if (NewNode = NULL) return
		FALSE;
	Newnode->value = newvalue;

	Change the predecessor and successor node of the new node
	newnode->next = current;
	if (previous = NULL)//At this point, insert the node as the first node in the list, modify the head pointer
		*ROOTP = newNode;
	else
		previous->next = NewNode;

	return TRUE;
}

I used to do this when I was writing: A pointer to the current pointer and before the current pointer, so we need to discuss whether the original list is empty. The book mentions an abstraction, each time inserting a new node, changing a pointer to the new node and pointing to the next node, so that you can omit the steps to discuss whether the inserted node is the first node. The code is as follows:

#include "sll_node.h"
#include <stdlib.h>

#define	FALSE	0
#define TRUE	1

// InsertNode2: Inserts the NewValue value into the ascending sorted list, returns true correctly, the error returns false
//NEXTP is a pointer to the current node, initially the head pointer
int INSERTNODE2 (node * * NEXTP, int newvalue)
{node
	*newnode;//new node pointer node
	*current;//Current node pointer

	= *NEXTP;//Initial current node is NEX The node to which the TP pointer points
	//finds the new inserted node while (the current
	!= NULL && current->value < NewValue)
	{
		nextp =¤t->next;
		Current = current->next;
	}

	allocating memory for new nodes
	NewNode = (node *) malloc (sizeof (node));
	if (NewNode = NULL) return
		FALSE;
	Newnode->value = newvalue;

	The steps for inserting are unified. That is: Each time you insert, the previous pointer points to the new node, the new node points to the next node
	*nextp = NewNode;
	Newnode->next = current;

	return TRUE;
}

Main function

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "sll_node.h"

int Insertnode (Node **rootp, int newvalue);
int InsertNode2 (Node **nextp, int newvalue);

int main ()
{
	srand (time (0));

	Node *head = (node *) malloc (sizeof (node));
	Head->next = NULL;

	for (int i = 0; i < 5; i++)
	{
		int temp = rand ()%;
		printf ("%d\n", temp);
		Insertnode (&head,temp);
		InsertNode2 (&head,temp);
	}
	
	Node *p = head->next;
	while (P!= NULL)
	{
		printf ("%d\n", p->value);
		p = p->next;
	}

	GetChar ();
	GetChar ();
	return 0;
}

Because I personally did not have a serious classroom training, their own graduate school to contact programming, data structure and so on. Read a lot of the advice on self-study programming is to write more, and want to have abstract ideas, so this method written down, may be very to the trained people are not a problem.

I feel this abstraction is very good, hope is to give oneself programming a good beginning of the road:

At the same time, because I am beginning to find that the test code will also spend a lot of time, so the completion of the code are posted up, not just functions, hope can also help more people like me.


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.