簡體   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