一边画图,一边想算法真的很简单,,,数据结构(一画图就神马都清楚了)。想法是对的就应该差不多,一些小错误一调试就能找出来了,
以前调试不过就很烦,很想别人给帮忙看看,改改。殊不知别人是很难改好你的程序的,改好了也应该是别人的新算法咯
现在觉得调试还可以哦
PS:以下代码都是完整且调试通过了的哦
"Link_list.h"
typedef int ElemType;
typedef struct _Linklist
{
ElemType data;
struct _Linklist *next;
}Link;
//(带头结点的实现)
//1.初始化单链表
void init_list(Link *head, ElemType a);
//2.判断单链表是否为空,若为空链表,返回 true;否则返回 false。
bool is_empty(Link *head);
//3.获取单链表长度
int get_length(Link *head);
//4.单链表头插法
bool insert_head(Link *head, ElemType a);
//5.单链表尾插法
bool insert_tail(Link *head, ElemType value);
//6.销毁单链表
void destory_list(Link *head);
//7.显示链表全部内容
void show_list(Link *head);
//8.根据数据域删除结点
void delete_node(Link *head ,ElemType a);
//9.根据数据域获得结点
int get_node(Link *head, ElemType a);
//10.根据位置删除结点
void delete_pos(Link *head, int pos);
//11.根据位置插入结点
void insert_by_pos(Link *head, int pos, ElemType a);
"Link_list.cpp"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"Link_list.h"
//初始化
void init_list(Link *head, ElemType a)
{
Link *tmp = (Link *)malloc(sizeof(Link )*1);
assert(tmp != NULL);
tmp->data = a;
tmp->next = NULL;
head->next = tmp;
}
//判空
bool is_empty(Link *head)
{
if(head->next == NULL)
{
return true;
}
return false;
}
//获得链表长度
int get_length(Link *head)
{
int length = 0;
while(head->next != NULL)
{
length++;
head = head->next ;
}
return length;
}
//insert by head
bool insert_head(Link *head, ElemType a)
{
if(head->next == NULL)
{
return false;
}
Link *tmp = (Link*)malloc(sizeof(Link)*1);
tmp->next = head->next;
head->next = tmp;
tmp->data = a;
return true;
}
//insert by tail
bool insert_tail(Link *head, ElemType value)
{
if(head->next == NULL)
{
return false;
}
Link * tmp= (Link *)malloc(sizeof(Link)*1);
Link *p = NULL;
p = head->next ;
while(p->next != NULL)
{
p = p->next;
}
p->next = tmp;
tmp->next = NULL;
tmp->data =value;
return true;
}
//destroy List /*放在最后*/
void destory_list(Link *head)
{
Link *p = head->next ;
Link *f = head->next ;
while(f != NULL)
{
f = p->next;
free(p);
p = f;
}
head->next = NULL;
}
//print
void show_list(Link *head)
{
Link *p = head->next ;
while(p != NULL)
{
printf("%d\n",p->data );
p = p->next ;
}
}
//根据数据域删除结点
void delete_node(Link *head ,ElemType a)
{
Link *p = head->next ;
Link *f = head->next ;
while(f->data != a)
{
p = f;
f = p->next ;
}
p->next = f->next ;
free(f);
}
//根据数据域获得结点
int get_node(Link *head, ElemType a)
{
int count = 0;
Link *p = head->next ;
while(p != NULL)
{
++count;
if(p->data == a)
{
return count;
}
p = p->next;
}
}
//10.根据位置删除结点
void delete_pos(Link *head, int pos)
{
Link *p = head->next ;
Link *f = head->next ;
if(pos == 1)
{
head->next = p->next;
free(p);
}
else
{
while(pos--)
{
if(pos == 1)
{
f = p;
}
p = f->next;
}
f->next = p->next ;
free(p);
}
}
//11.根据位置插入结点
void insert_by_pos(Link *head, int pos, ElemType a)
{
Link * tmp= (Link *)malloc(sizeof(Link)*1);
Link *p = head->next ;
Link *f = head->next ;
if(pos == 1)
{
insert_head(head,a);
}
else
{
while(pos)
{
if(pos == 1)
{
f = p;
}
p = f->next;
pos--;
}
f->next = tmp;
tmp->next = p;
tmp->data = a;
}
}
"main.cpp"
#include<stdio.h>
#include"Link_list.h"
int main()
{
Link s;
init_list(&s,12);
insert_head(&s,-12);
insert_tail(&s,100);
insert_by_pos(&s,2,520);
//delete_pos(&s,2);
//printf("%d\n",get_node(&s,100));
show_list(&s);
//printf("%d\n",get_length(&s));
//delete_node(&s,12);
//show_list(&s);
//destory_list(&s);
return 0;
}