学习日志 姓名: 张福辉 日期:2018.7.24
|
|
今日学习任务
| 1.无表头单向链表的相关操作 2.有表头单向链表的相关操作 |
今日任务完成情况
(详细说明本日任务是否按计划完成,开发的代码量) | 按要求完成任务 |
今日开发中出现的问题汇总
| 无 |
今日未解决问题
| 无 |
今日开发收获 | 对单项链表的创建、添加节点、删除节点、倒序有了深入理解,对有无表头的链表的不同有了了解 |
自我评价
(是否按开发规范完成既定任务,需要改进的地方,与他人合作效果等)
| 按规定完成任务 |
其他
| 无
|
#include <stdio.h>
#include <stdlib.h>
enum RESULT
{
LINK_NULL,
INSERT_SUCCESS,
INSERT_FAIL,
DELETE_SUCCESS,
DELETE_FAIL,
REVERSE_SUCCESS,
REVERSE_FAIL
};
struct node
{
int num;
struct node *next;
};
typedef struct node Node;
typedef Node * Link;
void create_link(Link *head)
{
*head = NULL;
}
void insert_head_node(Link newnode, Link *head)
{
newnode->next = *head;
*head = newnode;
}
void insert_tail_node(Link newnode, Link *head)
{
if(*head == NULL)
{
newnode->next = NULL;
*head = newnode;
}
else
{
Link temp = *head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;
newnode->next = NULL;
}
}
int insert_mid_node(Link newnode, Link *head, int num)
{
if(*head == NULL)
{
return LINK_NULL;
}
Link temp = *head;
while(temp != NULL)
{
if(temp->num == num)
{
newnode->next = temp->next;
temp->next = newnode;
return INSERT_SUCCESS;
}
temp = temp->next;
}
return INSERT_FAIL;
}
int delete_node(Link *head, int num)
{
if(*head == NULL)
{
return LINK_NULL;
}
Link temp = *head;
if((*head)->num == num)
{
(*head) = (*head)->next;
free(temp);
temp = NULL;
return DELETE_SUCCESS;
}
else
{
Link p = temp;
temp = temp->next;
while(temp != NULL)
{
if(temp->num == num)
{
p->next = temp->next;
free(temp);
temp = NULL;
return DELETE_SUCCESS;
}
p = temp;
temp = temp->next;
}
return DELETE_FAIL;
}
}
int reverse_link(Link *head)
{
if(*head == NULL)
{
return LINK_NULL;
}
if((*head)->next == NULL)
{
return REVERSE_FAIL;
}
Link p = *head;
Link s = p->next;
Link t = s->next;
while(t != NULL)
{
s->next = p;
p = s;
s = t;
t = t->next;
}
s->next = p;
(*head)->next = NULL;
*head = s;
return REVERSE_SUCCESS;
}
void display_link(Link head)
{
Link temp = head;
while(temp != NULL)
{
printf("%d\n", temp->num);
temp = temp->next;
}
}
int main()
{
int i;
Link head;
Link newnode;
create_link(&head);
for(i = 0; i < 10; i++)
{
newnode = (Link)malloc(sizeof(Node));
newnode->num = i + 1;
//insert_head_node(newnode, &head);
insert_tail_node(newnode, &head);
}
display_link(head);
reverse_link(&head);
display_link(head);
#if 0
delete_node(&head, 5);
display_link(head);
newnode = (Link)malloc(sizeof(Node));
newnode->num = 100;
insert_mid_node(newnode, &head, 6);
display_link(head);
#endif
return 0;
}
/*********************************************/
#include <stdio.h>
#include <stdlib.h>
enum RESULT
{
LINK_NULL,
INSERT_SUCCESS,
INSERT_FAIL,
DELETE_SUCCESS,
DELETE_FAIL,
REVERSE_SUCCESS,
REVERSE_FAIL
};
struct node
{
int num;
struct node *next;
};
typedef struct node Node;
typedef Node * Link;
void create_link(Link *head)
{
*head = (Link)malloc(sizeof(Node));
(*head)->next = NULL;
}
void insert_head_node(Link newnode, Link *head)
{
newnode->next = (*head)->next;
(*head)->next = newnode;
}
void insert_tail_node(Link newnode, Link *head)
{
Link temp = *head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;
newnode->next = NULL;
}
int insert_mid_node(Link newnode, Link *head, int num)
{
Link temp = (*head)->next;
while(temp != NULL)
{
if(temp->num == num)
{
newnode->next = temp->next;
temp->next = newnode;
return INSERT_SUCCESS;
}
temp = temp->next;
}
return INSERT_FAIL;
}
int delete_node(Link *head, int num)
{
Link temp = *head;
Link p = temp;
temp = temp->next;
while(temp != NULL)
{
if(temp->num == num)
{
p->next = temp->next;
free(temp);
temp = NULL;
return DELETE_SUCCESS;
}
p = temp;
temp = temp->next;
}
return DELETE_FAIL;
}
int reverse_link(Link *head)
{
if((*head)->next == NULL || (*head)->next->next == NULL)
{
return REVERSE_FAIL;
}
Link p = *head;
Link s = p->next;
Link t = s->next;
while(t != NULL)
{
s->next = p;
p = s;
s = t;
t = t->next;
}
s->next = p;
(*head)->next->next = NULL;
(*head)->next = s;
return REVERSE_SUCCESS;
}
void display_link(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;
create_link(&head);
for(i = 0; i < 10; i++)
{
newnode = (Link)malloc(sizeof(Node));
newnode->num = i + 1;
insert_head_node(newnode, &head);
//insert_tail_node(newnode, &head);
}
display_link(head);
//reverse_link(&head);
//display_link(head);
//delete_node(&head, 5);
//display_link(head);
//newnode = (Link)malloc(sizeof(Node));
//newnode->num = 100;
//insert_mid_node(newnode, &head, 6);
//display_link(head);
return 0;
}