zoukankan      html  css  js  c++  java
  • 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;

            }

        }

        

    }

  • 相关阅读:
    如何使DIV居中
    #ifdef DEBUG的理解
    iOS程序启动画面的制作
    ios4支持高分辨率的一些总结(转)
    将旧的推送通知从通知中心移除掉的三种方法
    Mac锁屏设置快捷键
    湖北黑客因盗窃域名被刑事拘留 涉嫌诈骗犯罪
    php连接Access数据库错误及解决方法
    Cannot send session cache limiter – headers already sent
    php代码注意事项
  • 原文地址:https://www.cnblogs.com/chenhaosuibi/p/3488582.html
Copyright © 2011-2022 走看看