I am having an issue with deleting a node with specified character in a linked list. The program accept command line arguments, combines them in a single string and add each character to a linked list as node.
When i try to delete a character 'a' with command line argument "mango" it works fine.. i.e. it successfully deletes second node. when i try to do the same thing with "orange", the program doesn't delete it... means the program cannot work with third and farther nodes..
The program must not use any global variables so i've used double pointer. All the functions are working properly this problem may have occured due to some mistakes in locate() and deleteChar() function but i cannot figure out what the mistake is.Can this problem be solved using double pointer?? I cannot figure out what is wrong in this program.. I am new to c programming, please help me with this.. Please rectify me.. Thanks in advance..
The code is given below:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct linkedList {
char ch;
struct linkedList *node;
};
char* combineWithNoSpaces(int, char *[]);
void addTolinkedList(char *, struct linkedList **, int *);
void displayWithNoSpaces(struct linkedList **);
struct linkedList *locate(struct linkedList**);
void deleteChar(struct linkedList**);
int main(int argc, char *argv[]) {
/*some variables*/
char *str;
struct linkedList *s;
int indexer = 0;
/*add data from arguments to linked list combine arguments with no spaces
* as a single string
*/
s = (struct linkedList *) malloc(sizeof(struct linkedList));
str = combineWithNoSpaces(argc, argv);
addTolinkedList(str, &s, &indexer);
/*diaplay the added data to linked list with no spaces */
printf("your combined argument is \n");
displayWithNoSpaces(&s);
printf("\n");
/* Delete specified character */
printf("Now Deleting the node with specified character : \n");
deleteChar(&s);
/* Display the data after deleting */
printf("Displaying after deleting..\n");
displayWithNoSpaces(&s);
printf("\n");
return 0;
}
int i = 0;
struct linkedList *locate(struct linkedList **s){
if((*s)->node->ch == 'a'){
return *s;
}
else if((*s)->node!=NULL){
locate(&((*s)->node));
}
return NULL;
}
void deleteChar(struct linkedList **s){
struct linkedList *temp, *tag;
tag = locate(s);
if(tag!= NULL){
temp = tag->node->node;
free(tag->node);
tag->node = temp;
}
}
void displayWithNoSpaces(struct linkedList **s) {
if ((*s) != NULL) {
printf("%c", (*s)->ch);
displayWithNoSpaces(&(*s)->node);
}
return;
}
void addTolinkedList(char *str, struct linkedList **s, int *indexer) {
if (*indexer == strlen(str)) {
*s = NULL;
return;
} else {
(*s)->ch = *(str + *indexer);
(*s)->node = (struct linkedList *) malloc(sizeof(struct linkedList));
++*indexer;
addTolinkedList(str, &(*s)->node, indexer);
}
}
char * combineWithNoSpaces(int argc, char *argv[]) {
int i, j;
int count = 0;
int memory = 0;
char *str;
for (i = 1; i < argc; i++) {
for (j = 0; j < strlen(argv[i]); j++) {
++memory;
}
}
str = (char *) malloc(memory * sizeof(char));
for (i = 1; i < argc; i++) {
for (j = 0; j < strlen(argv[i]); j++) {
*(str + count) = argv[i][j];
++count;
}
}
return str;
}