简体   繁体   中英

Display function of Circular Linked List Debugging

I have tried to implement Circular Linked List with features like insertion, deletion at position, then repeated insertion or deletion based on changing position. However there is a case when initially the list is not yet created and I want to insert at position 1. My program should allow this and should give invalid position for other positions for the mentioned case. However whenever I am trying to do this it is giving me segmentation fault during display in the while loop. Please help. My code is:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

struct node
{
    int data;
    struct node *next;
};

typedef struct node node;

node *head,*tail;
int count=0;

void addnode(int d)
{
    node *newnode = (node *)malloc(sizeof(node));
    newnode->data = d;
    newnode->next = head;
    if(head==NULL)
    {
        head = newnode;
        tail = newnode;
    }
    else
    {
        tail->next = newnode;
        tail = newnode;
    }
    count++;
}

void insert(int d,int pos)
{
    if((pos==(count+1))||(head==NULL))
    {
        addnode(d);
    }
    else
    {
        if(pos==1)
        {
            node *newnode = (node *)malloc(sizeof(node));
            newnode->data = d;
            newnode->next = head;
            head = newnode;
            tail->next = newnode;
        }
        else
        {
            int a = 1;
            node *temp = head;
            while(a!=(pos-1))
            {
                temp = temp->next;
                a++;
            }
            node *newnode = (node *)malloc(sizeof(node));
            newnode->data = d;
            newnode->next = temp->next;
            temp->next = newnode;
        }
        count++;
    }
}

void delete(int pos)
{
    if(count==0)
    {
        printf("Underflow \n");
        return;
    }
    if(pos==1)
    {
        if(head->next==head)
        {
            head = tail = NULL;
        }
        else
        {
            head = head->next;
            tail->next = head;
        }
    }
    else
    {
        int a = 1;
        node *temp = head;
        while(a!=(pos-1))
        {
            temp = temp->next;
            a++;
        }
        if(pos==count)
        {
            tail = temp;
        }
        temp->next = temp->next->next;
    }
    count--;
}

void display()
{
    node *temp = head;
    if(head==NULL)
    {
        printf("Empty List \n");
        return;
    }
    while(temp->next != head)       
    {
        printf("%d ",temp->data);
        temp = temp->next;
    }
    printf("%d %d\n",temp->data,temp->next->data);
}

void main()
{
    int ch;
    do
    {
        printf("\f\n");
        printf("1. Create the list \n");
        printf("2. Insert an element at any position \n");
        printf("3. Delete an element at any position \n");
        printf("4. Display the list \n");
        printf("5. Quit \n");
        printf("Enter your choice : \n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:
            {
                int a;
                char c;
                printf("Enter the data : \n");
                scanf("%d",&a);
                addnode(a);
                while(1)
                {
                    printf("Do you want to continue[Y/N] : ");
                    scanf(" %c",&c);
                    if(c=='Y' || c=='y')
                    {
                        printf("Enter the data : \n");
                        scanf("%d",&a);
                        addnode(a);
                    }
                    else if(c=='N' || c=='n')
                    {
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
                break;
            }
            case 2:
            {
                int a,pos;
                char c;
                printf("Enter the data : \n");
                scanf("%d",&a);
                printf("Enter the position : \n");
                scanf("%d",&pos);
                if((pos>(count+1))||(pos<1))
                {
                    printf("Invalid Position \n");
                    break;
                }
                insert(a,pos);
                while(1)
                {
                    printf("Do you want to continue[Y/N] : ");
                    scanf(" %c",&c);
                    if(c=='N' || c=='n')
                    {
                        break;
                    }
                    if(c!='Y' && c!='y')
                    {
                        continue;
                    }
                    printf("Enter the data : \n");
                    scanf("%d",&a);
                    printf("Enter the position : \n");
                    scanf("%d",&pos);
                    if((pos>(count+1))||(pos<1))
                    {
                        printf("Invalid Position \n");
                        break;
                    }
                    insert(a,pos);
                }
                break;
            }
            case 3:
            {
                int pos;
                char c;
                printf("Enter the position : \n");
                scanf("%d",&pos);
                if((pos>count)||(pos<1))
                {
                    printf("Invalid Position \n");
                    break;
                }
                delete(pos);
                while(1)
                {
                    printf("Do you want to continue[Y/N] : ");
                    scanf(" %c",&c);
                    if(c=='N' || c=='n')
                    {
                        break;
                    }
                    if(c!='Y' && c!='y')
                    {
                        continue;
                    }
                    printf("Enter the position : \n");
                    scanf("%d",&pos);
                    if((pos>count)||(pos<1))
                    {
                        printf("Invalid Position \n");
                        break;
                    }
                    delete(pos);
                }
                break;
            }
            case 4:
            {
                display();
                break;
            }
            case 5:
            {
                return;
            }
            default:
            {
                printf("Invalid choice \n");
                break;
            }
        }
    }while(ch!=5);

    getch();
}

When the list is empty, ie head is NULL, your addnode doesn't create a circular list.

Your code will set newnode->next to NULL (because head is NULL) but what you want is newnode->next = newnode to get a circular list.

So inside addnode you need:

    ...
    if(head==NULL)
    {
        head = newnode;
        tail = newnode;
        newnode->next = newnode;
    }
    ...

Alternatively you can move the line newnode->next = head; to be after the if-else clause.

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