C语言单向链表的创建、释放、插入、删除、翻转操作练习
2013-11-05 20:48
591 查看
(声明:本程序仅为个人学习之用,本菜鸟并不认为其在程序结构、逻辑复杂度、代码简洁性、可读性等方面具有代表性,盼不误导后来者)
/**********************************************************************************
* @brief: 实现可重复有序单向链表的创建、释放、删除、插入、遍历、取长、反转等操作
* @author: wuchuan<wuchuan@qq.com>.
* @date: 2013.11.5
***********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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\n", 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]\n", argv[0]);
printf("------[count] indicates how many nodes you want to create\n");
printf("------[number1] indicates which node you want to delete\n");
printf("------[number2] indicates a num node you want to insert\n");
return -1;
}
printf("Now will create %d nodes in the link\n", atoi(argv[1]));
create_link(atoi(argv[1]));
print_link(head);
printf("\n*****link len = %d*****\n", get_link_len(head));
printf("Now will delete num=%d node in the link\n", atoi(argv[2]));
delete_node(&head, atoi(argv[2]));
print_link(head);
printf("\n*****link len = %d*****\n", get_link_len(head));
printf("Now will insert num=%d node into the link\n", atoi(argv[3]));
insert_node(&head, atoi(argv[3]));
print_link(head);
printf("\n*****link len = %d*****\n", get_link_len(head));
printf("Now reverse the link\n");
head = reverse_link(&head);
print_link(head);
printf("\n*****link len = %d*****\n", get_link_len(head));
free_link(head);
return 0;
}
/**********************************************************************************
* @brief: 实现可重复有序单向链表的创建、释放、删除、插入、遍历、取长、反转等操作
* @author: wuchuan<wuchuan@qq.com>.
* @date: 2013.11.5
***********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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\n", 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]\n", argv[0]);
printf("------[count] indicates how many nodes you want to create\n");
printf("------[number1] indicates which node you want to delete\n");
printf("------[number2] indicates a num node you want to insert\n");
return -1;
}
printf("Now will create %d nodes in the link\n", atoi(argv[1]));
create_link(atoi(argv[1]));
print_link(head);
printf("\n*****link len = %d*****\n", get_link_len(head));
printf("Now will delete num=%d node in the link\n", atoi(argv[2]));
delete_node(&head, atoi(argv[2]));
print_link(head);
printf("\n*****link len = %d*****\n", get_link_len(head));
printf("Now will insert num=%d node into the link\n", atoi(argv[3]));
insert_node(&head, atoi(argv[3]));
print_link(head);
printf("\n*****link len = %d*****\n", get_link_len(head));
printf("Now reverse the link\n");
head = reverse_link(&head);
print_link(head);
printf("\n*****link len = %d*****\n", get_link_len(head));
free_link(head);
return 0;
}
相关文章推荐
- 单向链表小练习--》创建链表头,添加删除链表节点,释放链表内存,选择插入数据
- (C语言版)链表(二)——实现单向循环链表创建、插入、删除、释放内存等简单操作
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- c++之链表篇1:单向链表的创建,打印,删除,插入,销毁等基本操作
- (C语言版)链表(二)——实现单向循环链表创建、插入、删除、释放内存等简单操作
- c语言链表基本操作(带有创建链表 删除 打印 插入)
- 常见算法:C语言中链表的操作(创建,插入,删除,输出)
- C语言实现带头结点的链表的创建、查找、插入、删除操作
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- C语言 单向链表的创建、遍历、排序、删除操作
- 链表操作综合练习(创建、拆分、插入、删除、合并、打印、逆置)
- (C语言版)链表(二)——实现单向循环链表创建、插入、删除、释放内存等简单操作
- (C++版)链表(一)——实现单向链表创建、插入、删除等相关操作
- [转]C语言之单向链表的创建插入删除等功能
- C语言各种链表操作(创建、打印、删除、插入、反转)
- 单向链表的基本操作-创建、插入、删除
- 单向链表的操作:创建,删除,插入,销毁,查找
- (C语言版)链表(一)——实现单向链表创建、插入、删除等简单操作(包含个人理解说明及注释,新手跟着写代码)
- 单向链表的相关操作(创建,遍历,插入,删除,逆置)
- 链表的基本操作(创建,查找指定位置元素,删除指定元素,插入,倒置,去重,求集合的差,分别交换结点与交换结点值实现的冒泡排序,将两个有序链表合并成一个有序链表)c语言实现