美文网首页
错误的链表插入函数代码

错误的链表插入函数代码

作者: crabor | 来源:发表于2018-03-26 13:01 被阅读0次
#include <stdio.h>
#include<stdlib.h>
#include<time.h>

typedef struct NODE {
  struct NODE *next;
  int value;
} Node;

typedef enum { ERROR = 0, OK = 1 } Status;

Status Insert(Node **ppLink,int newValue){
    Node *new, *current;

    new = (Node *)malloc(sizeof(Node));
    if(new==NULL){
        return ERROR;
    }
    new->value = newValue;

    while(current=*ppLink,current!=NULL&&current->value<newValue){
        ppLink = &current->next;
    }

    *ppLink = new;
    new->next = current;

    return OK;
}
Status Insert1(Node **ppLink,int newValue){
    Node new, *current;

    new.value = newValue;

    while(current=*ppLink,current!=NULL&&current->value<newValue){
        ppLink = &current->next;
    }

    *ppLink = &new;
    new.next = current;

    return OK;
}

int main(int argc,char *argv[]){
    Node *linkedList=NULL;
    int temp;
    srand((unsigned)time(NULL));
    for (int i = 0; i < 10;i++){
        temp = rand() % 101;
        printf("%d ", temp);
        Insert(&linkedList,temp);
    }
    printf("\n");
    for (Node *p = linkedList; p!= NULL;p=p->next){
        printf("%d ", p->value);
    }
    printf("\n");
    Insert1(&linkedList, 20);
    for (Node *p = linkedList; p!= NULL;p=p->next){
        printf("%d ", p->value);
    }
    return 0;
}

相关文章

  • 错误的链表插入函数代码

  • 链表复习(一)

    链表性质——此处略 简单的链表基本结构(cpp): 实现链表插入函数——参数为Node*和int,分别表示要插入的...

  • 单链表的操作

    单链表代码定义 单链表的操作 初始化单链表 插入结点 注: L为插入的单链表,node为将要插入的结点 前插法 尾...

  • JS 实现链表

    Node 为创建节点的构造函数;LinkedList 为链表操作函数的构造函数。对链表的操作包括:插入节点、移除节...

  • 单链表的实现--c结构体实现、Python类实现

    手写了一遍链表的代码,写完神清气爽再也不害怕写链表代码了hhhhh 链表的具体代码实现,链表的功能有创建、插入、删...

  • 双链表的操作

    双链表的代码定义 双链表的操作 初始化双链表 插入 前插法 尾插法 任意位置插入 双链表的遍历输出 元素删除与双链...

  • 链表的插入2018-07-15

    1.创建一个链表,调用插入函数和输出函数 #include #include typedef struct nod...

  • 单链表反转

    单链表反转 单链表初始化 输出 反转 释放 实现代码 尚未实现 元素插入 元素删除

  • 第二章 线性表-双链表的操作2019-02-01

    犯错误的地方: 1、双链表的尾插发自己还想着跟在中间插入节点一样的写法,犯了很严重的错误,只需要这些代码就能解决问...

  • 数据结构基础--链表

    目录 基本性质 链表的分类按连接方向分类按照有无循环分类 链表问题代码实现的关键点 链表插入和删除的注意事项 链表...

网友评论

      本文标题:错误的链表插入函数代码

      本文链接:https://www.haomeiwen.com/subject/pxqrcftx.html