zoukankan      html  css  js  c++  java
  • 嵌入式Linux c语言常用数据结构

    链表 树 哈希表

    单向链表

    1、链表建立

    typedef struct node
    {
        int data;
        struct node *next;
    }NODE;
    NODE *create()
    {
        NODE *head,*q,*p;
        char ch;
        int flag,a;
        head=(NODE *)malloc(sizeof(NODE));
        q=head;
        scanf("%d",&flag);
        ch=getchar();
        scanf("%d",&a);
        while(ch!="!")
        {
            a!=flag;
            p=(NODE *)malloc(sizeof(NODE));
            p->data=a;
            q->next=p;
            q=p;
            ch=getchar();
            scanf("%d",&a);
        }
        q->next=NULL;
        return (head);
    }

    2、查找

    typedef struct node
    {
        int data;
        struct node *next;
    }NODE;
    NODE *find_number(NODE *head,int i)
    {
        int j;
        NODE *p;
        j=1;
        p=head->next;
        while(j<i&&(p!=NULL))
        {
            p=p->next;
            j++;
        }
        return (p);
    }

    按值查找算法如下:

    typedef struct node
    {
        int data;
        struct node *next;
    }NODE;
    NODE *find_value(NODE *head,int v)
    {
        NODE *p;
        p=head->next;
        while((p->data!=v)&&(p!=NULL))
        {
            p=p->next;
        }
        return (p);
    }

    3、求链表长度

    typedef struct node
    {
        int data;
        struct node *next;
    }NODE;
    int ListLength(NODE *head)
    {
        int length=0;
        NODE *temp;
        temp=head;
        while(temp->next!=NULL)
        {
            length++;
            temp=temp->next;
        }
        return length;
    }

    4、插入运算

    void insert(NODE *head,NODE *p,int x)
    {
        NODE *q;
        q=(NODE *)malloc(sizeof(NODE));
        q->data=x;
        if(head=NULL)
        {
            head=q;
            q->next=NULL;
        }
        else
        {
            q->next=p->next;
            p->next=q;
        }
    }
    5、删除运算

    void del(NODE *head,NODE *p,int x)
    {
        NODE *p,*q;
        q=head;
        p=head->next;
        while((p!=NULL)&&(p->data!=x))
        {
            q=p;
            p=p-next;
        }
        if(p=NULL)
            printf("cannot found\n");
        else
        {
            q->next=p-next;
            free(p);
        }
    }

  • 相关阅读:
    字符串的输入函数gets
    51nod 1113(矩阵快速幂简单题)
    矩阵快速幂基础
    hdu-2141(二分查找+暴力)
    opencv 摄像头 指定大小 数量 的图片
    Windows环境下使用tensorflow opencv的小事儿
    Vue+D3 V4 实现模块化
    Vue安装
    js 日历
    js基础
  • 原文地址:https://www.cnblogs.com/junzhkevin/p/1977958.html
Copyright © 2011-2022 走看看