- 3
- 0
- 约4.28千字
- 约 12页
- 2017-08-18 发布于河南
- 举报
单链表增删改查(A single list of crud)
单链表增删改查(A single list of crud)
The structure of a single linked list is the simplest of the data structures, each of which has only one pointer to the latter.
Single linked list creation:
Typedef struct node
{
Int data; / / node content
Node *next; / / the next node
}node;
/ / create a single list
Node, *create ()
{
Int i=0; / / the number of data in the list
Node, *head, *p, *q;
Int x = 0;
Head = (node * malloc) (sizeof (node)); / / create a head node
While (1)
{
Printf (Please input the data:);
Scanf (%d, x);
If (0 = = x) //data for the creation of the end of 0
{
Break;
}
P = (node *) malloc (sizeof (node));
P-data = x;
If (++i = = 1) / / list of only one element
{
Head-next = p; / / head is connected to the back
}
Else
{
Q-next = p; / / connected to the end of the list
}
Q = p; //q points to the last node
}
Q-next = NULL; / / the last pointer list for NULL
Return head;
}
In the code above, the while cycle each time an integer data read from the terminal, and call malloc dynamic allocation list node memory the integer data, then in the end is inserted into the single list; finally when the data of 0 represents the insertion end of data the end node, next pointer to NULL.
/ / single chain length measurement
Int length (node *head)
{
Int len = 0;
Node *p = head-next;
While (NULL = = P)
{
Len++;
P = p-next;
}
Return len;
}
/ / print list
Void print (node *head)
{
Int POS = 0;
Node *p = NULL;
If (NULL = head-next)
{
Printf (Link, is, empty, \n);
Return;
}
P = head-next;
While (NULL! = P) / / traversing the list
{
Printf (The,%dth, node,%d\n, is:, ++pos, p-data);
P = p-next;
}
}
/ / single list node search
/ / node search POS position, returns the node pointer
//pos starts at 0, and 0 returns the head node
Node *search_node (node, *head, int, POS)
{
Node *p = head-next;
If (POS 0) //pos is not in the right position
{
Printf (incorrect, position, to, search, node, \n);
Return NULL;
}
If (POS = 0)
{
Return head;
}
If (NULL = P)
{
Printf (Link is empty \n!); / / list is
您可能关注的文档
- 农村垃圾处理(Rural garbage disposal).doc
- 农村电力网作业题(The problem of rural power network).doc
- 农村房屋买卖院墙的权属争议之确定及权利救济(To determine the ownership dispute and the sale of rural housing rights relief wall).doc
- 冠心病的急救(Emergency coronary heart disease).doc
- 冬令怕冷,常吃这类食品可帮助改善怕冷(The winter cold, eat this kind of food can help improve the cold).doc
- 冬凌茶简介(Brief introduction of Rabdosia rubescens tea).doc
- 冬季施工措施论文(The winter construction measures).doc
- 冒险点点操作说明(Little adventure instructions).doc
- 冰封王座六级统一考试(The frozen throne six grade examination).doc
- 冰心的语言特色(The language features of Bing Xin).doc
原创力文档

文档评论(0)