繁体   English   中英

在时钟中使用循环链表的问题

[英]Issues in using circular linked list for a clock

所以我得到了制作数字时钟所需的这项工作。 用户设置点头的数量,每个点头等于1 / n秒,N是用户点头的数量,之后我被要求做出3个指针:

  • 一秒钟
  • 一分钟
  • 一个小时

Seconds指针必须在循环链接中运行所有的点头。 对于完整旋转,分钟指针应开始移动到下一个节点,并且分钟指针总共移动60次后,小时指针应紧随其后。 时钟需要执行以下操作:

  1. 用户可以将时钟设置回零:00:00:00;

  2. 用户可以以秒为单位设置时钟。

  3. 该程序需要在每次活动后显示时钟。

  4. 用户可以为闹钟编程时钟,其中时钟向用户显示一条消息。

  5. 它需要找到其指针(不是程序的时钟)对齐的下一小时(例如:12:00,01:05,02:10,03:15等)

  6. 它会在释放列表使用的剩余内存后完成程序。

这是我的代码,但是我遇到了一些困难。

#include<stdio.h>
#include<stdlib.h>
int counter=0;
typedef struct Node 
{
    int data;
    struct Node *next;
}node;

void insert(node *pointer, int data)
{
    node *start = pointer;
    /* Iterate through the list till we encounter the last node.*/
    while(pointer->next!=start)
    {
        pointer = pointer -> next;
    }
    /* Allocate memory for the new node and put data in it.*/

    pointer->next = (node *)malloc(sizeof(node));
    pointer = pointer->next;
    pointer->data = data;
    pointer->next = start;
} 

void print(node *start,node *pointer)
{
    if(pointer==start)
    {
            return;
    }
    printf("%d ",pointer->data);
    print(start,pointer->next);
}

int main()
{
    /* start always points to the first node of the linked list.
       temp is used to point to the last node of the linked list.*/
    node *start,*temp;
    start = (node *)malloc(sizeof(node)); 
    temp = start;
    temp -> next = start;
    /* Here in this code, we take the first node as a dummy node.
       The first node does not contain data, but it used because to avoid handling special cases
       in insert and delete functions.
     */
    node *sec,*min,*hour;
    int v,c,n;
    printf("1. Insert N\n");
    printf("2. Make Time Zero\n");
    printf("3. Set Clock\n");    
    int query;
    scanf("%d",&query);
    if(query==1)
    {
        int data,i,n;
        printf("Posa n thes\n");
        scanf("%d",&n);
        for (i = 0; i < 60*n; i++)
        {    
            data = i;
            insert(start,data);
            printf("%d\n",i);
        }

        node *sec_copy;
        sec_copy=start;
        min=start;
        hour=start;

        while(n>0)
        {    
            sec_copy=sec_copy->next;
            n--;
            c++;
            if(c == 59*n)
            {
                min=min->next;
                c=0;
                v++;
            }
            if(v == 60)
            {
                hour=hour->next;
                v=0;
            }
        } 
    }

    printf("%d",sec->data);
    if(query==2)
    {    
        int timer;
        timer=0;
        printf("%.2d:%.2d:%.2d",timer,timer,timer);

    }
    if(query==3)
    {
        int h,m,s;
        printf("Set me hours");
        scanf("%d",&h);
        h = h%24;
        printf("Set me min");
        scanf("%d",&m);
        h = h+m/60;
        m = m%60;
        printf("Set me secs");
        scanf("%d",&s);
        h = h + s/3600;
        m = m + s%3600;
        s = s%60;
    }
 }

正如Jochaim Pileborg指出的那样,您确实需要使用调试器。

我看到的段错误的唯一原因是您调用: printf("%d",sec->data); 无需初始化sec
您也不会初始化'v'和'c',并且解决方案中有两个'n'变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM