今日学习任务
| 1、熟练掌握单链表
|
今日任务完成情况
|
今天的任务基本完成。 1、主要学习了有表头和无表头单链表 2、学习了结点(头插,尾插等) 3、删除结点 4、逆序
|
今日开发中出现的问题汇总
| 无 |
今日未解决问题
|
无 |
今日开发收获 | 今天学习了有表头和无表头单链表的相关知识,让我对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;
}