English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

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.

2006-10-28 19:28:12 · 4 answers · asked by Anonymous in Computers & Internet Programming & Design

4 answers

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) & delete that particular node from linklist.

del_loc(ND **p, int loc)
{
ND *temp, *old ;
int i;
temp = *p ;
for ( i=1 ; i {
old = temp ;
temp = temp->link ;
if( temp == NULL )
{
printf("Location %d not found" , loc) ;
return ;
}
}

if( loc ==1 )
*p = temp->link ;

else
{
old->link = temp->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->data = info ; // info taken as input
q->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 & I've all these implementations in a single program, I can give u later if u need plz tell me. Bye .

2006-10-28 21:10:11 · answer #1 · answered by Innocence Redefined 5 · 0 0

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;
};

2006-10-28 19:47:30 · answer #2 · answered by iuneedscoachknight 4 · 0 0

just google c++ and there are a lot of tutorial websites. As far as learning the language is concerned, It takes a long time and you can always learn new things. Also dont expect it to be easy, but if you know other languages its definitely a huge plus

2016-03-28 00:31:03 · answer #3 · answered by Anonymous · 0 0

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

first_node = first_node->next;
free(first_node); //or delete first_node;

2006-10-28 21:41:01 · answer #4 · answered by manoj Ransing 3 · 0 0

fedest.com, questions and answers