简体   繁体   中英

Issues in using circular linked list for a clock

So I got this work that I need to give about making a digital clock. The user sets the number of nods and each nod is equal with 1/n of second with N being the number of nods the user puts.After that I am called to make 3 pointers:

  • one for seconds
  • one for minutes
  • one for hours

The Seconds pointer must be running all the nods in the circular link. For a full rotation the pointer for the minutes should start moving to the next node and after a total number of 60 moves of the minute pointer, the hour pointer should follow right after. The clock needs to do the following things:

  1. The user can set the clock back to zero: 00:00:00;

  2. The user can set the clock with precision of seconds.

  3. The program needs to show the clock after each activity.

  4. The user can program the clock for an alarm in which the clock shows a message to the user.

  5. It needs to find the next hour that its pointers ( on the clock not the program) get align ( for example : 12:00,01:05,02:10,03:15 etc)

  6. it finishes the program after freeing up any memory left that our list used.

here is my code but I am having some difficulties with it.

#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;
    }
 }

As Jochaim Pileborg pointed out you really need to use the debugger for this.

The only reason for a segfault I see is that you call: printf("%d",sec->data); without initializing sec .
You also do not initialize 'v' and 'c', and there are two 'n' variables in your solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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