Basic operations on a single-linked table
The structure of the linked list node is related to the linked list. I have been tossing it for a long time and it is always intermittent. If you want to spend a little time in a unified manner, it is estimated that you will be able to overcome it. If you want to solve the problem with procrastination, you will not be fully aware of it until now. Now we need to understand the linked list content that we understand.
Speaking of Single-Chain tables, you need to write the following content.
To operate a linked list, you must first create a linked list. How can I insert, delete, and search for a linked list.
Creating a single-chain table consists of the following parts:
- Create a struct of the linked list node.
- Allocate space for nodes
- Assign values to nodes
- Determine the location for the node
The following is the structure of the linked list node.
1 typedef struct Node2 {3 int element;4 struct Node *next;5 }node;
The second is the function for creating a linked list:
/********************************* Description: create a linked list * parameter: none * return value: chain table header pointer ********************************/node * creat () {node * head, * p, * q; int x = 0; bool cycle = true; head = (node *) malloc (sizeof (node); p = head; while (cycle) {printf ("enter an integer"); scanf ("% d", & x); if (x! = 0) {q = (node *) malloc (sizeof (node); q-> element = x; p-> next = q; p = q ;} else cycle = false;} head = head-> next; p-> next = NULL; return head ;}
When creating a linked list, pay special attention to the following points: 1: Allocate space for nodes. 2: assign a value to the node 3: determine the location of the node.
node *head;head = creat();
You can use these two statements in the main function to easily call the function used to create a linked list.
There are also a few small functions, which are not very important, but they are useful for learning. You may be able to use them at any time. Let's take a look at the following.
/********************************* Description: measure the length of a linked list * parameter: head: linked list head pointer * return value: chain table length *******************************/int flength (node * head) {int count = 1; node * p; p = head; while (p-> next! = NULL) {++ count; p = p-> next;} return count ;} /********************************* description: print the linked list * parameter: head: linked list header pointer * return value: chain table length *******************************/void fprint (node * head) {int length = flength (head); node * pdata = head; for (int I = 0; I <length; ++ I) {printf ("the value of element % d is % d", I, pdata-> element); pdata = pdata-> next ;}}
2. delete an element
Some tips are involved in deleting elements.
/********************************* Description: delete an element in the linked list * parameter: head: linked list header pointer * num: Data to be deleted * return value: chain table header ******************************/node * fdelete (node * head, int num) {node * p1, * p2; p1 = head; p2 = head; while (num! = P1-> element & p1-> next! = NULL) {p2 = p1; p1 = p1-> next;} if (num = p1-> element) // whether the deleted element is a header node or a non-header node must be treated separately. {If (p1 = head) {head = p1-> next; free (p1) ;}else {p2-> next = p1-> next; free (p1 ); p1 = NULL; // The purpose of writing this sentence is not to make p1 a floating pointer. } Else printf ("the element to be deleted cannot be found !!! ");}
Basic operations on a single-linked table
# Include "stdafx. h"
# Include <stdio. h>
# Include <malloc. h>
Typedef char ElemType;
Struct LNode
{
ElemType data;
Struct LNode * next;
};
//************************************** * ******************** Set the empty table setnull ()
Void setnull (struct LNode ** p)
{
* P = NULL;
}
//************************************** * ******************** Length ()
Int length (struct LNode ** p)
{
Int n = 0;
Struct LNode * q = * p;
While (q! = NULL)
{
N ++;
Q = q-> next;
}
Return (n );
}
//************************************** * ********************** Get node ()
ElemType get (struct LNode ** p, int I)
{
Int j = 1;
Struct LNode * q = * p;
While (j <I & q! = NULL)/** // * Find the I-th node */
{
Q = q-> next; j ++;
}
If (q! = NULL)/** // * finds the I-th node */
Return (q-> data );
Else
{
Printf ("the location parameter is incorrect! \ N ");
Return NULL;
}
}
//************************************** * ********************* Search for locate () by value ()
Int locate (struct LNode ** p, ElemType x)
{
Int n = 0;
Struct LNode * q = * p;
While (q! = NULL & q-> data! = X)/** // * Find the first node with the data domain x */
{
Q = q-> next;
N ++;
}
If (q = NULL)/** // * the node whose data field is equal to x is not found */
Return (-1 );
Else/**... the remaining full text>
Basic operations on a single-linked table
# Include "stdafx. h"
# Include <stdio. h>
# Include <malloc. h>
Typedef char ElemType;
Struct LNode
{
ElemType data;
Struct LNode * next;
};
//************************************** * ******************** Set the empty table setnull ()
Void setnull (struct LNode ** p)
{
* P = NULL;
}
//************************************** * ******************** Length ()
Int length (struct LNode ** p)
{
Int n = 0;
Struct LNode * q = * p;
While (q! = NULL)
{
N ++;
Q = q-> next;
}
Return (n );
}
//************************************** * ********************** Get node ()
ElemType get (struct LNode ** p, int I)
{
Int j = 1;
Struct LNode * q = * p;
While (j <I & q! = NULL)/** // * Find the I-th node */
{
Q = q-> next; j ++;
}
If (q! = NULL)/** // * finds the I-th node */
Return (q-> data );
Else
{
Printf ("the location parameter is incorrect! \ N ");
Return NULL;
}
}
//************************************** * ********************* Search for locate () by value ()
Int locate (struct LNode ** p, ElemType x)
{
Int n = 0;
Struct LNode * q = * p;
While (q! = NULL & q-> data! = X)/** // * Find the first node with the data domain x */
{
Q = q-> next;
N ++;
}
If (q = NULL)/** // * the node whose data field is equal to x is not found */
Return (-1 );
Else/**... the remaining full text>