Data structure---Single-linked list

Source: Internet
Author: User
Tags join advantage

A single-linked list is an advance table-link storage representation. A linked list is made up of n nodes, each node can consist of two parts: 1, data structure body 2, pointer to the next node

typedef int DATATYPE;
node struct
typedef struct node
{
	DataType data;
	struct node *link;

} ListNode;

The advantage of a linked list relative to an array is that it can randomly find a space in memory, either continuous or discontinuous, but to ensure the size of the next node that the space can tolerate.


The first one in the list to give a short, the advantage is that the short position is defined as 0, there is the first definition of the data is 1, so convenient to find.

There is a picture of the advantages of the insertion can be used without the head node is directly inserted in a way, if there is no head node, it is necessary to consider whether the head node problem.

Gives two important operations to add and remove

1. Increase (join to insert to nth position)

When adding the head node, just remember: four steps:

A, malloc a node

B. Locate the node on the n-1 location

C. Connect the nth node and the new node

D. Disconnect nth and N-1 nodes, and connect n-1 nodes with new nodes

int Insert (ListNode *first, int i, DataType x)
{
	//backup pointer
	listnode *temp = first;
	Allocate space for the new node
	listnode *newnode = (ListNode *) malloc (sizeof (ListNode));
	Determine if the space is allocated successfully
	if (!newnode)
	{
		printf ("Allocation failed \ n");
		return 0;
	}
	Assigning a value to a new node
	newnode->data = x;
	Newnode->link = NULL;
	Look for the previous node where you want to insert the for
	(int count = 0; count < i-1; count++)
	{
		temp = temp->link;
	}
	Connect the original node where you want to insert the node to the new node, and then connect the previous node and the new node that you want to insert into the node
	newnode->link = temp->link;
	Temp->link = NewNode;
	return 1;

}

2, delete (join to delete the nth)

This is a three-step walk:

A. Find the first n-1 node

B. Connect the n-1 and N+1 nodes

C, free drop node n

void ReMove (ListNode *first, int i, DataType x)
{
	//backup pointer
	listnode *temp = First,*pbefore,*pcurr;
	Determine if the position is correct
	if (i > GetLength (first))
	{
		printf ("Position error \ n");
		return;
	}
	Look for the previous node where you want to delete the for
	(int count = 0; count < i-1; count++)
	{
		temp = temp->link;
	}
	Determine the node before deleting the node
	pbefore = temp;
	Confirm the deleted node
	Pcurr = temp->link;
	if (pcurr->data! = x)
	{
		printf ("Please enter the correct data \ n");
		return;
	}
	Put the previous and next connection on the node to be deleted
	Pbefore->link = pcurr->link;
	Release the node you want to delete free
	(Pcurr);

}

There are a few places to be aware of:

1) when defining pointers and when you want to use pointers to a struct or a string, you need to allocate the amount of memory space (this is important, and often someone forgets the error that caused the segment error or memory error access)

2) You need to point the pointer to null when the pointer bit is used in time

There's one more thing to note: null is NULL is not 0 address, nor any other address, do not think it is 0 (judge the pointer when you want to use P==null or P!=null, I have written on the above specifications, we forgive)


All the code is given below

Header file:

#ifndef _head_h_
#define _HEAD_H_
#define  _crt_secure_no_warnings
#include <stdio.h>
# Include <stdlib.h>

#define OK 1
#define ERROR 0

typedef int DATATYPE;
node struct
typedef struct node
{
	DataType data;
	struct node *link;

} ListNode;

initialization function
void Initlist (ListNode *first);
Calculates the chain table length
int getlength (ListNode *first);
Insert function
int Insert (ListNode *first, int i, DataType x);
Outputs
void output (ListNode *first);
Remove
the node void remove (ListNode *first, int i, DataType x);
Find node
int search (ListNode *first, DataType x);

void Printmsg ();

int getOption ();
#endif

function file

#include "head.h" void Initlist (ListNode *first) {//If failure returns if (!first) {printf ("Allocation space failed \ n");
	Return
}//Place the next node empty because in principle no wild pointers are allowed, which is dangerous first->link = NULL;
	} int GetLength (ListNode *first) {//backup pointer, in order not to affect subsequent operations listnode *temp = first->link;
	int count = 0;
		while (temp! = NULL) {temp = temp->link;
	count++;
} return count;
	} int Insert (ListNode *first, int i, DataType x) {//backup pointer listnode *temp = first;
	Allocate space for the new node ListNode *newnode = (ListNode *) malloc (sizeof (ListNode));
		Determine if the space is allocated successfully if (!newnode) {printf ("Allocation failed \ n");
	return 0;
	}//Assign a value to the new node newnode->data = x;
	Newnode->link = NULL;
	Look for the previous node where you want to insert the for (int count = 0; count < i-1; count++) {temp = temp->link;
	Connect the original node where you want to insert the node to the new node, and then connect the previous node and the new node that you want to insert into the node Newnode->link = temp->link;
	Temp->link = NewNode;

return 1;
	} void OutPut (ListNode *first) {//backup pointer listnode *temp = first;
		Determine if the linked list is empty if (!temp->link) {printf ("the list is empty \ n");
	Return }//Loop output content for (int i = 0; i < getlength (first); i++)
		{temp = temp->link;
	printf ("%d", temp->data);
} printf ("\ n");
	} void ReMove (ListNode *first, int i, DataType x) {//backup pointer listnode *temp = First,*pbefore,*pcurr;
		Determine if the position is correct if (i > GetLength (first)) {printf ("position error \ n");
	Return
	}//Look for the previous node to delete the location for (int count = 0; count < i-1; count++) {temp = temp->link;
	}//Determine the node before deleting the node Pbefore = temp;
	Confirm the deleted node Pcurr = temp->link;
		if (pcurr->data! = x) {printf ("Please enter the correct data \ n");
	Return
	}//Put the previous and next connection to delete the node on pbefore->link = pcurr->link;

Release the node you want to delete free (PCURR);

} int IsEmpty (ListNode *first) {return (First->link = = NULL)? 1:0;}
	int Search (ListNode *first, DataType x) {ListNode *temp = first;
	int i = 0;
		Determine if the linked list is empty if (!temp->link) {printf ("the list is empty \ n");
	return-1;
		} while (Temp->data! = x) {i++;
	temp = temp->link;
} return i;
	} void Printmsg () {printf ("1, insert a data \ n");
	printf ("2, delete a data \ n"); printf ("3, getone element seat \ n ");
	printf ("4, view all data \ n");
	printf ("5, see if the table is empty \ n");
	printf ("6, exit \ n");

printf (">>");}
	int getOption () {//Get user input operation char input;
	scanf ("%c", &input);
	_flushall ();	
	Fflush (stdin);  
	Input=toupper (input);
return input; }

Main function

#include "head.h"

void Main (void)
{
	int i,x,op;
	ListNode first;
	Initlist (&first);
	Printmsg ();
	while (op = getOption ())
	{
		switch (OP)
		{case
		' 1 ':
			printf ("Please enter position and element \ n");
			scanf ("%d%d", &i, &x);
			_flushall ();
			Insert (&first,i,x);
			printf ("Insert element%d", x);
			break;
		Case ' 2 ':
			printf ("Please enter position and element \ n");
			scanf ("%d%d", &i, &x);
			_flushall ();
			ReMove (&first, I, x);
			printf ("Delete element%d\n", x);
			break;
		Case ' 3 ':
			printf ("Please enter the element you want to find \ n");
			scanf ("%d", &x);
			_flushall ();
			printf ("element in%d\n", Search (&first,x));
			break;
		Case ' 4 ':
			outPut (&first);
			break;
		Case ' 5 ':
			printf ("%d", IsEmpty (&first));
			break;

		Case ' 6 ':
			printf ("---byebye----");
			return;
			break;
		Default: Break
			;
		}
		printf ("\n>>");
	}
	System ("pause");
}

The concept of package and interface of the program is very important, to use the encapsulation of the interface as much as possible

                                                          &NBSP;&NBSP  

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.