Design and implementation of 1.3 single-linked list

Source: Internet
Author: User
Tags define null

The implementation of the basic operation of the single-linked list, including the establishment and release of the list, find, seek length, find successor, insert, delete, output and other functions.

Debug Environment: devc++//library file and preset definition #include <stdio.h> #include <stdlib.h> #define NULL 0 typedef int elemtype;//Specify single-linked list

Data type//single-linked list storage structure defines typedef struct LNODE {elemtype data;//data domain struct lnode *next;//pointer field}lnode,*linklist; /****---------------------------------------------------****///single-linked list method One (function with return worth to the table head pointer)//function name: createone (int n)// Parameters: (incoming) int n, number of incoming linear table nodes//function: Establish an empty linear table//return value: lnode* Returns the struct pointer, which is the resulting table-head pointer/****----------------------------------------
	----------****/lnode* createone (int n) {int i;
	Lnode *head;//Define the head node pointer lnode *p;//Define a new node pointer///Establish a linear list of lead nodes head = (lnode*) malloc (sizeof (Lnode));

	Head->next = NULL;
	printf ("Pleade input the data for linklist nodes:\n"); 
	    for (i = n; i > 0; i--) {p = (lnode*) malloc (sizeof (Lnode));//A new node is created for the space ("%d", &p->data) and the new node scanf
		New node inserted into the table header P->next = head->next;
	Head->next = p; Return head;//returns the head node pointer, which can get the address of the single-linked list}/****---------------------------------------------------------****///single-linked list establishmentMethod Two (function no return value)//function Name: Createtwo (lnode*head, int n)//parameter: (incoming) Lnode*head pass in a linked list pointer//(incoming) int n, number of incoming linear table nodes//function: Create an empty Linear table//return value: No/****--------------------------------------------------------****/void Createtwo (linklist &head,
	int n) {int i;
	Lnode *p;//Define a new node pointer///Establish a linear list of lead nodes head = (linklist) malloc (sizeof (Lnode));

	Head->next = NULL;
	 printf ("Pleade input the data for linklist nodes:\n"); for (i = n; i > 0; i--) {p = (lnode*) malloc (sizeof (Lnode));//For new node application space, that is, to create a node scanf ("%d", &p->data);//New node
		Assignment//new node inserted into the table header P->next = head->next;
	Head->next = p; }}/****--------------------------------------------------****///Function Name: Insertlnode (lnode *l, int i, elemtype e)//parameter: (Pass Lnode*l, incoming linear header pointer L//(incoming) int i insert position//(incoming) Elemtype e insert element//function: Linear table Insert an element//return value: int, return 1 indicates success, 0 indicates failure/**** -------------------------------------------------****/int Insertnode (lnode *l, int i, int e) {Lnode *p = l;//defines a pointer to the first

	Pointer to a node int j = 0; Look backwards with the hands.Until P points to the first node while (P&&j < i-1) {p = p->next;
	++j; }//Insertion position legal judgment if (!p| | J > I-1) {printf ("error!
		The location OS illegal! ");
	return 0;
	} Lnode *s;
	s = (lnode*) malloc (sizeof (Lnode));//Create new node S->data = e;//new node assignment//Insert node S->next = p->next;
	P->next = s;
return 1; }/****------------------------------------------------****///Function Name: Deletenode (linklist &l, int i, int &e)//Parameter : (incoming) linklist &l, linear header pointer L address//(incoming) int i delete location//(outgoing) Elemtype &e Store The value of the delete node element/function: Delete an element in a linear table//return value: El Type Emtype returns the value of the delete node element/****-----------------------------------------------****/elemtype deletenode (linklist &l,
	int i, int &e) {Lnode *p;    p = L;   Defines a pointer to the first node of lnode*q;

	Temporarily store the node you want to delete int j = 0;
		The pointer looks backwards until p points to the I node while (P->next && J < i-1) {p = p->next;
	++j; }//Delete location legitimacy judgment if (!p| |
		j>i-1) {printf ("element is not exist!");
	return 0;
	} q = q->next; P->next = q->next;//Delete the first node E = Q->d ATA;
	Free (q); Return (e);//Returns the value of the delete node element}/****------------------------------------------------------------------*****///function name: D       Isplaylist (linklist &l, int i, elemtype &e)//Parameters: (incoming) linklist &l, linear header pointer L address//(incoming) int i display position// Outgoing Elemtype &e Store Displays the value of the node element//function: Displays all elements in the linear table//return value: No/****----------------------------------------------------------
	--------****/void Displaylist (linklist &l) {Lnode *p;
	p = l->next;
		while (p!= NULL) {printf ("%d", p->data);
	p = p->next;
} printf ("\ n");
	}//******************* test Program *****************************//int main () {Lnode *l1;
	int nodenum;
	printf ("Please input the Init linknode number:\n");
	scanf ("%d", &nodenum);
	L1 = Createone (nodenum);
	LNODE*L2;
	Createtwo (l2,nodenum);//You can also call generate L2 linked list//output current list contents printf ("The present L1 is:");

	Displaylist (L1);
	Call int result;//in order to determine the success of the call, you can define the result to check result = Insertnode (l1,2,88);
	if (result) {printf ("Success to insert!\n"); }//output current linked list content PRINTF ("The current L1 is");
	Displaylist (L1);
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.