c语言单向链表操作
typedef struct nodes_t {
int data;
struct nodes_t* next;
}nodes_t;
static nodes_t* head_point = NULL;
nodes_t* createNode(nodes_t *node) {
nodes_t *p1;
if (node == NULL) {
return head_point;
}
if (head_point == NULL) {
head_point = node;
head_point->next = NULL;
return head_point;
}
p1 = head_point;
while (p1->next!=NULL)
{
p1 = p1->next;
}
p1->next = node;
node->next = NULL;
return head_point;
}
void printNode() {
nodes_t* p1 = head_point;
while (p1 != NULL)
{
printf("data:%d\r\n",p1->data);
p1 = p1->next;
}
}
int delNode(nodes_t* node) {
nodes_t* p1 = head_point;
nodes_t* temp;
if (node == head_point) {
head_point = head_point->next;
}
else {
while (p1 != NULL) {
temp = p1;
p1 = p1->next;
if (p1 == NULL) {
return 1;
}
else if(p1==node){
temp->next = p1->next;
return 0;
}
}
}
return 0;
}
使用:
nodes_t p1 = {0};
p1.data = 1;
head_point = createNode(&p1);
nodes_t p2 = { 0 };
p2.data = 2;
head_point = createNode(&p2);
nodes_t p3 = { 0 };
p3.data = 3;
head_point = createNode(&p3);
printNode();
delNode(&p2);
printNode();