C言語の一方向チェーンの作成、解放、挿入、削除、反転操作の練習
4752 ワード
(声明:このプログラムは個人学習のためだけのもので、本菜鳥はプログラム構造、論理複雑度、コードの簡潔性、可読性などの面で代表性があるとは思わないので、後進者を誤解させないようにしたい)
/**********************************************************************************
* @brief: 、 、 、 、 、 、
* @author: wuchuan.
* @date: 2013.11.5
***********************************************************************************/
#include
#include
#include
struct node
{
int num;
struct node *next;
};
struct node *head = NULL;
void create_link(int n)
{
int i = 0;
struct node *cur = NULL;
while (i++ < n)
{
struct node *tmp = NULL;
tmp = (struct node *)malloc(sizeof(struct node));
if (tmp != NULL)
{
tmp->num = i;
tmp->next = NULL;
if (head == NULL)
head = cur = tmp;
else
{
cur->next = tmp;
cur = cur->next;
}
}
}
}
void free_link(struct node *head)
{
struct node *tmp = NULL;
struct node *cur = head;
while (cur)
{
tmp = cur;
cur = cur->next;
free(tmp);
tmp = NULL;
}
}
void print_link(struct node *head)
{
struct node *cur = NULL;
for (cur = head; cur; cur = cur->next)
printf("cur->num = %d
", cur->num);
return;
}
int get_link_len(struct node *head)
{
int len = 0;
struct node *cur = NULL;
for (cur = head; cur; cur = cur->next)
len++;
return len;
}
#if (0)
/* ok */
int insert_node(struct node **head, int num)
{
struct node *tmp = NULL;
struct node *cur = *head;
if (*head == NULL)
return -1;
tmp = (struct node *)malloc(sizeof(struct node));
if (tmp == NULL)
return -2;
tmp->num = num;
if ((*head)->num >= num)
{
tmp->next = *head;
*head = tmp;
return 0;
}
while (cur)
{
if (cur->next == NULL)
{
cur->next = tmp;
tmp->next = NULL;
return 0;
}
else if (cur->next->num >= num)
{
tmp->next = cur->next;
cur->next = tmp;
return 0;
}
cur = cur->next;
}
return -3;
}
#endif
int insert_node(struct node **head, int num)
{
struct node *cur, *pre, *pnew;
cur = pre = pnew = NULL;
if (*head == NULL)
return -1;
pnew = (struct node *)malloc(sizeof(struct node));
if (pnew == NULL)
return -2;
pnew->num = num;
pnew->next = NULL;
if ((*head)->num >= num)
{
pnew->next = *head;
*head = pnew;
return 0;
}
pre = *head;
cur = (*head)->next;
while (cur)
{
if (cur->num >= num)
{
pnew->next = cur;
pre->next = pnew;
return 0;
}
pre = cur;
cur = cur->next;
}
/* */
pre->next = pnew;
return 0;
}
void delete_node(struct node **head, int num)
{
struct node *pre, *cur, *p;
pre = cur = p = NULL;
while (*head != NULL && (*head)->num == num)
{
p = *head;
*head = (*head)->next;
free(p);
}
pre = *head;
cur = (*head)->next;
while (cur)
{
if (cur->num == num)
{
p = cur;
cur = cur->next;
pre->next = cur;
free(p);
continue;
}
pre = cur;
cur = cur->next;
}
return;
}
struct node *reverse_link(struct node **head)
{
if (*head == NULL || (*head)->next == NULL)
return *head;
struct node *pre, *cur, *next;
pre = *head;
cur = (*head)->next;
(*head)->next = NULL;
while (cur)
{
//next next
next = cur->next;
// next ( )
cur->next = pre;
pre = cur;
cur = next;
}
// head
*head = pre;
return *head;
}
/**
* test demos
* gcc link.c -o link -g -Wall
*/
int main(int argc, char *argv[])
{
if (argc != 4)
{
printf("Usage: %s [count] [number1] [number2]
", argv[0]);
printf("------[count] indicates how many nodes you want to create
");
printf("------[number1] indicates which node you want to delete
");
printf("------[number2] indicates a num node you want to insert
");
return -1;
}
printf("Now will create %d nodes in the link
", atoi(argv[1]));
create_link(atoi(argv[1]));
print_link(head);
printf("
*****link len = %d*****
", get_link_len(head));
printf("Now will delete num=%d node in the link
", atoi(argv[2]));
delete_node(&head, atoi(argv[2]));
print_link(head);
printf("
*****link len = %d*****
", get_link_len(head));
printf("Now will insert num=%d node into the link
", atoi(argv[3]));
insert_node(&head, atoi(argv[3]));
print_link(head);
printf("
*****link len = %d*****
", get_link_len(head));
printf("Now reverse the link
");
head = reverse_link(&head);
print_link(head);
printf("
*****link len = %d*****
", get_link_len(head));
free_link(head);
return 0;
}