数据结构c语言版链表adt,链表ADT C语言实现

链表ADT

自己打的单链表

希望代码对大家有帮助哈哈,很全。

基本实现了应该有的功能

1.判断链表是否为空

2.判断链表是否为满

3.节点数量

4.链表的遍历

5.链表节点的替换

6.链表节点的寻找

7.链表节点的插入

8.链表节点的添加

8f6e9dab92229a1c276f3823bee4850d.png

#define true 1

#define false 0

#include

#include

struct list

{int data;

struct list * next;

};

typedef struct list List;

int ListIsEmpty( List * head);

unsigned int ListItemCount( List * head);

void Traverse( List * head);

void Replace( List **ptr,int a,List * target);

List * SeekItem( List * head,int a);

bool InsertItem( List *head,int a,List * target);

int InsertHeadItem(List ** ptr,List * target);

List * AddItem( List * head);

int main(void)

{

}

int ListIsEmpty( List * head)

{

if(head==NULL)

return true;

else return false;

}

unsigned int ListItemCount( List * head)

{

unsigned int count=0;

List * p=head;

while(p!=NULL){

++count;

p=p->next;

}

return count;

}

void Traverse( List * head)

{

int count = 0;

List * p=head;

while(p!=NULL){

++count;

printf("%d%10d\n",count,p->data);

p=p->next;

}

}

void Replace( List **ptr,int a,List * target)//Replace(ptr,a,target);List **ptr 为了能替换第一个节点 ,需要改变头指针的值

{

List * p=*ptr;

List * pr=p;

if(*ptr==NULL) {

printf("nothing!");

exit(0);

}

while(p!=NULL&&p->data!=a){

pr=p;

p=p->next;

}

if(p==NULL){

printf("not found!");

exit(1);

}

if(p!=NULL){

if(*ptr==p){

target->next=p->next;

*ptr=target;

free(p);

}

else{

target->next=p->next;

pr->next=target;

free(p);

}

}

}

List * SeekItem( List * head,int a)

{

int count=0;

List * p,* pr;

p=head;

pr=p;

if(head==NULL){

printf("nothing!");

exit(0);

}

while(p!=NULL){

count++;

pr=p;

if(p->data==a){

printf("find it , it's %d\n",count);

return p;

}

p=p->next;

}

if(p=NULL){

printf("not found!");

return NULL;

}

}

bool InsertItem( List * head,int a,List * target)

{

List * p;

p=SeekItem(head,a);

if(p==NULL){

printf("not found !");

return false;

}

target->next=p->next;

p->next=target;

return true;

}

int InsertHeadItem(List ** ptr,List * target)

{

target->next=*ptr;

*ptr=target;

return true;

}

List *AddItem( List * head)

{

List * pr=head;

List * p=NULL;

p=(List *)malloc(sizeof(List));

if(p==NULL){

printf("can't' malloc!");

}

p->next=NULL;

printf("please input data :");

scanf("%d",&p->data);

if(head==NULL){

head=p;

}

else{

while(pr->next!=NULL){

pr=pr->next;

printf("text!\n");

}

pr->next=p;

}

Traverse(head);

return head;

}

原创 转载注明出处

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值