zoukankan      html  css  js  c++  java
  • 单链表的基本操作(C语言)数据结构

    1、定义单链表类型并动态创建单链表;

    2、实现单链表的取元素操作、插入操作和删除操作;

    3、实现输出单链表中各元素值的操作;

    4、将单链表中的最小元素移到最前面。

    #include<stdio.h>
    #include<stdlib.h>
    #define ok 1
    #define N 100
    typedef struct LNode
    {
    int no;
    struct LNode *next;
    }LNode,*LinkList;
    void CreateList(LinkList &L) //创建链表
    {


    int i;
    LNode *p,*q;
    L=(LNode*)malloc(sizeof(LNode));
    if(L!=NULL)
    {
    p=L;
    for(i=0;i<N;i++)
    {
    q=(LNode*)malloc(sizeof(LNode));
    if(q!=NULL)
    { printf("输入第%d个人的学号: ",i+1); scanf("%d",&q->no);

    if(q->no==404){
    break;

    }
    q->next=NULL;
    p->next=q;
    p=q;


    }
    }

    }
    printf(" ");

    }

    int ListDelete(LinkList &L,int post)
    { int i;
    LNode *cur,*pre;
    cur=L;
    if(0==post)
    {
    return 1;
    }else
    {
    if(1==post)
    {
    cur=cur->next;
    L->next=cur->next;
    free(cur);
    }else{
    for(i=2;i<post+1;i++)
    {
    cur=cur->next;
    pre=cur;
    }
    cur=cur->next;
    pre->next=cur->next;
    free(cur);
    }

    return 1;
    }
    }

    /*void DeleteArea(LinkList &L){
    int i,t,max,min;
    printf("请输入要删除的学号的区域: ");
    scanf("%d%d",&min,&max);

    t=max-min;
    LNode *cur,*pre;
    cur=L;
    if(min==1){
    for(i=0;i<=t;i++){
    cur=cur->next;
    L->next=cur->next;
    free(cur);
    }
    }

    }
    */
    void DeleteArea(LinkList &L){
    int max,min;
    printf("请输入要删除的学号的区域: ");
    scanf("%d%d",&min,&max);


    LNode *cur,*pre;

    cur=L;
    while(cur->next){

    if(cur->next->no>min&&cur->next->no<max){


    pre=cur->next;
    cur->next=pre->next;

    free(pre);

    } else{
    cur=cur->next;
    }
    }


    }


    int Insert(LinkList &L,int post)
    {
    int i;
    LNode *cur,*pre,*node;
    if(L!=NULL)
    {
    cur=L;
    node=(LNode*)malloc(sizeof(LNode));
    if(node!=NULL)
    {
    printf("请输入新人的学号: ");
    scanf("%d",&node->no);
    if(1==post)
    {
    node->next=L->next;
    L->next=node;
    }else{
    for(i=2;i<post+2;i++)
    {
    pre=cur;
    cur=cur->next;
    }
    node->next=pre->next;
    pre->next=node;
    }
    }
    }
    printf(" ");
    return ok;
    }

    void Print(LinkList &L) //输出学生信息
    {
    int post=1;
    LNode *cur;
    cur=L->next;
    printf("当前的学生信息如下所示: ");
    while(cur!=NULL)
    { printf("第%d个人的学号为:%d ",post,cur->no); cur=cur->next;
    post++;
    }

    }
    int main()
    {
    int mm,post,i,max,min;
    LNode *head;
    CreateList(head);

    Print(head);
    for(i=0;i<1;i++)
    {
    printf("请输入要插入学号的位置: ");
    scanf("%d",&post);
    Insert(head,post);
    Print(head);


    printf("请输入要删除的学号的位置: ");
    scanf("%d",&mm);
    ListDelete(head,mm);
    Print(head);


    DeleteArea(head);
    Print(head);

    }

    return 0;}

  • 相关阅读:
    [Python]爬虫v0.1
    [Python]同是新手的我,分享一些经验
    [python]闭包到底是什么鬼?
    测试Flask应用_学习笔记
    Flask模板_学习笔记
    SQL Server Alwayson概念总结
    JDBC数据库编程:ResultSet接口
    JDBC操作,执行数据库更新操作
    接口怎么实例化?
    java数据库编程:JDBC操作及数据库
  • 原文地址:https://www.cnblogs.com/xufeng123/p/7788427.html
Copyright © 2011-2022 走看看