• C语言单链表的实现


    //

    //  main.c

    //  gfhjhgdf

    //

    //  Created by chenhao on 13-12-23.

    //  Copyright (c) 2013 chenhao. All rights reserved.

    //

    #include"stdio.h"

    #include <stdlib.h>

    typedefstruct List_Node{

    int info;

    struct List_Node *next;

    }node;

    //链表长度

    int Count_Node(node *head)

    {

    node *p;

    int num = 0;

    if (head == NULL)

    {

            return 0;

    }

    p = head->next;

    while(p!=NULL)

    {

    num++;

    p = p->next;

    }

    return num;

    }

    //按值查找

    void FindByValue(node *head, int v)

    {

    node *p;

    int i = 0, num = 0;

    if (head == NULL)

    {

    return;

    }

    p = head->next;

    while(p != NULL)

    {

    i++;

    if (p->info == v)

    {

    num++;

    printf("%d ",i);

    }

    p = p->next;

    }

    printf("找到%d个等于%d的值",num,v);

    }

    //插入元素

    void InsertItem(node *head, int loc, int num)

    {

    node *p, *pre, *s;

    int i = 0;

    if (loc > Count_Node(head)||loc < 0)

    {

    printf("插入位置错误 ");

    return;

    }

    if (head == NULL)

    {

    return;

    }

    pre = head;

    p = head->next;

    while(i != loc)

    {

    i++;

    pre = p;

    p = p->next;

    }

    s = (node*)malloc(sizeof(node));

    s->info = num;

    s->next = p;

    pre->next = s;

    }

    //删除元素

    int deleteItem(node *head, int loc)

    {

    node *p, *pre;

    int i=1, num=0;

    if (loc>Count_Node(head)||loc<=0)

    {

    printf("删除位置错误 ");

    return0;

    }

    if (head == NULL)

    {

    return0;

    }

    pre = head;

    p = pre->next;

    while(i!=loc)

    {

    i++;

    pre = p;

    p=p->next;

    }

        

    num = p->info;

    pre->next = p->next;

    p->next = NULL;

    free(p);

    return num;

    }

    //清空链表

    void FlushLink(node *head)

    {

    node *p;

    if (head == NULL)

    {

    return;

    }

    p = head->next;

    head->next = NULL;

    free(p);

    }

    //销毁链表

    void DestoryLink(node *head)

    {

    free(head);

    }

    //按序号查找

    int FindByNo(node *head, int loc)

    {

    node *p;

    int i=1;

    if (loc>Count_Node(head)||loc<=0)

    {

    printf("位置错误 ");

    return0;

    }

    if (head == NULL)

    {

    return0;

    }

    p = head->next;

    while(i!=loc)

    {

    i++;

    p=p->next;

    }

    return p->info;

    }

    //尾插法建立带头结点的单链表

    node* Creat_Node()

    {

    node *head,*pre,*p;

    int x;

    head=(node*)malloc(sizeof(node));;

    head->next=NULL;

    pre=head;

    printf("输入各结点的值,0结束:");

    while(EOF!=(scanf("%d",&x))&&x!=0)

    {

    p=(node*)malloc(sizeof(node));

    p->info=x;

    p->next=pre->next;

    pre->next=p;

    pre=pre->next;

    }

    return head;

    }

    //头插法建立带头结点的单链表

    node* Build_Node()

    {

    node *head,*p;

    int x;

        

    head=(node*)malloc(sizeof(node));;

    head->next=NULL;

    printf("输入各结点的值,0结束:");

    while(EOF!=(scanf("%d",&x))&&x!=0)

    {

    p=(node*)malloc(sizeof(node));

    p->info=x;

    p->next=head->next;

    head->next=p;

    }

    return head;

    }

    //Y前插入X

    void Before_y_Insert_x(node* head,int y,int x)

    {

    node *pre,*p,*s;

    if (head ==NULL)

    {

    return;

    }

    pre=head;

    p=pre->next;

    while(p&&p->info!=y)

    {

    pre=p;

    p=p->next;

    }

    if(p==NULL)

    {

    printf("error!%d不在该链表中 ",y);

    }

    else

    {

    s=(node*)malloc(sizeof(node));

    s->info=x;

    s->next=p;

    pre->next=s;

    }

    }

    //判断链表是否有序

    int Is_Sort(node *head)

    {

    node *p,*pre;

    int flag;

    if (head ==NULL)

    {

    return0;

    }

    pre=head->next;

    p=pre->next;

    flag=pre->info>p->info?1:0;

    while(p)

    {

    pre=p;

    p=p->next;

    if(p)

    {

    if(flag!=pre->info>p->info?1:0)

    {

    return 0;

    }

    }

    }

    return1;

    }

    //链表反序

    void convert_Node(node *head)

    {

    node *pre,*p=head->next;

    if (head ==NULL)

    {

    return;

    }

    head->next=NULL;

    while(p)

    {

    pre=p;

    p=p->next;

    pre->next=NULL;

    pre->next=head->next;

    head->next=pre;

    }

    }

    //打印单链表

    void Print_Node(node *head)

    {

    node *p;

    if (head ==NULL)

    {

    return;

    }

    p=head->next;

    printf("输出该链表:");

    while(p)

    {

    printf("%-5d--->",p->info);

    p=p->next;

    }

    if(p==NULL)

    {

    printf("^ ");

    }

    }

    void menu()

    {

    printf("   1:尾插法创建带头节点的链表 ");

    printf("   2:头插法创建带头节点的链表 ");

    printf("   3:链表长度 ");

    printf("   4:Y前插入X ");

    printf("   5:判断链表是否有序 ");

    printf("   6:链表反序 ");

    printf("   7:按值查找 ");

    printf("   8:插入元素 ");

    printf("   9:删除元素 ");

    printf("   10:按序号查找 ");

    printf("   11:清空链表 ");

    printf("   12:销毁链表 ");

    printf("   13:打印链表 ");

    printf("   14:退出 ");

    }

    int main()

    {

        int goon = 1;

        node *head=NULL;//*head1=NULL,*head2=NULL;

        int x=0, y=0;

        //int flag = 0;

        int choice;

        menu();

        while(goon)

        {

            printf("请选择: ");

            scanf("%d",&choice);

            switch (choice)

            {

    case 0:

    menu();

    break;

    case 1:

    printf("尾插法创建带头节点的链表 ");

    head=Creat_Node();

    break;

    case 2:

    printf("头插法创建带头节点的链表 ");

    head=Creat_Node();

    break;

    case 3:

    printf("链表长度 ");

    printf("链表长度为%d ",Count_Node(head));

    break;

    case 4:

    printf("Y前插入X 请输入XY: ");

    scanf("%d%d",&x,&y);

    Before_y_Insert_x(head,y,x);

    break;

    case 5:

    printf("判断链表是否有序 ");

                    

    if(Is_Sort(head)==1)

    {

    printf("该链表有序! ");

    }

    else

    {

    printf("该链表无序! ");

    }

    break;

    case 6:

    printf("链表反序 ");

    convert_Node(head);

    break;

    case 7:

    printf("按值查找 请输入要查找的值 ");

    scanf("%d",&x);

    FindByValue(head,x);

    break;

    case 8:

    printf("插入元素 请输入插入的位置(0到链表长度之间)和元素");

    scanf("%d%d",&x,&y);

    InsertItem(head,x,y);

    break;

    case 9:

    printf("删除元素 请输入删除元素的位置(1到链表长度之间)");

    scanf("%d",&x);

    printf("删除的元素为%d",deleteItem(head,x));

    break;

    case 10:

    printf("按序号查找 请输入要查找的序号 ");

    scanf("%d",&x);

    printf("找到的值为%d",FindByNo(head,x));

    break;

    case 11:

    printf("清空链表 ");

    FlushLink(head);

    break;

    case 12:

    printf("销毁链表 ");

    DestoryLink(head);

    break;

    case 13:

    Print_Node(head);

    break;

    case 14:

    printf("Exit!");

    goon = 0;

    break;

    default:

    break;

            }

        }

        

    }

  • 相关阅读:
    无线网络的切换bat
    远程桌面远程服务器拷贝失灵 解决方法
    不能将 Null 值赋给类型为 (不可为 null 的值类型)的成员。解决方法
    应该改变面向对象的说法
    Windows 2012 装 Remote Desktop Organizer 无法连接到其他远程服务器
    JavaScript 运行时错误: 无法获取未定义或 null 一种解决方案
    ssd硬盘u盘装win7扩展文件时0x80070570错误
    swfit-小知识Demo
    ios-简单算法
    swfit-扩展语法
  • 原文地址:https://www.cnblogs.com/chenhaosuibi/p/3488582.html
Copyright © 2020-2023  润新知