This is a program to add, display & delete an item/node from the double linked list ... Here the add & display works correctly... where my delete is having some
problem, when ever I delete an item, it also deletes the tails..
kindly can someone help out...
Here is my data types used in my program
Code:
typedef struct
{
char data[MAX_PRID_LEN];
int OidLen;
}Data_Type;
//structure for global data
typedef struct TAG_DATA
{
Data_Type Data;
int Status;
struct TAG_DATA *next;
struct TAG_DATA *prev;
}__stGData;
typedef struct TAG_LIST
{
__stGData *head;
__stGData *tail;
int count;
}__stGList;
__stGList g_PridList;
// my main() function
{
GData=(__stGData *)malloc(sizeof(__stGData));
len=sizeof(int);
printf("\nEnter the Prid value: ");
scanf("%d",&prid);
memcpy(GData->Data.data,&prid,len);
GData->Data.OidLen = len;
res=AddtoGList(&g_PridList, GData);
if(res!=0)
{
printf("Adding created PRID to list failed");
free(GData);
}
DisplayPridGList(g_PridList);
printf("\nEnter the Prid value u wish to remove: ");
scanf("%d",&prid);
memcpy(GData->Data.data,&prid,(len));
res =RemovefromGList(&g_PridList, GData);
}
//Function to add element
{ __stGData *temp=list->head;
if (list->head == NULL)
{
/* no list items right now */
list->head = list->tail=GData;
GData->next=NULL;
GData->prev=NULL;
}
else
{
while(temp->next!=NULL)
{
temp=temp->next;
}
/* there are one or more item */
temp->next = GData;
GData->prev=temp;
GData->next=NULL;
list->tail=GData;
}
/* increment number of items */
list->count ++;
}
//Function to display the list elements
{
__stGData *temp=list.head;
int i=1,j,val;
while(temp)
{
printf("%d>",i);
memcpy(&val,temp->Data.data,temp->Data.OidLen);
printf("%d",val);
i++;
temp=temp->next;
printf("\n");
}
}
// Function to remove
int RemovefromGList(__stGList *list,__stGData *GData)
{
int printval;
__stGData *temp=list->head;
if(list->head==NULL)
{
printf("Global Table is Empty");
return -1;
}
if(temp->next==NULL)
{
if(strncmp(temp->Data.data,GData->Data.data,temp->Data.OidLen)==0)
{
temp->prev=NULL;
temp->next=NULL;
free(temp);
list->head=NULL;
}
}
else
{
while(temp)
{
if(strncmp(temp->Data.data,GData->Data.data,temp->Data.OidLen)==0)
{
if(temp->prev == NULL)
{
list->head=temp->next;
list->head->prev=NULL;
}
else
{
temp->prev->next=temp->next;
}
if(temp->next == NULL)
{
list->tail=temp->prev;
}
else
{
temp->next->prev=temp->prev;
}
break;
}
temp=temp->next;
}
}
list->count--;
return 0;
}