给你个完整的:
#include <stdio.h>
#include <stdlib.h>
typedef int data_t;
typedef struct linknode{
data_t data;
struct linknode *next;//结构体不完整类型,可以定义指针,但是不能引用内容
}link_t;
link_t *creat_link()
{
link_t *head;
head = malloc(sizeof(link_t));
head->next = NULL;
return head;
}
/*在链表的头结点 之后 第零个元素之前插入以新数据*/
int ins_head_link(link_t *head,const data_t *data)
{
//1.申请空间
//2.保存数据,指针指向,插入位置的下一个元素
//3.头结点里的指针指向,新结点
link_t *newnode;
newnode = malloc(sizeof(link_t));
newnode->data = *data;
newnode->next = head->next;
head->next = newnode;
return 0;
}
int ins_index_link(link_t *head,int index,const data_t *data)
{
//1.定位
//2.插入
while(index -- ){
head = head->next;
if(NULL == head)
return -1;
}
ins_head_link(head,data);
return 0;
}
int del_head_link(link_t *head)
{
link_t *temp;
if(NULL == head->next)
return -1;
temp = head->next;// (*head).next
head->next = temp->next;// head->next->next;
free(temp);
return 0;
}
int del_index_link(link_t *head,int index)
{
if(NULL == head->next)
return -1;
//1.定位到要删除结点的前一个
while(index --){
head = head->next;
if(NULL == head->next)
return -1;
}
//2.执行删除
del_head_link(head);
return 0;
}
/*找到一个元素并 返回该元素结点的前一个结点的地址*/
link_t *locate_link(link_t *head,const data_t *data)
{
while(NULL != head->next){
if(*data == head->next->data)
return head;
head = head->next;
}
return NULL;
}
int del_locate_link(link_t *head,const data_t *data)
{
head = locate_link(head,data);
if(NULL != head)
return del_head_link(head);
return -1;
}
int ins_locate_link(link_t *head,const data_t *data,const data_t *newdata)
{
head = locate_link(head,data);
if(NULL != head)
return ins_head_link(head,newdata);
return -1;
}
int get_head_link(link_t *head,data_t *databuf)
{
if(NULL == head->next)
return -1;
*databuf = head->next->data;
return 0;
}
int length_link(link_t *head)
{
link_t *h;
int count = 0;
h = head->next;
while(NULL != h){
count ++;
h = h->next;
}
return count;
}
int empty_link(link_t *head)
{
return NULL == head->next;
}
int change_index_link(link_t *head,int index,const data_t *newdata)
{
if(NULL == head->next)
return -1;
//1.定位到要删除结点的前一个
while(index --){
head = head->next;
if(NULL == head->next)
return -1;
}
//2.执行更新
head->next->data = *newdata;
return 0;
}
int clean_link(link_t *head)
{
while(0 == del_head_link(head))
;
return 0;
}
int dis_link(link_t **headp)
{
clean_link(*headp);
free(*headp);
*headp = NULL;
return 0;
}
void print_link(link_t *head)
{
link_t *h;
h = head->next;
while(NULL != h){
printf("%d ",h->data);
h = h->next;
}
printf("\n");
}
int main(int argc, const char *argv[])
{
link_t *head;
link_t *node;
int i;
data_t buf;
head = creat_link();
for(i = 7;i >= 0;i --){
ins_head_link(head,&i);
}
print_link(head);
buf = 250;
ins_index_link(head,9,&buf);
// del_index_link(head,8);
print_link(head);
buf = 0;
node = locate_link(head,&buf);
if(NULL != node)
printf(">>:%d\n",node->next->data);
else
printf("not fund\n");
buf = 123;
change_index_link(head,5,&buf);
print_link(head);
del_locate_link(head,&buf);
print_link(head);
dis_link(&head);
return 0;
}
温馨提示:内容为网友见解,仅供参考