Linked List of C language, C Language
I have reviewed the C language knowledge these two days. In order to prepare for learning OC in the next stage, the compiling and running environment of the following code is Xcode5.0, I wrote a blog post to share with you the knowledge of the C language related to linked lists that I reviewed yesterday. The following is the summary of the content of the C language. The Code is also compiled based on my own ideas, if you have any shortcomings, please criticize and advise me.
Specifically, the linked list is the content of a linear table in the data structure. The content stored in the linked list is arranged in a linear manner, just like a line that Concatenates the data to be stored, the linked list can be analogous to a beaded sub-string. The data is the beads, and the next pointer between the data is equivalent to the thread of the beads.
The time complexity of linked list operations: the time complexity of inserting data into the linked list is O (1 ).
To really understand the linked list and its representation in C language, the premise is to understand the pointers and struct in C language. To put it bluntly, entering the code is the key, and there are basically comments in the code.
1. Use struct to define the nodes of the linked list
1 // define node 2 typedef struct node {3 // store data 4 int data; 5 // point to next Node 6 struct node * next; 7} node;
2. define the overall structure of the linked list to store the node and information in the linked list.
// Define the chain table structure typedef struct {// stores the Node * head of the header Node; // records the number of nodes int count;} List;
3. initialize the linked list and assign the head node to the linked list. The number of nodes is initialized to 0.
/***************************************: Initialize the linked list and allocate the header node. The number of nodes is 0 * parameter: linked list pointer * Author: Mr. li * Date: 14-07-23 *********************************** * ************/void initLinkList (List * list) {// allocate memory to the header Node list-> head = malloc (sizeof (Node); // The next Node of the header Node is empty list-> head-> next = NULL; // The total number of nodes is 0 list-> count = 0 ;}
4. create a single linked list. For the sake of simplicity, store the data to be stored in the array first. Below I use the reverse order method to create the linked list, which is to insert data from the head node, you can also create a linked list in sequence and insert data from the back of the linked list.
/***************************************: Create a linked list in reverse order and insert * parameter: linked list pointer * from the first node. Author: Mr. li * Date: 14-07-23 *********************************** * ************/void createLinkList (List * list) {// data int a [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} used to create a linked list; // a temporary Node pointer is used to allocate Node memory Node * p; for (int I = 0; I <10; I ++) {// allocate memory to the new Node p = (Node *) malloc (sizeof (Node )); // assign p-> data = a [I] to the new node; // Add the value to the end of the header node, create a chain table in reverse order p-> next = list-> head-> next; list-> head-> next = p; // Add the number of linked list nodes to a list-> count ++ ;}}
5. To facilitate viewing the values in the linked list, a function is required to print the data in the linked list.
/***************************************: Print the value in the linked list * parameter: linked list pointer * Author: Mr. li * Date: 14-07-23 *********************************** * ************/void printList (List * list) {// temporary Node pointer variable Node * p; // traverse p = list-> head-> next; while (p! = NULL) {// custom output integer function print (p-> data); p = p-> next;} putchar ('\ n ');}
6. query the position of an element in the linked list
/***************************************: Find the position of the specified value in the linked list, and some return its position. If not, return-1 * parameter: linked list pointer. the value to be searched * Author: Mr. li * Date: 14-07-23 *********************************** * ***********/int search (List * list, int obj) {// defines the cursor Node * p; // The Position of the flag value int local = 0; p = list-> head-> next; while (p! = NULL) {local ++; if (p-> data = obj) {// return local;} p = p-> next ;} // if the value does not exist, return-1 return-1 ;}
7. Delete the corresponding data in the linked list
/***************************************: Delete the value in the linked list * parameter: linked list pointer, the value to be deleted * Author: Mr. li * Date: 14-07-23 *********************************** * ************/void delete (List * list, int obj) {// query whether the element to be deleted is in the linked list. If no element exists, return-1 int flag = search (list, obj ); // determine the validity of the value if (flag =-1) {printf ("no value to be deleted! \ N ") ;}else {// defines two secondary cursor nodes * p, * q; // assigns q, p A value of q = list-> head; p = list-> head-> next; // search for the corresponding value in a loop and delete while (p! = NULL) {if (p-> data = obj) {q-> next = p-> next; p-> next = NULL; free (p); break ;} q = p; p = p-> next ;}}}
8. insert data into the linked list
/***************************************: Insert the corresponding value to the specified position * parameter: linked list pointer, inserted position, inserted value * Author: Mr. li * Date: 14-07-23 *********************************** * ***********/void insert (List * list, int local, int number) {int count = 0; // judge the legality of the value if (count> list-> count) {printf ("the value you entered is invalid \ n");} else {// defines the cursor pointer, pointing to the head Node of the linked list * p = list-> head; while (count! = Local) {p ++; count ++;} // allocate a new Node and assign Node * q = (Node *) to the new Node *) malloc (sizeof (Node); q-> data = number; q-> next = NULL; // Insert a new Node q-> next = p-> next; p-> next = q ;}}
The above is the code for learning the linked list of the dish. Let's share it with you !!
How to define a linked list in C language is better to explain each code in detail
/* Creat a list */
# Include "stdlib. h"
# Include "stdio. h"
Struct list
{Int data;
Struct list * next;
};
Typedef struct list node;
Typedef node * link;
Void main ()
{Link ptr, head;
Int num, I;
Ptr = (link) malloc (sizeof (node ));
Ptr = head;
Printf ("please input 5 numbers ==>\ n ");
For (I = 0; I <= 4; I ++)
{
Scanf ("% d", & num );
Ptr-> data = num;
Ptr-> next = (link) malloc (sizeof (node ));
If (I = 4) ptr-> next = NULL;
Else ptr = ptr-> next;
}
Ptr = head;
While (ptr! = NULL)
{Printf ("The value is ==> % d \ n", ptr-> data );
Ptr = ptr-> next;
}
}
The above is a simple C program for creating a linked list. A linked list is a data block with data and a pointer to the next data. This data link can be operated, such as inserting data, deleting data, and so on. As for instructions, first define a struct that contains data and pointers to the next data block. Then allocate space. Note that the last one is NULL. You can also point to the first data block to form a circular linked list.
What is the difference between linked list and queue in C language?
Hello.
A linked list is a data structure, while a queue is an abstract concept, just like a stack.
Ships are a relatively abstract concept, including wooden ships and iron ships. The queue is like a ship, and the linked list is like a ship building material.
Queues can be implemented using linked lists or dynamic arrays. This abstract concept can be implemented using various specific data structures.
Elemtype * elem, the first element of SQQUEUE, actually points to an array where elements of the elemtype are stored, then front and rear mark the array subscript corresponding to the first and last elements of the team.
Typedef struct _ Point {
Int x, y;
} Point;
# Define elemtype Point // This elemtype can be any structure defined by yourself. It can be a struct or a simple data type.
Elemtype array [10] = {0}; // This is the data structure of the queue. Here it is a Point array.
SQQUEUE queue = {0 };
Queue. elem = array; // the elements in the array are the elements in the queue.
Queue. front = queue. rear = queue. size = 0;
The next pointer you mentioned is a member of the linked list node. Think about the differences between linked list nodes and linked list nodes.
Typedef struct _ ListNode {// This Is A linked list Node
Int x, y; // This is the stored data
Struct _ ListNode * next;
} ListNode;
Typedef struct _ List {// This Is A linked List, which does not store next
ListNode * front, rear;
} List;
If you still don't understand it, you can ask me.