Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
#include<stdio.h>
#include<stdlib.h>

typedef struct node {
    int info;
    int key;
    struct node *next;
}node;

void insfirst(node *ptr,int data,int key)
{
    while(ptr->next!=NULL)
    ptr=ptr->next;
    ptr->next=(node *)malloc(sizeof(node));
    ptr=ptr->next;
    ptr->info=data;
    ptr->key=key;
    ptr->next=NULL;
}

void insert(node *ptr,int data,int key)
{
    node *prev;
    ptr=ptr->next;
    while((ptr->key)<key)
    {
        prev=ptr;
        ptr=ptr->next;
    }
    node *nw;
    nw=(node*)malloc(sizeof(node));
    nw->info=data;
    nw->key=key;
    nw->next=ptr;
    prev->next=nw;
}

int delete(node **start,int *key)
{
    node *ptr;
    ptr=*start;
    ptr=ptr->next;
    int data=ptr->info;
    *key=ptr->key;
    start=ptr->next;
    return(data);
}

void display(node *ptr)
{
    ptr=ptr->next;
    while(ptr!=NULL)
    {
        printf("INFO=%d\t\tKEY=%d\n",ptr->info,ptr->key);
        ptr=ptr->next;
    }
}

int main()
{
    node *start;
    start=(node*)malloc(sizeof(node));
    start->next=NULL;
    int data[10]={5,10,15,20,25,30,35,40,45,50};
    int key[10]={12,31,36,41,49,53,62,76,81,99};
    int op=-1,i,item,ke;
    for(i=0;i<10;i++)
    insfirst(start,data[i],key[i]);
    printf("Initial list:\n\n");
    display(start);
    while(op!=4)
    {
        printf("\nVarious operations available are:\n\n1:Insert\n2:Delete\n3:Display\n4:Exit\n");
        scanf("%d",&op);
        if(op==1)
        {
            printf("Enter the element to be inserted and its respective key\n");
            scanf("%d%d",&item,&ke);
            insert(start,item,ke);
        }
        else if(op==2)
        {
            item=delete(&start,&ke);
            printf("DELETED NODE: INFO=%d\tKEY=%d\n",item,ke);
        }
        else if(op==3)
        display(start);
        else if(op==4)
        exit(0);
    }
    getch();
}
Posted

When the while loop is not taken, the prev pointer is not initialized (the program should raise an access violation).
 
Share this answer
 
v2
Comments
Brady Bar 21-May-13 8:30am    
Well when the "key" passed to the function is greater than the keys of the all the elements present while loop would run and the prev would be initialised to the last node in the list.The code should work in that case.
Brady Bar 21-May-13 8:31am    
I haven't found a solution yet....
YvesDaoust 21-May-13 9:24am    
This code does not even compile !
Brady Bar 21-May-13 21:16pm    
The code compiles on Dev C++ and some operations work fine.The flaw is just in the case when element is to be inserted at the last on the basis of its key.
Brady Bar 22-May-13 8:06am    
I haven't found a solution yet....so please keep suggestin!!!
VB
while((ptr->key)<key)
will not stop when it passes the end of the list.
Should be
C++
while(ptr && (ptr->key)<key)
followed by correct handling of that case.
 
Share this answer
 
v2
Comments
Brady Bar 22-May-13 8:38am    
thanks!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900