zoukankan      html  css  js  c++  java
  • 链表

    #include<stdlib.h>
    #include<stdio.h>
    typedef struct NODE
    {
     struct NODE *next;
     int length;
     int date;
    }node;
    typedef struct NODE *list;
    int initlist(list *head)
    {
     *head=(list)malloc(sizeof(list));
     (*head)->next=NULL;
     (*head)->length=0;
     return 1;
    }
    int crelist(list h,int n)
    {
     printf("输入数据i:");
     for(int i=0;i<n;i++)
     {
      node *p;
      p=(node *)malloc(sizeof(node));
      scanf("%d",&p->date);
      p->next=h->next;
      h->next=p;
      h->length++;
     }
     return 1;
    }
    int show(list r)
    {
     node *p;
     p=(node *)malloc(sizeof(node));
     p=r->next;
     printf("length=%d ",r->length);
     while(p)
     {
      printf("%d ",p->date);
      p=p->next;
     }
     printf(" ");
     return 0;
    }
    node *found(list h,int n)
    {
      node *p;
     p=(node *)malloc(sizeof(node));
     p=h;
     int j=0;
     while(p!=NULL&&j<n)
     {
      p=p->next;j++;
     }
     return p;
    }
    int insertlist(list &h,int w0,int s)
    {
     if(w0<1||w0>h->length+1) return 0;
     node *p,*q;
     p=(node *)malloc(sizeof(node));
     q=(node *)malloc(sizeof(node));
     p=found(h,h->length-w0+1);
     q->date=s;
     q->next=p->next;
     p->next=q;
     h->length++;
     return 1;
    }
    int dele(list &r,int a)
    {
     if(a<1||a>r->length) return 0;
     node *p;
     p=(node *)malloc(sizeof(node));
     p=found(r,r->length-a);
     p->next=p->next->next;
     r->length--;
     return 1;
    }
    int main()
    {
     list w;
     initlist(&w);
     int n,date;
     printf("输入数据个数:");
     scanf("%d",&n);
     crelist(w,n);
     show(w);
     int wi,s;
     printf("输入插入数据位置和数据:");
     scanf("%d%d",&wi,&s);
     insertlist(w,wi,s);
     show(w);
     int a;
     printf("输入删除数据位置:");
     scanf("%d",&a);
     dele(w,a);
     show(w);
    }
  • 相关阅读:
    C语言数据结构(二)
    面向对象
    Java集合类汇总
    C语言运算符优先级和结合性
    c语言数据结构(一)
    浏览器间bug
    HTTP协议中的1xx,2xx,3xx,4xx,5xx状态码分别表示什么,列举常见错误码及含义
    SSL是啥?
    {转}大公司里怎样开发和部署前端代码?
    浏览器 HTTP 缓存原理分析
  • 原文地址:https://www.cnblogs.com/jarrem/p/4888936.html
Copyright © 2011-2022 走看看