一,单链表插入操作
[cpp] view plaincopy
- typedef struct NODE {
- struct NODE *link;
- int value;
- }Node;
- #include <stdlib.h>
- #include <stdio.h>
- #define FALSE 0
- #define TRUE 1
- int s_insert(Node **rootp,int new_value) //root是一个指向Node的指针,所以指针root的指针类型是Node**
- {
- Node *current;
- Node *new;
- Node *previous;
- current = *rootp;
- previous = NULL;
- while(current != NULL && current->value < new_value)
- {
- previous = current;
- current = current->link;
- }
- new = (Node*)malloc(sizeof(Node));
- if(new == NULL)
- {
- return FALSE;
- }
- new->value = new_value;
- new->link = current;
- if(previous == NULL)
- {
- *rootp = new;
- }
- else
- {
- previous->link = new;
- }
- return TRUE;
- }
时间: 2024-10-23 22:27:58