C-Circular List - push-delete-traversal

Below , there is a function for inserting data into a node of a circular list. Can u please help me create a delete function for the circular list by providing some code? Also, can you provide some code for traversing the circular list? Thanks in advance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  typedef struct nodeStruct
{
    int val;
    struct nodeStruct *next;
} node;

node *l;
static void push(node **head, int v)
{
    node *temp = malloc(sizeof(node));
    temp->val = v;
    temp->next = *head;
    *head = temp;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
static void push(node *head, int v)
{
    node *temp = (node*)malloc(sizeof(node));
    temp->val = v;
    temp->next = head;
    head = temp;
}

/* Deleting node 'head' is difficult because we have no direct access to its predecessor.
 * But it's easy deleting the successor, so I go this way.
 */
static void del( node *head)
{
    if (head  == nullptr) return;  // list is empty
    if (head  == head->next)
    {
        free(head);   // the list have only one element
        head == nullptr;
        return;
    }
    node *tmp = head->next; // will get deleted
    head->next  = head->next->next;
    free(tmp);
}

static node* next( node *head)
{
    // using the ternary conditional
    return head == nullptr ? nullptr : head->next;
}
Topic archived. No new replies allowed.