zoukankan      html  css  js  c++  java
  • 线性表的链式存储

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <stdlib.h>
    #include <string.h>
    #include <iomanip>
    #define N 500010
    #define INF 10000000
    #define LL long long
    #define eps 10E-9
    #define mem(a)  memset(a,0,sizeof(a))
    #define w(a)   while(a)
    #define s(a)   scanf("%d",&a)
    #define ss(a,b)   scanf("%d%d",&a,&b)
    #define sss(a,b,c)   scanf("%lld%lld%lld",&a,&b,&c)
    #define MAXN 9999
    #define MAXSIZE 10
    #define DLEN 4
    #define MAXN 9999
    #define MAXSIZE 10
    #define DLEN 4
    using namespace std;
    typedef struct node
    {
        int data;
        struct node *next;
    } Node;
    void create(Node **p)
    {
        *p=NULL;
    }
    int my_insert(Node **p, int x)
    {
        Node *p1;
        p1 = (Node *) malloc(sizeof (Node));
        if (p1 == NULL)
            return false;
        Node *p2=*p;
        while(p2 != NULL)
        {
            if (p2->data == x)
                break;
            p2= p2->next;
        }
        p1->data = x;
        p1->next = *p;
        *p = p1;
        return true;
    }
    int my_delete(Node **head, int x)
    {
        Node *p=*head, *q;
        if (p->data == x)//考虑头结点就是要删除的元素
        {
            *head = (*head)->next;
            free(p);
            return true;
        }
        else
        {
            q = p; p = p->next; //q指向前一个节点,p指向下一个节点
            while(p != NULL)
            {
                if (p->data == x)
                {
                    q->next = p->next;
                    free(p);
                    return true;
                }
                q = p; p = p->next;
            }
        }
        return false;
    }
    int my_find(Node **head, int x)
    {
        Node *p=*head;
        while(p != NULL)
        {
            if (p->data == x)
                break;
            p = p->next;
        }
        return p->data;
    }
    void my_clear(Node **head)
    {
        Node *p=*head,*q;
        while (p != NULL)
        {
            q = p;
            p = p->next;
            free(q);
        }
    }
    void my_showalldata(Node **head)
    {
        Node *p=*head,*q;
        while (p != NULL)
        {
            cout<<p->data<<" ";
            p = p->next;
        }
        cout<<endl;
    }
    int main()
    {
        int x, n;
        Node *head;
        create(&head);
        cout<<"input the count(n) of data :"<<endl;
        cin>>n;
        cout<<"input the n'data:"<<endl;
        while(n--){
            cin>>x;
            my_insert(&head,x);
        }
        cout<<"this is all the data of the table:"<<endl;
        my_showalldata(&head);
        cout<<"input the data you want to delete:"<<endl;
        cin>>x;
        my_delete(&head,x);
        my_showalldata(&head);
        cout<<"input the data you want to find:"<<endl;
        cin>>x;
        cout<<"the data you want find is:"<<my_find(&head,x)<<endl;
       // my_clear(&head);// formating
        free(head);
        return 0;
    }

  • 相关阅读:
    利用heroku+mongoLab 部署Node 运用
    css常见解决方案
    JavaScript数组基本用法
    ES5中数组的新方法
    JavaScript闭包详解
    力扣第991题 坏了的计算器
    力扣第1189题 “气球” 的最大数量
    力扣第142题 环形链表 II
    力扣第260题 只出现一次的数字 III
    力扣第141题 环形链表
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/7105270.html
Copyright © 2011-2022 走看看