Single-chain table

Source: Internet
Author: User

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.

  1. Create a single-chain table.

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 !!! ");}

Single-chain table

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.