The C ++ programming language is widely used and plays a very important role in the eyes of developers. Here, we can use the C ++ linked list related skills to fully understand the application methods of this language and how their applications can bring us different feelings.
C ++ linked list operation code example:
- // Linklist. cpp: defines the entry point of the console application.
- # Include "stdafx. h"
- # Include "malloc. h"
- # Include "stdlib. h"
- # Define NULL 0
- # Define LEN sizeof (struct student)
- Struct student
- {
- Long num;
- Float score;
- Struct student * next;
- };
- Int n;
- Struct student * creat ()
- {
- Struct student * head;
- Struct student * p1, * p2;
- N = 0;
- P1 = p2 = (struct student *) malloc (LEN );
- Scanf ("% ld", & p1-> num );
- Scanf ("% f", & p1-> score );
- Head = NULL;
- While (p1-> num! = 0)
- {
- N ++;
- If (n = 1)
- {
- Head = p1;
- }
- Else
- {
- P2-> next = p1;
- }
- P2 = p1;
- P1 = (struct student *) malloc (LEN );
- Scanf ("% ld", & p1-> num );
- If (p1-> num = 0)
- Break;
- Scanf ("% f", & p1-> score );
- }
- P2-> next = NULL;
- Return (head );
- };
- Void print (struct student * head)
- {
- Struct student * p;
- Printf ("\ n New, These % d records are: \ n", n );
- P = head;
- If (head! = NULL)
- {
- Do
- {
- Printf ("% d % 5.1f \ n", p-> num, p-> score );
- Pp = p-> next;
- } While (p! = NULL );
- }
- }
- Struct student * insert (struct student * head, struct student * stud)
- {
- Struct student * p0, * p1, * p2;
- P1 = head;
- P0 = stud;
- If (head = NULL)
- {
- Head = p0;
- P0-> next = NULL; // insert into head point
- }
- Else
- {
- While (p0-> num> p1-> num) & (p1-> next! = NULL ))
- {
- P2 = p1; // p2 is point to just p1 point to node;
- P1p1 = p1-> next;
- }
- If (p0-> num <= p1-> num)
- {
- If (p1 = head)
- {
- Head = p0; // insert into before first node
- }
- Else
- {
- P2-> next = p0; // insert into after point p2
- }
- P0-> next = p1;
- }
- Else
- {
- P1-> next = p0; // insert into after last point
- P0-> next = NULL;
- }
- }
- N ++;
- Return (head );
- };
- Struct student * del (struct student * head, long num)
- {
- Struct student * p1, * p2;
- If (head = NULL)
- {
- Printf ("\ n list Null! \ N ");
- Return (head );
- }
- P1 = head;
- While (num! = P1-> num & p1-> next! = NULL)
- // Find num if equal p1-> num
- {
- P2 = p1;
- P1p1 = p1-> next;
- }
- If (num = p1-> num)
- {
- If (p1 = head)
- Head = p1-> next; // delete head node because num = head. num
- Else
- P2-> next = p1-> next; // delete node. node is not head point
- Printf ("delete: % ld \ n", num );
- N --;
- }
- Else
- {
- Printf ("% ld not been found! \ N ", num );
- }
- Return (head );
- };
- Int _ tmain (int argc, _ TCHAR * argv [])
- {
- Struct student * head, * end;
- Head = creat ();
- Print (head );
- Struct student insertnode;
- Insertnode. num = 3;
- Insertnode. score = 900;
- Head = insert (head, & insertnode );
- Print (head );
- Head = del (head, 3 );
- Print (head );
- Return 0;
- }
The implementation methods of C ++ linked list operations are described here.