By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,656 Members | 738 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,656 IT Pros & Developers. It's quick & easy.

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

P: 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
Share this Question
Share on Google+
5 Replies


P: 61
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

P: 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

P: 61
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

P: 10
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

P: 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.