469,300 Members | 2,212 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,300 developers. It's quick & easy.

how to find a variable in a given list and replace/delete them according to our wish

3
hello there
i want to know a program which will find find a given variable in a list and replace/delete them according to our wish. Well it would be preferable if its in C.
thanks
Sep 11 '06 #1
5 2192
Have you gone through the program for the deletion of a node in a linked list? If you done this program then you can solve the problem.
Sep 11 '06 #2
nayeef
3
Have you gone through the program for the deletion of a node in a linked list? If you done this program then you can solve the problem.
can you atleast tell me the link for that program..its pretty exhaustive in here..it would be highly appreciated..also i want to do this in a structure instead of a linked list..guess that will be possible
thanks
nayeef
Sep 11 '06 #3
Then you are talking about the array of structures.

See in an array you can not delete an element. The thing you can do is that you can replace a particular element or you simply left shift all the elements.
Sep 11 '06 #4
hi Nayeef below is the code which I wrote long back for finding a number in a list and replacing desired one
this is C code only using linked list.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define EXITSUCC 1;
#include <conio.h>

void push(struct node** , int i );
void insert(struct node** , int , int );

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


void main()
{
int i;
struct node* head = malloc(sizeof(struct node));
struct node* ptr = malloc(sizeof(struct node));
struct node* temp = malloc(sizeof(struct node));
clrscr();

push(&head,0);
ptr = head;
for(i=1;i<=8;i++){
push(&(head->next),i);
head = head->next;
}

//storing for future use here....
temp = ptr;

while(ptr != NULL ){
printf("\n%d",ptr->data);
ptr = ptr->next;
}

printf("\n.....Want to insert data here.........\n");
insert(&temp,56,5);
printf("\n.........After Insert operation........\n");
while(temp != NULL ){
printf("\n%d",temp ->data);
temp = temp ->next;
}


getch();
}

void push(struct node** str,int data){

struct node* temp = malloc(sizeof(struct node));

temp -> data = data;
temp -> next = NULL;
*str = temp;

}

void insert(struct node** str, int n,int old){

struct node* temp = malloc(sizeof(struct node));
struct node* new1 = malloc(sizeof(struct node));

temp = *str;
while( temp!= NULL ){
if(temp->data == old){
new1 -> next = temp -> next;
new1 -> data = n;
temp -> next = new1;
return;
}
temp = temp->next;
}

}
Sep 11 '06 #5
nayeef
3
Thanks a lot sehgal and pukur.Your tips were indeed handy
Regards
Nayeef
Sep 13 '06 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Philip WATTS | last post: by
25 posts views Thread by Neo Geshel | last post: by
3 posts views Thread by Wijaya Edward | last post: by
reply views Thread by 2Barter.net | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.