The realization of the chain List of C language __c language

Source: Internet
Author: User
Tags int size

About the use of malloc and free:

Malloc format void Malloc (int size): Allocates a size memory space to a pointer variable.

such as int *a; A= (int*) malloc (int) pointer a points to a 4-byte memory space where a holds the address of the memory space.

Free format void (VOID*PTR) Action: Frees the memory space pointed to by a pointer variable, but the pointer variable still points to that memory space.

If you release the memory space that the pointer points to, you still have access to the memory space, which results in unpredictable errors (internal interrupts, insufficient permissions).

Note: After you release the pointer, the pointer points to null, or it becomes a wild pointer.

About wild pointers, null pointers

Wild pointer definition: Point to a deleted memory space or to a memory space that has exceeded access rights

Genesis: ①: not initialized at the time of definition. ②: The pointer has been deleted or released without pointing to Null③: The pointer accesses a scope that exceeds the variable.

Because his point is unknown, access can lead to unpredictable errors.

Null pointer definition: pointer-type variable pointing to null (that is, integer 0)

For example: int *p=null, try to assign null when initializing, one, avoid pointing to the wild pointer and then call an unknown error, second, in the linked list or other cases are easy to judge conditions.

#include #include #define SIZE_STU sizeof (struct stu) struct stu {int num;
		int score;
    struct Stu *next;
	};    int n; Number of record list//I think the insertion node in the list is the hardest one in the list function. The difficulties are found at the insertion point and are differentiated at the insertion position//Insert node element struct Stu *linkedlist_insert (struct stu *head, struct stu *unit) {str
		UCT Stu *p1, *P2;
		P1 = head;                                                                                                                       
			if (head!= null) {//Search for insertion point://////////////////////////////////////////////////////
				while (p1->numnum&&p1->next!=null) {p2 = p1;
			P1 = p1->next;
			///insertion point position judgment: ① The first node of the link table before the ② linked list node ③ the end of the list node. if (p1->next==null&&unit->num>p1->num)//list node at the end of the two criteria can not be the location of the exchange {P1 
		->next = unit;
			The next node of the end p of the list is the ordinal of the null,p is greater than the ordinal of the insertion element unit->next = = NULL;   
} else {if (HEAD==P1)//Chain table header Node {head = unit; After excluding the end of a linked list nodeCondition is not considered, so P equals head can be used as a distinguishing condition between the//fore and linked list nodes of the first node of the linked list.                                              
				Unit->next = p1;                                             
					else//List node {unit->next = P1;
				P2->next = unit;
		}} else//The header is empty when {head = unit;   } n + + 1;
	List entry plus 1 return head; ///Before encountering a wrong idea, Whlie inserts the specified position as the judgment condition, thinks when p->next/is empty, then uses P=p->next to be able to appear the unknown error (afterwards to prove that does not appear the error)/the output list element void link
		 Edlist_input (struct stu *head) {struct Stu; if (head = = null) {printf ("trailing empty.")
		 \ n ");
			 else {p = head;
		  while (P!= NULL) {printf ("The school number is:%d Grade:%d The address is:%p\n", P->num, P->score, p);
			 p = p->next;
		}}//Find node element struct Stu *linkedlist_search (struct stu *head) {struct Stu *p=null;
		int sid= 0;
		p = head;
		printf ("Please enter the student's number to view:");
		scanf ("%d", &sid); while (p->next!= null&&p->num!= sid) {p = p->next;
			} if (p->num!= sid) {printf ("The number you entered is incorrect \ n");
		return NULL;
		
	printf ("Student's score:%d\n", P->score);
		}//delete node element struct Stu *linkedlist_delete (struct stu *head) {struct Stu *p1=null,*p2=null;
		int sid= 0;
		P1 = head;
		printf ("Please enter the student number to be deleted:");
		scanf ("%d", &sid);
			while (p1->next!= null&&p1->num!= sid) {p2 = p1;
		P1 = p1->next;
			} if (p1->num!= sid) {printf ("The number you entered is incorrect \ n");
		return NULL;
		} if (P1 = head)///when deleted as the header of the linked list node {head = p1->next;
		else {P2->next = p1->next; Free (p1);
		After releasing a pointer variable, be sure to point the pointer variable to null printf ("\ n you have successfully deleted \ n");
		n--;
	return head;
		}//Release list void Linkedlist_return (struct stu *head) {struct Stu;
		P1 = head;
		P2 = head;
			while (P2!= NULL) {free (P1);
			P1 = p2;
		P2 = p2->next; int main (iNT Argc,char *argv) {struct Stu *head = NULL;
	struct Stu *unit = NULL;
	Char c= ' 0 ';
	printf ("--------------------Please enter 1; Insert node------------------------\ n");
	printf ("--------------------please enter 2; output list------------------------\ n");
	printf ("--------------------Please enter 3; find node------------------------\ n");
	printf (---------------------Please enter 4; Delete node------------------------\ n ");
	printf ("--------------------enter 5; release the list and safely exit the program----------\ n");
		while (1) {printf ("Please enter the action you want to do:");
		scanf ("%c", &c);
			Switch c//uses GetChar () as a buffer to accept line wrapping (carriage return) {case ' 1 ': {
			Unit = (struct stu*) malloc (Size_stu);
			Unit->next = NULL;
				if (unit = = NULL) {printf ("Insert node failed \ n");
			Exit (0);
			printf ("Please enter student's number and score:");
			scanf ("%d", &unit->num);
			scanf ("%d", &unit->score);
			Head=linkedlist_insert (Head,unit);
			printf ("\ n you have inserted a successful!\n");
			GetChar ();
		Break
			Case ' 2 ': {linkedlist_input (head);
			GetChar ();
		BreakCase ' 3 ': {Linkedlist_search (head);
			GetChar ();
		Break
			Case ' 4 ': {Head=linkedlist_delete (head);
			GetChar ();
		Break
			Case ' 5 ': {Linkedlist_return (head); printf ("You have successfully exited the program.")
			");
			Exit (0);
		Break
			} default:printf ("Input error: Please re-enter \ n");
		GetChar ();
} system ("Pause"); }

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.