#Pls need help with linked list implementation

6 messages · Page 1 of 1 (latest)

fringe timber
#

There is an issue while running this code, when I insert a value at a specific position which does not exist, the program just terminates. It doesn't have any issues when I execute this first in the program. Can someone help me fix this?

karmic lagoonBOT
#

@fringe timber

Aviie Gyu Uploaded Some Code
Uploaded these files to a Gist
fringe timber
#
#include <stdlib.h>
#include <conio.h>
#include <windows.h>

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

typedef struct Node node;
node *head=NULL, *last=NULL;

void Options();
void InsertFirst();
void InsertAtPos();
void InsertLast();
void DeleteItem();
void DeleteList();
void Display();


int main(void) { 
    while(1) {
        system("@cls||clear");

        int option;
        Options();
        scanf("%d",&option);
      
        switch(option) {
            case 1:    
            {  
                    int addChoice, item, pos;



                    printf("\nPlease enter the item to be added: ");
                    scanf("%d", &item);

                    printf("\nWhere to add the item?");
                    printf("\n[1] Start of the list");
                    printf("\n[2] End of the list");
                    printf("\n[3] Specific position");
                    printf("\n[0] Cancel");
                    printf("\nEnter choice: ");
                    scanf("%d", &addChoice);

                    switch (addChoice) {
                        case 1: 
                            InsertFirst(item);
                            printf("Item added at first position\n");
                            Sleep(1000);
                            break;
                        case 2:
                            InsertLast(&head, item);
                            printf("Item added at last position\n");
                            Sleep(1000);
                            break;
#
                        
                        
                            printf("Enter the position at which the item should be inserted: ");
                              scanf("%d", &pos);
                              
                              int flag = 0;
                              
                              while (head != NULL){
                                  InsertAtPos(item, pos);
                                printf("Item added at position %d\n", pos);
                                flag = 1;
                                Sleep(1000);
                                break;
                            }
                            
                           if(flag==0) {
                                printf("Key not found, adding to the end of the list.\n");
                                InsertLast(&head, item);
                                Sleep(1000);
                                break;
                                }
                        
                }
            }
                break;
            case 2: 
            {
                int element, pos;
    
                printf("\nPlease enter an element to delete from the list: ");
                scanf("%d", &element);

                DeleteItem(element, pos);
                
                Sleep(2000);
            }
                break;
            case 3:
                Display();
                Sleep(2000);
                break;
            case 4: 
                DeleteList();
                printf("The list has been emptied!\n");
                Sleep(2000);
                break;
            default: 
                exit(0);
                break;
        }
    }
}
#
        printf("LIST MENU");
        printf("\n\t[1] Add Item");
        printf("\n\t[2] Delete Item");
        printf("\n\t[3] Display List");
        printf("\n\t[4] Delete List");
        printf("\n\t[0] Exit Program");
        printf("\n\tEnter Choice: ");
}

void InsertFirst(int value) {

    node *temp_node = (node *) malloc(sizeof(node));
    
    temp_node->data=value;
    temp_node->next = head;

    head = temp_node;
}

void InsertAtPos(int value, int pos){

    
    int i;
    
    struct Node* temp1 = (struct Node*)malloc(sizeof(struct Node));
    temp1->data = value;
    temp1->next = NULL;
    
    if(pos == 1){
        temp1->next = head;
        head = temp1;
        return;
    }
    
    struct Node* temp2 = head;
    for(i = 0; i < pos-2; i++){
        temp2 = temp2->next;
    }
    
    temp1->next = temp2->next;
    temp2->next = temp1;
    
    
}```
#
    node *newNode = malloc(sizeof(node));
    newNode->data = val;
    newNode->next     = NULL;

    if(*head == NULL)
         *head = newNode;
    else {
        node *lastNode = *head;
        
        while(lastNode->next != NULL) {
            lastNode = lastNode->next;
        }

        lastNode->next = newNode;
    
    }
}



void DeleteItem(int value, int pos) {
    node *myNode = head, *previous=NULL;
    int flag = 0;

    while(myNode!=NULL) {
        if(myNode->data==value) {
            if(previous==NULL)
                head = myNode->next;
                
            else
                previous->next = myNode->next;
                
            pos++;
            
            printf("The element %d, which is at position %d, has been deleted\n", value, pos+1);
            flag = 1;
            free(myNode); 
            break;
        }

        previous = myNode;
        myNode = myNode->next;
    }

    if(flag==0)
        printf("The list contains no such element\n");
}

void DeleteList() {
    struct node *temp;

    while(head != NULL) {
        temp = head;
        head = head->next;

        free(temp);
    }
}

void Display() {
    printf("\nYour full linked list is\n");

    node *myList;
    myList = head;

    while(myList!=NULL)
    {
        printf("%d ", myList->data);

        myList = myList->next;
    }
    puts("");
}