学习日志 姓名:赵俊豪 日期:7.24
|
|
今日学习任务
| 嵌入式C语言开发实战 |
今日任务完成情况
(详细说明本日任务是否按计划完成,开发的代码量) | 无表头链表的操作和有表头链表的操作其中包括对链表进行头插、尾插、中间插,指定删除、逆序的操作 |
今日开发中出现的问题汇总
| 无 |
今日未解决问题
| 无 |
今日开发收获 | 今天解决了以为很难的链表的基本操作 |
自我评价
(是否按开发规范完成既定任务,需要改进的地方,与他人合作效果等)
| 今天在学习过程中能及时跟进老师的进度及要求 |
其他
| 无 |
#include <stdio.h>
#include <stdlib.h>
enum RESULT
{
LINK_EMPTRY,
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_EMPTRY;
}
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_EMPTRY;
}
Link temp = *head;
if((temp)->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()
{
Link head;
Link newnode;
create_link(&head);
int i;
for(i = 0; i < 10; i++)
{
newnode = (Link)malloc(sizeof(Node));
newnode->num = i + 1;
insert_tail_node(newnode,&head);
//insert_head_node(newnode,&head);
}
display_link(head);
#if 0
newnode = (Link)malloc(sizeof(Node));
newnode->num = 11;
insert_mid_node(newnode,&head,5);
#endif
//delete_node(&head,1);
reverse_link(&head);
display_link(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
enum RESULT
{
LINK_EMPTRY,
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;
*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()
{
Link head;
Link newnode;
create_link(&head);
int i;
for(i = 0; i < 10; i++)
{
newnode = (Link)malloc(sizeof(Node));
newnode->num = i + 1;
insert_tail_node(newnode,&head);
//insert_head_node(newnode,&head);
}
display_link(head);
#if 0
newnode = (Link)malloc(sizeof(Node));
newnode->num = 11;
insert_mid_node(newnode,&head,5);
#endif
//delete_node(&head,1);
reverse_link(&head);
display_link(head);
return 0;
}