0
#include<stdio.h>
#include<stdlib.h>
    struct Node {
        int data;
        struct Node *next;

    };
void printList(struct Node *n)
{
    while (n!=NULL) {

    printf(" %d|\t", n->data);
    n = n->next;
    }
}


int main () {
    struct Node* head = NULL;
    struct Node* second = NULL;
    struct Node* third = NULL;
    struct Node* fourth = NULL;
    struct Node* fifth = NULL;

    head = (struct Node*)malloc(sizeof(struct Node));
    second = (struct Node*)malloc(sizeof(struct Node));
    third = (struct Node*)malloc(sizeof(struct Node));
    fourth = (struct Node*)malloc(sizeof(struct Node));
    fifth = (struct Node*)malloc(sizeof(struct Node));

    head-> data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = fourth; 

    fourth->data = 4;
    fourth->next = fifth;

    fifth->data = 5;
    fifth->next = NULL;

    printList(head);
    delete();
    printList(head);
}
void delete(){
    struct Node* temp;
    int loc;
        printf("Enter the location what you want to delete : ");
        scanf("%d",&loc);
        if(loc>length()){
            printf("Invailid Location");
        }
            else if (loc==1) {

            temp = root;
            root= temp -> next;
            temp -> next = NULL;
            free (temp);
        }
}

There is a error in root variable and delete function. It says no deceleration of root variable. And previous implicit declaration of 'delete' was here Is it need to declare root variable first? If yes, where is want to be? Please let me know what is the error in this program.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Read your code again, where is the `root` variable defined? – Some programmer dude Jan 20 '19 at 14:15
  • Furthermore, how would the `delete` function have access to the list you create in the `main` function? – Some programmer dude Jan 20 '19 at 14:15
  • your `delete` function should have a parameter that passes your link list. – Huzo Jan 20 '19 at 14:19
  • Another thing: A node is not a list and a list is not a node. A list typically contains a pointer to the first element (head) and a pointer to the last element (tail) and maybe also the number of elements in the list. – Swordfish Jan 20 '19 at 14:19
  • [there](https://stackoverflow.com/questions/53380109/linked-list-c-programming-error-by-inserting-new-element/53380533#53380533) is a starting point for the implementation of a singly linked list I wrote for a differen question. – Swordfish Jan 20 '19 at 14:27

0 Answers0