苏嵌 项目实战
学习日志 姓名:周文东 日期:7月24日
|
|
今日学习任务
|
|
今日任务完成情况
(详细说明本日任务是否按计划完成,开发的代码量) | 今天按任务完成。 代码见下面。 |
今日开发中出现的问题汇总
| 无 |
今日未解决问题
| 无 |
今日开发收获 | 今天主要学习了单链表的头插,尾插,中插,删除结点和逆序,经过学习,我都编程顺利完成,让我初步体会了链表的好处,以便在以后学习中运用。 |
自我评价
(是否按开发规范完成既定任务,需要改进的地方,与他人合作效果等)
| 今天在按规范完成任务,链表是自己不熟悉的一块,所以要多写链表,认真预习和复习,与他人合作效果很好。 |
其他
| 无 |
#include<stdio.h>
struct node
{
int num;
struct node *p;
};
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 = p2;
p3->num = 3;
p3->next = NULL;
#include<stdio.h>
#include<stdlib.h>
enum result
{
LINK_EMPTY = -1,
INSERT_SUCCESS,
INSERT_FAIL,
DELETE_EMPTY,
DELETE_SUCCESS,
DELETE_FAIL,
RESERVE_SUCCESS,
RESERVE_FAIL,
};
struct node
{
int num;
struct node *next;
};
typedef struct node Node;
typedef Node *Link;
void creat_node(Link *head)
{
*head = NULL;
}
void insert_head_node(Link newnode,Link *head)
{
if(*head == NULL)
{
newnode->next = *head;
*head = newnode;
}
else
{
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_EMPTY;
}
Link temp = *head;
while(temp != NULL)
{
if(temp->num == 5)
{
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;
if(*head == NULL)
{
return DELETE_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 reserve_link(Link *head)
{
if(*head == NULL || (*head)->next == NULL)
{
return RESERVE_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 RESERVE_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;
creat_node(&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);
//insert_mid_node(newnode,&head,5);
}
//delete_node(&head,2);
reserve_link(&head);
display_link(head);
/* newnode = (Link)malloc(sizeof(Node));
insert_mid_node(newnode,&head,5);
display_link(head);*/
return 0;
}