链表的学习

链表的创建,查找,删除,打印。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define TAGLEN 20

typedef struct datanode{
      int data;
      char tag[TAGLEN];
      struct datanode * next;
}dand;

/*尾插法*/
void push_list(dand *p){
    int no;
    char tex[TAGLEN];/*两个临时变量,存储输入得数据*/
    dand *cp=(dand*)malloc(sizeof(dand));/*申请一个指针及其内存*/
    dand *aft=(dand*)malloc(sizeof(dand));

    puts("enter your data :");
    scanf("%d",&no);
    printf("ok,enter chars\n");
    getchar();
    /*for循环写入字符数组*/
    for(int i=0;i<TAGLEN;i++){
        scanf("%c",&tex[i]);
        if(tex[i] == '\n')
            break;
    }
    printf("all is %s",tex);


    cp->data = no;
    strcpy(cp->tag,tex);
    cp->next = NULL;
    if(p->next == NULL)
        p->next = cp;
    else{aft=p;
        while(aft->next!=NULL){
            aft=aft->next;
        }
        aft->next=cp;
        p=aft;
    }
}

/*打印链表*/
void display(dand *p){
    dand *cur;
    int n = 0;
    cur = p;
    while(cur != NULL){
        printf("data print,number is %d\n",n);
        printf("datd is %d\n",cur->data);
        printf("tag is %s\n",cur->tag);
        cur = cur->next;
        n++;
    }
    printf("link is empty\n");
}

/*计算节点数量*/
int count_link(dand *p){
    dand *curr;
    int n = 1;

    curr = p;
    while(curr->next != NULL){
        curr = curr->next;
        n++;
    }

    return n;
}

/*删除指定节点*/
dand * del_link(dand *p){
    int data;
    dand *cur = p;
    dand *pre = cur;

    if(p == NULL)
        return p;

    puts("enter the link you want to del");
    scanf("%d",&data);

    printf("datanode id %d\n",data);
    printf("ok1\n");
    while(cur!=NULL){
        if(cur->data!=data){
            pre = cur;
            cur=cur->next;
        }else{
            pre->next = cur->next;
            free(cur);
            cur = pre->next;
        }
    }
    printf("ok3\n");
    return p;
}

/*寻找指定节点*/
void find_link(dand *p,int numb){

    dand *cur;
    cur = p;
    int sum;

    sum = count_link(p);
    while(sum>0){
        if(cur->data == numb)
            printf("tag is %s\n",cur->tag);
        cur = cur->next;
    }
    printf("end serch");
}

void create_link(dand *p){
    int how;

    puts("ente the count you want to create");
    scanf("%d",&how);
    for(how;how>0;how--){
        push_list(p);
    }
}

int main(){
    dand *head=(dand*)malloc(sizeof(dand));
    int sum,fex;

    create_link(head);
    display(head);
    //sum = count_link(head);
    //scanf("%d",&fex);
    //find_link(head,fex);
    del_link(head);
    display(head);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值