您的位置:首页 > 编程语言 > C语言/C++

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 链表
相关文章推荐