C language linked list of general operations, create, insert, traverse, delete

Source: Internet
Author: User
#include "stdlib.h" #include "stdio.h" #include "string.h" typedef struct NODE {int data;
struct Node *next;

}slist;
	Slist *creat_slist () {//1 Create a header node and initialize slist *phead = NULL by the method of tail interpolation;
	Slist *pcur = NULL;
	Slist *pm = NULL;
	int data = 0;
	Pcur = Phead = (slist *) malloc (sizeof (slist));
	Phead->data = 0;

	Phead->next = NULL;
	2 loops Create nodes, values from the Node data field input from the keyboard,//1 as the input end sign printf ("\nplease enter the data of node ( -1:quit)");

	scanf ("%d", &data);
		while (data!=-1) {PM = (slist*) malloc (sizeof (slist));
		Pm->data = data;

		Pm->next = NULL;
		Pcur->next = PM;
		Pcur = PM;
		printf ("\nplease enter the data of node ( -1:quit)");
	scanf ("%d", &data);
return phead;
	int Slist_print (slist *phead) {slist* pcur = NULL;
	if (!phead) {return-1;
	} pcur = phead->next;
		while (pcur) {printf ("%d\n", pcur->data);
	Pcur = pcur->next;
return 1;
	Insert y int slist_nodeinsert (slist *phead, int x, int y) {slist* PM = NULL before the node value is x; slist* Pcur = NULL;
	slist* pLast = NULL;
	if (!phead) {return-1;
	} pLast = Phead;

	Pcur = phead->next;
	PM = (slist*) malloc (sizeof (slist));
	Pm->data = y;

	Pm->next = NULL;
		while (pcur) {if (Pcur->data = = x) {break;
		} pLast = Pcur;
	Pcur = pcur->next;
	} pm->next = Pcur;

	Plast->next = PM;
return 1;
	///delete the node as Y of the linked list node int slist_nodedel (slist *phead, int y) {slist* pcur = NULL;
	slist* pLast = NULL;
	if (!phead) {return-1;
	} pLast = Phead;
	Pcur = phead->next;
		while (pcur) {if (Pcur->data = = y) {break;
		} pLast = Pcur;
	Pcur = pcur->next;
	} if (!pcur) {return-1;
	} Plast->next = pcur->next;
	Free (pcur);
return 1;
	int slist_destory (slist *phead) {slist* pnext = NULL;

	slist* pcur = NULL;
	if (!phead) {return-1;
	} pcur = Phead;
	
	Pnext = pcur->next;
		while (1) {free (pcur);
		Pcur = Pnext;
		if (!pcur) {break;		
	} Pnext = pnext->next;
return 1; int MaiN () {slist* phead = creat_slist ();
	if (!phead) {return-1;

	} slist_print (Phead);
	Slist_nodeinsert (Phead, 20, 19);

	Slist_print (Phead);
	Slist_nodedel (Phead, 19);

	Slist_print (Phead);

	Slist_destory (Phead);
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.