学习日志 姓名:张朋 日期:2018.07.24
|
|
今日学习任务
|
|
今日任务完成情况
(详细说明本日任务是否按计划完成,开发的代码量) | 今日任务按计划完成。
|
今日开发中出现的问题汇总
|
|
今日未解决问题
| 无 |
今日开发收获 | 通过今天的学习,我学会了怎么进行链表的创建和使用,如何实现对链表的增删改查。同时,明白了数据结构的作用以及为什么提出链表。了解了数组和链表之间的联系。熟悉了创建链表的具体过程。 |
自我评价
(是否按开发规范完成既定任务,需要改进的地方,与他人合作效果等)
| 今天可以跟上老师的步骤,通过跟着老师上课的节奏,我一步步的学会了如何创建链表,怎么对链表进行增删改查和逆序,加上课堂上的反复练习,让我对链表有了比较深刻的理解,同时,弥补了之前学习链表时不会的地方。但是,这些知识还是需要花大量的时间进行练习。这是学习过程中必须经历的过程。 |
其他
| 无 |
1.结构体的相关代码(学习链表之前的引例):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int num;
struct node *next;
};
int main()
{
struct node *head;
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;
#if 0
while(head != NULL)
{
printf("%d\n",head->num);
head = head->next;
}
#endif
while(p1 != NULL)
{
printf("%d\n",p1->num);
p1 = p1->next;
}
return 0;
}
2.不带表头的单向链表,源代码如下:
#include <stdio.h>
#include <stdlib.h>
enum number
{
LINK_EMPTY = -1,
RESULT_SUCCESS,
RESULT_FAIL,
DELETE_SUCCESS,
DELETE_FAIL,
REVERSE_SUCCESS,
REVERSE_FAIL
};
struct node
{
int num;
struct node *next;
};
typedef struct node Node;
typedef Node * Link;
void init_head(Link *head)
{
*head = NULL;
}
void insert_head_link(Link newnode,Link *head)
{
if(*head == NULL)
{
*head = newnode;
newnode->next = NULL;
}
else
{
newnode->next = *head;
*head = newnode;
}
}
int insert_mid_node(Link newnode,Link *head,int num)
{
Link temp = *head;
if(*head == NULL)
{
return LINK_EMPTY;
}
while(temp != NULL)
{
if(temp->num == num)
{
newnode->next = temp->next;
temp->next = newnode;
return RESULT_SUCCESS;
}
temp = temp->next;
}
return RESULT_FAIL;
}
void insert_tail_link(Link newnode,Link *head)
{
Link temp = *head;
if(*head == NULL)
{
newnode->next = NULL;
*head = newnode;
}
else
{
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;
newnode->next = NULL;
}
}
int delete_node(Link *head,int num)
{
Link temp = *head;
if(*head == NULL)
{
return LINK_EMPTY;
}
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 || (*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;
init_head(&head);
Link newnode;
for(i = 0; i < 10; i++)
{
newnode = (Link)malloc(sizeof(Node));
newnode->num = i + 1;
//insert_head_link(newnode,&head);
insert_tail_link(newnode,&head);
}
display_link(head);
#if 0
newnode = (Link)malloc(sizeof(Node));
newnode->num = 16;
if(insert_mid_node(newnode,&head,5) == RESULT_SUCCESS)
{
display_link(head);
}
else
{
printf("the link is empty or no find num!\n");
}
#endif
//delete_node(&head,6);
reverse_link(&head);
display_link(head);
return 0;
}
3.带表头的单向链表,源代码如下:
#include <stdio.h>
#include <stdlib.h>
enum number
{
LINK_EMPTY = -1,
RESULT_SUCCESS,
RESULT_FAIL,
DELETE_SUCCESS,
DELETE_FAIL,
REVERSE_SUCCESS,
REVERSE_FAIL
};
struct node
{
int num;
struct node *next;
};
typedef struct node Node;
typedef Node * Link;
void init_head(Link *head)
{
*head = (Link)malloc(sizeof(Node));
(*head)->next = NULL;
}
void insert_head_link(Link newnode,Link *head)
{
newnode->next = (*head)->next;
(*head)->next = newnode;
}
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 RESULT_SUCCESS;
}
temp = temp->next;
}
return RESULT_FAIL;
}
void insert_tail_link(Link newnode,Link *head)
{
Link temp = *head;
while(temp->next != NULL)
{
temp = temp->next;
}
temp->next = newnode;
newnode->next = NULL;
}
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;
init_head(&head);
Link newnode;
for(i = 0; i < 10; i++)
{
newnode = (Link)malloc(sizeof(Node));
newnode->num = i + 1;
//insert_head_link(newnode,&head);
insert_tail_link(newnode,&head);
}
display_link(head);
#if 0
newnode = (Link)malloc(sizeof(Node));
newnode->num = 16;
if(insert_mid_node(newnode,&head,5) == RESULT_SUCCESS)
{
display_link(head);
}
else
{
printf("the link is empty or no find num!\n");
}
#endif
//delete_node(&head,6);
reverse_link(&head);
display_link(head);
return 0;
}