学习日志 姓名:叶圣民 日期:2018.07.24.
| |
今日学习任务 | 掌握单项链表的使用 |
今日任务完成情况(详细说明本日任务是否按计划完成,开发的代码量) | 链表的分类:单链表、单向循环链表、双链表、双向循环链表链表是数据结构:管理数据(存、取、遍历、查找)链表的作用:解决数组带来的空间利用问题数组静态分配空间;容易造成浪费;链表动态分配空间结点:数据域 和 链接域链接表:①定义结点类型 typedef 重命名②定义头指针③初始化链表④定义添加结点函数 |
今日开发中出现的问题汇总 | 链表的逆序有表头链表的逆序 |
今日未解决问题 | 无 |
①简单结构体
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int num;
struct node *next;
};
int main()
{
struct node *head = (struct node *)malloc(sizeof(struct node));
struct node *p1 = (struct node *)malloc(sizeof(struct node));
struct node *p2 = (struct node *)malloc(sizeof(struct node));
struct node *p3 = (struct node *)malloc(sizeof(struct node));
p1->num = 1;
p1->next = p2;
p2->num = 2;
p2->next = p3;
p3->num = 3;
p3->next = NULL;
head = p1;
while(head != NULL)
{
printf("%d\n",head->num);
head = head->next;
}
return 0;
}
②头插入结点
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
typedef struct node Node;
typedef Node * Link;
void init(Link *head)
{
*head = NULL;
}
void insert_head(Link newnode,Link *head)
{
if(*head == NULL)
{
newnode->next = NULL;
*head = newnode;
}
else
{
newnode->next = *head;
*head = newnode;
}
}
#if 0
void insert_tail(Link newnode,Link *head)
{
if(*head == NULL)
{
newnode->next = NULL;
*head = newnode;
}
else
{
Link tmp = *head;
while(tmp->next != NULL)
{
tmp = tmp->next;
}
newnode->next = NULL;
tmp->next = newnode;
}
}
#endif
void display(Link head)
{
Link temp = head;
while(temp != NULL)
{
printf("%d\n",temp->num);
temp = temp->next;
}
}
int main()
{
int i;
Link head;
Link newnode;
init(&head);
for(i = 0;i < 10; i++)
{
Link newnode = (Link)malloc(sizeof(Node));
newnode->num = i + 1;
insert_head(newnode,&head);
}
display(head);
return 0;
}
③尾插入结点
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
typedef struct node Node;
typedef Node * Link;
void init(Link *head)
{
*head = NULL;
}
#if 0
void insert_head(Link newnode,Link *head)
{
if(*head == NULL)
{
newnode->next = NULL;
*head = newnode;
}
else
{
newnode->next = *head;
*head = newnode;
}
}
#endif
void insert_tail(Link newnode,Link *head)
{
if(*head == NULL)
{
newnode->next = NULL;
*head = newnode;
}
else
{
Link tmp = *head;
while(tmp->next != NULL)
{
tmp = tmp->next;
}
newnode->next = NULL;
tmp->next = newnode;
}
}
void display(Link head)
{
Link temp = head;
while(temp != NULL)
{
printf("%d\n",temp->num);
temp = temp->next;
}
}
int main()
{
int i;
Link head;
Link newnode;
init(&head);
for(i = 0;i < 10; i++)
{
Link newnode = (Link)malloc(sizeof(Node));
newnode->num = i + 1;
insert_tail(newnode,&head);
}
display(head);
return 0;
}
④任意位置插入单个结点
#include <stdio.h>
#include <stdlib.h>
/***枚举***/
enum Judge
{
Link_empty,
Link_insert_fail,
Link_insert_success,
Delete_empty,
Delete_success,
Delete_fail,
Reverse_success,
Reverse_fail
};
/***定义结构体***/
struct node
{
int num;
struct node *next;
};
/***重命名***/
typedef struct node Node;
typedef Node * Link;
/***初始化 head***/
void init(Link *head)
{
*head = NULL;
}
/***头插***/
#if 0
void insert_head(Link newnode,Link *head)
{
if(*head == NULL)
{
newnode->next = NULL;
*head = newnode;
}
else
{
newnode->next = *head;
*head = newnode;
}
}
#endif
/***尾插***/
void insert_tail(Link newnode,Link *head)
{
if(*head == NULL)
{
newnode->next = NULL;
*head = newnode;
}
else
{
Link tmp = *head;
while(tmp->next != NULL)
{
tmp = tmp->next;
}
newnode->next = NULL;
tmp->next = newnode;
}
}
/***中间插入***/
#if 0
int insert_mid(Link newnode,Link *head,int n)
{
if(*head == NULL)
{
return Link_empty;
}
Link temp = *head;
while(temp != NULL)
{
if(temp->num == n)
{
newnode->next = temp->next;
temp->next = newnode;
return Link_insert_success;
}
temp = temp->next;
}
return Link_insert_fail;
}
#endif
/***删除节点***/
#if 0
int delete_node(Link *head, int n)
{
if(*head == NULL)
{
return Delete_empty;
}
Link tmp = *head;
if((*head)->num == n)
{
*head = (*head)->next;
free(tmp);
tmp = NULL;
return Delete_success;
}
else
{
Link p = tmp;
tmp = tmp->next;
while(tmp != NULL)
{
if(tmp->num == n)
{
p->next = tmp->next;
free(tmp);
tmp = NULL;
return Delete_success;
}
p = tmp;
tmp = tmp->next;
}
return Delete_fail;
}
}
#endif
/***逆序链表***/
#if 1
int reverse_node(Link *head)
{
if((*head == NULL) || ((*head)->next == NULL))
{
return Reverse_fail;
}
else
{
Link temp = *head;
Link p = temp->next;
Link s = p->next;
while(temp != NULL)
{
p->next = s;
s = p;
p = temp;
temp = temp->next;
}
p->next = s;
(*head)->next = NULL;
*head = p;
return Reverse_success;
}
}
#endif
/***显示链表***/
void display(Link head)
{
Link temp = head;
while(temp != NULL)
{
printf("%d\n",temp->num);
temp = temp->next;
}
}
int main()
{
int i;
Link head;
Link newnode;
init(&head);
#if 1
for(i = 0;i < 10; i++)
{
Link newnode = (Link)malloc(sizeof(Node));
newnode->num = i + 1;
insert_tail(newnode,&head);
}
#endif
display(head);
#if 0
int result2 = delete_node(&head,6);
if(result2 == Delete_empty)
{
printf("The link is empty.\n");
}
if(result2 == Delete_success)
{
display(head);
}
if(result2 == Delete_fail)
{
printf("Delete fail!\n");
}
#endif
#if 0
newnode = (Link)malloc(sizeof(Node));
newnode->num = 11;
int result1 = insert_mid(newnode,&head,5);
{
if(result1 == Link_empty)
{
printf("链表为空\n");
}
if(result1 == Link_insert_success)
{
display(head);
}
else
{
printf("链表插入失败\n");
}
}
#endif
int result3 = reverse_node(&head);
if(result3 == Reverse_fail)
{
printf("\n");
}
if(result3 == Reverse_success)
{
display(head);
}
return 0;
}
⑤有表头的单向链表
#include <stdio.h>
#include <stdlib.h>
/***枚举***/
enum Judge
{
Link_empty,
Link_insert_fail,
Link_insert_success,
Delete_empty,
Delete_success,
Delete_fail,
Reverse_success,
Reverse_fail
};
/***定义结构体***/
struct node
{
int num;
struct node *next;
};
/***重命名***/
typedef struct node Node;
typedef Node * Link;
/***初始化 head***/
void init(Link *head)
{
*head = (Link)malloc(sizeof(Node));
(*head)->next = NULL;
}
/***头插***/
#if 0
void insert_head(Link newnode,Link *head)
{
newnode->next = (*head)->next;
(*head)->next = newnode;
}
#endif
/***尾插***/
#if 0
void insert_tail(Link newnode,Link *head)
{
Link tmp = *head;
while(tmp->next != NULL)
{
tmp = tmp->next;
}
newnode->next = tmp->next;
tmp->next = newnode;
}
#endif
/***中间插入***/
#if 1
int insert_mid(Link newnode,Link *head,int n)
{
Link temp = *head;
while(temp != NULL)
{
if(temp->num == n)
{
newnode->next = temp->next;
temp->next = newnode;
return Link_insert_success;
}
temp = temp->next;
}
return Link_insert_fail;
}
#endif
/***删除节点***/
#if 0
int delete_node(Link *head, int n)
{
if(*head == NULL)
{
return Delete_empty;
}
Link tmp = *head;
if((*head)->num == n)
{
*head = (*head)->next;
free(tmp);
tmp = NULL;
return Delete_success;
}
else
{
Link p = tmp;
tmp = tmp->next;
while(tmp != NULL)
{
if(tmp->num == n)
{
p->next = tmp->next;
free(tmp);
tmp = NULL;
return Delete_success;
}
p = tmp;
tmp = tmp->next;
}
return Delete_fail;
}
}
#endif
/***逆序链表***/
#if 0
int reverse_node(Link *head)
{
if((*head == NULL) || ((*head)->next == NULL))
{
return Reverse_fail;
}
else
{
Link temp = *head;
Link p = temp->next;
Link s = p->next;
while(temp != NULL)
{
p->next = s;
s = p;
p = temp;
temp = temp->next;
}
p->next = s;
(*head)->next = NULL;
*head = p;
return Reverse_success;
}
}
#endif
/***显示链表***/
void display(Link head)
{
Link temp = head->next;
while(temp != NULL)
{
printf("%d\n",temp->num);
temp = temp->next;
}
}
int main()
{
int i;
Link head;
Link newnode;
init(&head);
#if 1
for(i = 0;i < 10; i++)
{
Link newnode = (Link)malloc(sizeof(Node));
newnode->num = i + 1;
insert_tail(newnode,&head);
}
#endif
display(head);
#if 0
int result2 = delete_node(&head,6);
if(result2 == Delete_empty)
{
printf("The link is empty.\n");
}
if(result2 == Delete_success)
{
display(head);
}
if(result2 == Delete_fail)
{
printf("Delete fail!\n");
}
#endif
#if 0
newnode = (Link)malloc(sizeof(Node));
newnode->num = 11;
int result1 = insert_mid(newnode,&head,5);
{
if(result1 == Link_empty)
{
printf("链表为空\n");
}
if(result1 == Link_insert_success)
{
display(head);
}
else
{
printf("链表插入失败\n");
}
}
#endif
#if 0
int result3 = reverse_node(&head);
if(result3 == Reverse_fail)
{
printf("\n");
}
if(result3 == Reverse_success)
{
display(head);
}
#endif
return 0;
}
1024