数据结构—单向链表

struct.h

#ifndef _STRUCT_H       
#define _STRUCT_H

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

typedef struct infonode{
        int data;
        struct infonode *next;

}info, *pinfo;

typedef struct search_node{
        pinfo prev;
        pinfo cur;
}searchnode, *psear;

searchnode sear;

void init(pinfo *head);
void insert(pinfo head, int data);
bool search(pinfo head,int data);
bool delete(pinfo head, int data);
bool is_empty(pinfo head);
void show(pinfo head);

#endif

func.c

 #include"struct.h"
 
 //初始化
 void init(pinfo *head)
 {
         (*head) = (pinfo)malloc(sizeof(info));
         if(*head == NULL)
         {
                 perror("init malloc falied");
                 exit(1);
         }
 
         (*head)->next = NULL;
 }
 
 
 //插入
 void insert(pinfo head, int data)
 {
         pinfo new = (pinfo)malloc(sizeof(info));
         if(new == NULL)
         {
                 perror("insert malloc");
                 return ;
         }
 
         new->data = data;
         new->next= head->next;
         head->next = new;
 
 }
 
 
 //查询
 bool search(pinfo head,int data)
 {
         if(is_empty(head))
         {
                 printf("not any data in the link\n");
                 return false;
         }
 
         for(sear.prev = head,sear.cur= head->next; sear.cur != NULL; sear.prev =
  sear.cur, sear.cur = sear.cur->next)
         {
                 if(sear.cur->data == data)
                         return  true;
         }
 
         return false;
 }
 
 //删除
 bool delete(pinfo head, int data)
 {
         bool result = search(head, data);
         if(result == false)
                 return false;
 
         sear.prev->next = sear.cur->next;
 
         return true;
 }
 
 //判空
 bool is_empty(pinfo head)
 {
         if(head->next == NULL)
                 return true;
         else
                 return false;
 }
 
 //遍历
 void show(pinfo head)
 {
         if(is_empty(head))
         {
                 printf("not data\n");
                 return ;
         }                                                                                                                           while(head->next != NULL)                                                                                                   {                                                                                                                                   head = head->next;                                                                                                          printf("%d ", head->data);                                                                                          }                                                                                                                           printf("\n");                                                                                                       }

main.c

#include"struct.h"

int main(void)
{
        int a[10] = {12,123,213,234,12,123,14,133432,12};
        int i;

        pinfo head;
        init(&head);

        for(i = 0; i < 10 ; i++)
                insert(head, a[i]);
        printf("the first add:");
        show(head);

        if(search(head, 123))
                printf("find the data\n");
        else
                printf("not find data\n");

        delete(head,213);
        printf("************\nafter delete the 213:");
        show(head);

        return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值