zoukankan      html  css  js  c++  java
  • 数据结构(复习) -------链表

    关于链表是最基础的数据结构,,在此不再多说;;;;只贴出一些简单代码!!!!!!!!!!!!!!!!

    // 关于数据结构的总结与复习  Coding
    
    // 1.线性表
    #include <cstdio>
    #include <cstdlib>
    #define ok 1
    #define error 0
    //#define _OJ_
    
    typedef struct Lnode
    {
        int data;
        struct Lnode *next;
    } Lnode, *Linklist;
    
    Linklist
    Init_List(void)
    //对链表的建立采用正序的方式插入
    {
        int i, n;
        Linklist L, p, head;
        L = (Linklist) malloc (sizeof(Lnode));
        head = L;
        scanf("%d", &n);
    
        for (i = 0; i < n; i++) {
        p = (Linklist) malloc (sizeof(Lnode));
        scanf("%d", &p->data);
        L->next = p;
        L = p;
        }
        L->next = NULL;
    
        return head;
    }
    
    Linklist
    Merge_List(Linklist La, Linklist Lb)
    //对两个链表的合并
    {
        Linklist Lc, Pa, Pb, Pc;
        Lc = (Linklist) malloc (sizeof(Lnode));
        Pa = La->next;    Pb = Lb->next;  Pc = Lc;
        printf("合并两个链表:");
        while (Pa && Pb) {
            if(Pa->data < Pb->data){
                Pc->next = Pa;  Pc = Pc->next;  Pa = Pa->next;
            }else{
                Pc->next = Pb;  Pc = Pc->next;  Pb = Pb->next;
            }
      }
    
        Pc->next = Pa ? Pa : Pb;
        return Lc;
    }
    
    int
    List_Inster(Linklist L, int i, int e)
    //向顺序表中插入元素
    {
        int j = 0;
        Linklist p, s;
        p = L;
        printf("在位置%d插入%d", i, e);
        while (p && j < i - 1) {       //总是找到此元素的前驱
            p = p->next;    j++;
        }
    
        if(p == NULL && j == 1) {
        printf("error
    ");    return 0;
        }
    
        s = (Linklist) malloc (sizeof(Lnode));
        s->data = e;    s->next = p->next;
        p->next = s;
    }
    
    int
    List_Delete(Linklist L, int i)
    //对链表进行删除操作
    {
        int j = 0, e;
        Linklist p, q;
        p = L;
        printf("删除第%d个元素 :", i);
        while(p->next && j < i - 1) {     //前一个节点(前驱)同理j = 1时  j < i
            p = p->next;    j++;
        }
    
        if(p->next == NULL && j == 0) {
            printf("error
    ");    return 0;
        }
    
        q = p->next;    p->next = q->next;
        e = q->data;
        free(q);
    
        return e;
    }
    
    void
    print(Linklist L)
    //打印输出
    {
        Linklist p;
        p = L->next;    printf("打印链表 :");
        while (p) {
            printf("%d ", p->data);
            p = p->next;
        }
        printf("
    ");
    }
    
    
    int main(int argc, char const *argv[]) {
    #ifndef _OJ_ //ONLINE JUDGE
           freopen("input.txt", "r", stdin);
           //freopen("output.txt", "w", stdout);
    #endif
        
        int n;
        Linklist L, p, q;
    
        L = Init_List();    p = Init_List();
    
        print(L);
    
        List_Inster(L, 2, 5);
    
        print(L);
    
        List_Delete(L, 3);
    
        print(L);
    
        q = Merge_List(L, p);
    
        print(q);
    
        return 0;
    }
    ---------------------------------------------------------------------------------------
    coding --------------------------------
    
  • 相关阅读:
    解决取消input标签中disabled属性
    【转】jQuery源码分析-13 CSS操作-CSS-类样式-addClass+removeClass+toggleClass+hasClass
    Bigpipe学习【转】
    用户请求的生命周期[传统模式与BigPipe]
    gcc扩展语法一:在表达式中的语句和声明
    分享一个关于pthread线程栈在mm_struct里面的分布问题
    neo4j 安装步骤 转自:http://blog.csdn.net/luoluowushengmimi/article/details/19987995
    sudoers文件解析 转自:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=1971013
    匿名union
    VMware虚拟机修改BIOS启动项
  • 原文地址:https://www.cnblogs.com/airfand/p/5059164.html
Copyright © 2011-2022 走看看