zoukankan      html  css  js  c++  java
  • 单链表的基本操作

    
    
    #include<stdio.h>
    #include<stdlib.h>
    typedef struct Node *List;
    struct Node {
        char data;
        List next;
    };
    //头插法的建立
    /*void CreatList(List *L) {
        List s;
        char e;
        (*L) =(List)malloc(sizeof(struct Node));
        (*L)->next = NULL;
        printf("开始输入数据
    ");
        scanf("%c", &e);
        while (e != '
    ') {
            s = (List)malloc(sizeof(struct Node));
            s->data = e;
            s->next = (*L)->next;
            (*L)->next = s;
            scanf("%c", &e);
        }
    }*/
    //尾插法的建立
    void CreatListRear(List *L) {
        List s,Rear;
        char c;
        (*L) = (List)malloc(sizeof(struct Node));//建立头指针
        (*L)->next = NULL;
        Rear = (*L);//Rear记录最后一个元素
        printf("开始输入数据尾插法的
    ");
        scanf("%c", &c);
        while (c != '
    ') {
            s = (List)malloc(sizeof(struct Node));//新建结点
            s->data = c;
            s->next = NULL;
            Rear->next = s; //新建结点连在前面结点后边
            Rear = s;       //Rear记录最后一个元素
            scanf("%c", &c); //输入回车则停止插入元素
        }
    }
    //查找数据位置返回地址
    List Poisition(List L, int i) {
        int j = 0;
        while (L != NULL&&j < i) {
            L = L->next;
            j++;
        }
        return L;
    }
    //插入
    void InsertList(List L) {
        int i;
        char e;
        List p,s;
        printf("在第几处插入
    ");
        scanf("%d", &i);
        getchar();//吸收回车符
        printf("插入的数据是
    ");
        scanf("%c", &e);
        p = Poisition(L, i-1);
        s = (List)malloc(sizeof(struct Node));
        s->data = e;
        s->next = p->next; 
        p->next = s;
    }
    void PrintList(List L) {
        L = L->next;
        while (L != NULL) {
            printf("%c", L->data);
            L = L->next;
        }
    }
    void DeleteList(List L) {
        int i;
        List s,p;
        printf("删除第几处的数据");
        scanf("%d", &i);
        p = Poisition(L, i - 1);
        s = p->next;
        p->next = s->next;
        free(s);
    }
    int main() {
        List L,s;
        //CreatList(&L);
        CreatListRear(&s);
        PrintList(s);
        InsertList(s);
        PrintList(s);
        DeleteList(s);
        PrintList(s);
    }
     
  • 相关阅读:
    TortoiseGit 合并主分支代码.pull request
    MVC EF 修改某些值
    MVC 提交表单
    MVC 统计之 自定义 列表
    MVC 起始页修改 区域
    asp.net C# MVC DropDownList
    asp.net C# MVC 提交表单后清空表单
    Python 线程启动的四种方式
    Git 命令的学习
    Node.js v10.1.0 Documentation
  • 原文地址:https://www.cnblogs.com/yudongxuan/p/7645020.html
Copyright © 2011-2022 走看看