Saturday, May 22, 2010

A question of C programming language.!!!!!!!! Please...?

I can't do this mind blowing program. Please solve it for me. Don't say that do it your self.......





Write a function in C to delete first node of a singly linked list. Each node in the list stores an alphanumeric string.

A question of C programming language.!!!!!!!! Please...?
I'm giving u a function that takes the location of the node (here u've said first node, so enter 1 as 'loc' okay) %26amp; delete that particular node from linklist.





del_loc(ND **p, int loc)


{


ND *temp, *old ;


int i;


temp = *p ;


for ( i=1 ; i%26lt;loc ; i++ )


{


old = temp ;


temp = temp-%26gt;link ;


if( temp == NULL )


{


printf("Location %d not found" , loc) ;


return ;


}


}





if( loc ==1 )


*p = temp-%26gt;link ;





else


{


old-%26gt;link = temp-%26gt;link ;


free(temp) ;


}


}








Now I don't know how u've implemented the entire program, for dynamic memory allocation u should declare a node in following way:





struct node


{


int data ;


struct node *link ;


} ;


typedef struct node ND ;





// creating a node


ND *q = (ND*)malloc(sizeof(ND)) ;


// assign a data to newly created node


q-%26gt;data = info ; // info taken as input


q-%26gt;link = NULL ; // indicates end of list





Now, if u want more operations like insert, delete, reverse, update, count, display -- all these I've done in previous semesters in Data-Structures %26amp; I've all these implementations in a single program, I can give u later if u need plz tell me. Bye .
Reply:You can actually do this in one statement if you're using pointer variables. A pointer variable stores a memory address as opposed to a value. In this case, all you have to do is take the pointer to the first node and reassign it to the second node like this.





pointer = node.next;





where next is a pointer from the first value to the second value. Assume that each node is comprised of two parts namely.





struct Node


{


char* Value;


Node* Next;


};
Reply:If your link is starting with the variable first_node, you can use following segment of code.





first_node = first_node-%26gt;next;


free(first_node); //or delete first_node;

imperial

No comments:

Post a Comment