zoukankan      html  css  js  c++  java
  • (续)顺序表之单循环链表(C语言实现)

    循环链表和单链表的唯一差别在于单循环链表的最后一个节点的指针域指向第一个节点,

    使得整个链表形成一个.





    C实现代码例如以下:

    #include<stdio.h>
    
    typedef struct node
    {
        int data;
        struct node *next;
    }Node;
    
    //链表的初始化
    Node* InitList(int number)
    {
        int i;
        Node *pHead=(Node *)malloc(sizeof(Node));
        Node *TempHead=pHead;
        Node *Head=pHead;
        int data;
        for(i=0;i<number;i++)
        {
            pHead=(Node *)malloc(sizeof(Node));
            printf("Please input the %dst node data:
    ",i+1);
            scanf("%d",&data);
            pHead->data=data;
            pHead->next=Head->next;
            TempHead->next=pHead;
            TempHead=TempHead->next;
        }
        return Head;
    }
    
    //显示链表
    void ShowList(Node *Head)
    {
        Node *TempNode=Head;
        TempNode=TempNode->next;
        printf("Show List:
    ");
        while(TempNode->next!=Head->next)
        {
            printf("%d ",TempNode->data);
            TempNode=TempNode->next;
        }
        printf("%d",TempNode->data);
        printf("
    ");
    }
    
    //输出链表某个值的位置
    int ListLocation(Node *Head,int data,int number)
    {
        Node *TempNode=Head;
        int location=1;
        TempNode=TempNode->next;
        while(TempNode->next!=Head->next)
        {
            if(TempNode->data==data)
                {
                    return location;
                }
                location++;
                TempNode=TempNode->next;
        }
        if(location>=number)
            printf("Not found!");
    }
    
    //输出链表某个位置的值
    int ListData(Node *Head,int location,int number)
    {
        if(location>number)
            printf("Not found!");
    
        Node *TempNode=Head;
        TempNode=TempNode->next;
        int i;
        for(i=1;i<=number;i++)
        {
            if(location==i)
                return TempNode->data;
            TempNode=TempNode->next;
        }
    }
    
    //头入法插入元素
    void HeadInsertData(Node *Head,int data)
    {
        Node *TempNode=Head;
        Node *InsertNode=(Node *)malloc(sizeof(Node));
            InsertNode->data=data;
    
    
        while(TempNode->next->next!=Head->next)
            TempNode=TempNode->next;
    
        TempNode=TempNode->next;
    
        InsertNode->next=Head->next;
        TempNode->next=InsertNode;
        Head->next=InsertNode;
    }
    
    //尾入插入除元素
    void TailInsertData(Node *Head,int data)
    {
        Node *TempNode=Head;
        Node *InsertNode=(Node *)malloc(sizeof(Node));
        InsertNode->data=data;
    
        while(TempNode->next->next!=Head->next)
            TempNode=TempNode->next;
    
        TempNode=TempNode->next;
    
        TempNode->next=InsertNode;
        InsertNode->next=Head->next;
    }
    
    
    
    //删除头结点
    void HeadDeleteData(Node *Head)
    {
        Node *TempNode=Head;
        while(TempNode->next->next!=Head->next)
            TempNode=TempNode->next;
        TempNode->next->next=Head->next->next;
        Head->next=Head->next->next;
    }
    
    
    //删除尾结点
    void TailDeleteData(Node *Head)
    {
        Node *TempNode=Head->next;
        while(TempNode->next!=Head->next)
        {
            TempNode=TempNode->next;
            if(TempNode->next->next==Head->next)
                break;
        }
    
        TempNode->next=Head->next;
    }
    
    int main()
    {
        Node *Head;
        int number;
        printf("Please input the node number:
    ");
        scanf("%d",&number);
        Head=InitList(number);
        printf("The initital list is:
    ");
        ShowList(Head);
    
        int flag;
        printf("
    
    ");
        printf("**********************Your Choice********************
    ");
        printf("****************1-输出链表某个值的位置***************
    ");
        printf("****************2-输出链表某个位置的值***************
    ");
        printf("****************3-头入法插入元素*********************
    ");
        printf("****************4-尾入法插入元素*********************
    ");
        printf("****************5-删除头结点*************************
    ");
        printf("****************6-删除尾结点*************************
    ");
        printf("****************0-退出*******************************
    ");
        printf("
    
    ");
        printf("Please input flag:
    ");
        scanf("%d",&flag);
    
        switch(flag)
        {
            case 1:
            {
                int data;
                printf("Please input the data you want locate:
    ");
                scanf("%d",&data);
                int location;
                location=ListLocation(Head,data,number);
                printf("The data's location is: %d",location);
                break;
            }
            case 2:
            {
                int location;
                printf("Please input the location you want  data:
    ");
                scanf("%d",&location);
                int data;
                data=ListData(Head,location,number);
                printf("The location's data is: %d
    ",data);
                break;
            }
            case 3:
            {
                int data;
                printf("Please input the data you want insert in head:
    ");
                scanf("%d",&data);
                HeadInsertData(Head,data);
                ShowList(Head);
                break;
            }
            case 4:
            {
                int data;
                printf("Please input the data you want insert in tail:
    ");
                scanf("%d",&data);
                TailInsertData(Head,data);
                ShowList(Head);
                break;
            }
            case 5:
            {
                HeadDeleteData(Head);
                ShowList(Head);
                break;
            }
            case 6:
            {
                TailDeleteData(Head);
                ShowList(Head);
                break;
            }
            case 7:
            {
               printf("You choose to exit.
    ");
               break;
            }
        }
        return 0;
    }
    
    结果图:



    转载请注明作者:小刘

  • 相关阅读:
    Java正则表达式进阶教程之构造方法
    command模式
    黑马程序员面向对象继承
    Spring+Quartz实现定时任务的配置方法
    NIO学习笔记3(UDP)
    java继承总结
    轻松快速掌握JAVA设计模式
    Struts2>OGNL 小强斋
    Struts2>标签 小强斋
    Struts2>fielderror显示处理 小强斋
  • 原文地址:https://www.cnblogs.com/slgkaifa/p/6853698.html
Copyright © 2011-2022 走看看