Way linked list is a linked list, which is characterized by the direction of the link is one-way linked list, access to the list by starting from the head, down sequentially read. Of course, later on in-depth study of data structures, will be involved in a doubly linked list, circular list such as linear table, I am no longer here to explain.
Chain is mainly formed by the junction of a consecutive string together, wherein each node is further divided into two parts: (data) data field, (Next) pointer field.
- Data field (data): main information is stored on node
- Pointer field (next): is stored in the address of the next node
One-way linked list form:
Well, since we know the elements of a single list of what and how the form of a single list, we can begin to hands-simple add, delete, search, change.
First of all, we must learn to create lists, create nothing more than increase:
- If the list is empty, then the establishment of the list and will add this node as the first node
- If the list is not empty, then traverse directly into the tail
Incidentally, in order to avoid a double operation of the pointer, the ease of understanding you, I use the node to store the first address of the first node point, that is, I rounding data field header node, in its next point to a new node, as shown above way linked list form
//两个参数 一个是头结点,一个是 要插入的结点
//头结点:它的next指向的才是一个新的结点开始
void addNode(listNode * head, listNode * q)
{
listNode * p = head->next;
if (head->next == NULL)
{
head->next = q;
}
else
{
while (p->next != NULL)
{
p = p->next;
}
p->next = q;
}
}
Delete list: Delete here, referring to the delete a node which, if it wants to delete a node, it is to traverse that meet the given criteria put it removed.
void deleteNode(listNode * head, int data)
{
listNode * p = head->next, *q = head;
//如果是空的 可以返回一个提醒也可以不处理
while (p)
{
if (p->data == data)
{
q->next = p->next;
free(p);
break;
}
q = p;
p = p->next;
}
}
Search, change the operation I will not elaborate here, because this is to implement a cycle, and then determine whether each data field is consistent with what you are looking ...
Finally, I end this blog with a whole program:
#include <stdio.h>
#include <stdlib.h>
struct listNode
{
int data;
struct listNode * next;
};
typedef struct listNode listNode;
void addNode(listNode * head, listNode * q);
void deleteNode(listNode * head, int data);
int main()
{
listNode head,*q,*p;
head.next = NULL;
int i;
//链表的创建
for (i = 1; i < 6; i++)
{
q = (listNode *)malloc(sizeof(listNode));
if (!q) exit(0);
q->data = i;
q->next = NULL;
addNode(&head, q);
}
p = head.next;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
deleteNode(&head, 2); //删除数据域为 2 的结点
p = head.next;
//遍历输出
while (p)
{
printf("%d ", p->data);
p = p->next;
}
//释放整个链表的内存空间
p = head.next;
q = &head;
while (p)
{
q->next = p->next;
free(p);
p = q->next;
}
return 0;
}
//两个参数 一个是头结点,一个是 要插入的结点
//头结点:它的next指向的才是一个新的结点开始
void addNode(listNode * head, listNode * q)
{
listNode * p = head->next;
if (head->next == NULL)
{
head->next = q;
}
else
{
while (p->next != NULL)
{
p = p->next;
}
p->next = q;
}
}
void deleteNode(listNode * head, int data)
{
listNode * p = head->next, *q = head;
//如果是空的 可以返回一个提醒也可以不处理
while (p)
{
if (p->data == data)
{
q->next = p->next;
free(p);
break;
}
q = p;
p = p->next;
}
}
operation result:
If you prefer, remember to point a praise wow! ! ! Thanks.