#刚好前几天写了一个来练手,基本功能都有了。自己看着改改吧
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include <assert.h>
typedef int DataType;//链表结点中保存的数据类型,可以自行更改。需要注意的
//是链表打印输出中,也要更改相应的类型。
struct _Node
{
DataType data;
struct _Node *next;
}Node;
typedef struct _Node list_single ;
list_single *Node_Create(DataType data)//新建结点
{
list_single *p=NULL;
p=(list_single *)malloc(sizeof(list_single));
if(p==NULL)
{
printf("malloc fair!\n");
exit(1);
}
p->data=data;
p->next=NULL;
return p;
}
int List_Count(list_single *head)
{
int k=0;
list_single *p=head;
while(p->next)
{
p=p->next;
k++;
}
return k;
}
void Push_Head(list_single *head, DataType data)//表尾添加
{
list_single *p=Node_Create(data);
if (head==NULL)
{
head =p;
}
else
{
p->next=head->next;
head->next=p;
}
printf("Push head %c finish.\n",data);
}
void Push_Back(list_single *head, DataType data)//表头添加
{
list_single *p= NULL;
if (head==NULL)
{
head = Node_Create(data);
p=head;
}
else
{
p=head;
while (p->next)
{
p= p->next;
}
}
p->next = Node_Create(data);
printf("Push back %c finish.\n",data);
}
int Pop_Back(list_single *head)//表尾弹出
{
list_single *p= NULL;
list_single *t= NULL;
if (head==NULL)
{
return 0;
}
else
{
p=head;
while (p->next)
{
t=p;
p=p->next;
}
t->next=NULL;
free(p);
}
//printf("%c\n",p->data );
printf("Pop back finish.\n");
return List_Count(head);
}
int Pop_Head(list_single *head)//表头弹出
{
list_single *p= NULL;
if (head==NULL)
{
// printf("error:node list is empty.\n");
return 0;
}
else
{
p=head->next;
head->next=p->next;
}
free(p);
//printf("%c\n",p->data );
printf("Pop head finish.\n");
return List_Count(head);
}
int Node_Delete(list_single *head, DataType data)//删除指定内容结点
{
list_single *p= NULL;
list_single *t= NULL;
int mark =0;
if (head!=NULL)
{
p=head;
while (p->next)
{
if(p->data==data)
{
t->next=p->next;
free(p);
mark=1;
break;
}
t=p;
p=p->next;
}
}
// p->next = Node_Create(data);
if(mark)
printf("Delete %c finish.\n",data);
else
printf("Not found %c in list.\n",data);
return mark;
}
list_single *Node_Find(list_single *head, DataType data)//查找表中内容
{
list_single *p= NULL;
int mark =0;
if (head!=NULL)
{
p=head;
while (p->next)
{
//mark++;
p=p->next;
if(p->data==data)
{
mark=1;
break;
}
}
}
if(mark)
return p;
else
return NULL;
}
int Node_Insert(list_single *head, DataType after_Data,DataType data)//插入结点
{
list_single *p= NULL;
list_single *t=NULL;
list_single *np= Node_Create(data);
int mark =0;
if (head!=NULL)
{
p=head;
while (p->next)
{
p=p->next;
if(p->data==after_Data)
{
t=p->next;
p->next=np;
np->next=t;
mark=1;
break;
}
}
}
printf("Inert %c after %c finish.\n",after_Data,data);
return mark;
}
void List_Print(list_single *head)//打印链表
{
list_single *p=NULL;
assert(head);
if(head!=NULL)
{
p=head;
printf("Head");
while(p->next)
{
p=p->next;
printf(" ->%d",p->data );//注意这里的数据类型
//要与头部定义中的数据类型保持一致
}
printf("\n");
}
}
int isEmpty(list_single *head)
{
if(head->next==NULL)
return 1;
else
return 0;
}
void List_Create(list_single *head)//建立链表
{
list_single *p=NULL;
int i,n=5;//初始链表长度
p=head;
//printf("Head");
for(i=0;i<n;i++)
{
p->next=Node_Create(0);//这里可更改链表结点中的初始值,默认设置为0
//(list_single*)malloc(sizeof(list_single));
p=p->next;
// printf(" ->%c",p->data );
}
//printf("\n");
}
void Destroy(list_single* head)//销毁链表
{
list_single *p = head->next;
assert(head);
while (p->next)
{
list_single *t=p;
p=p->next;
free(t);
t=NULL;
}
//free(head);
}
int main()
{
list_single a,b;
List_Create(&a);//建表
List_Create(&b);
printf("isEmpty(a):%d\n",isEmpty(&a));//判断是否空
Push_Head(&a,100);/头部添加
Node_Insert(&a,100,50);//在100后面插入50
Push_Back(&a,200);//末尾添加
return 0;
}
温馨提示:答案为网友推荐,仅供参考