1

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

The output is also given below: sample output

  • Possible duplicate of [C programming Linked Lists delete node at position N](http://stackoverflow.com/questions/5011990/c-programming-linked-lists-delete-node-at-position-n) – progyammer Oct 20 '16 at 11:24
  • Trust me it's not duplicate progy_rock, the function locate() is not returning the structure pointer properly or i cannot accept the returned structure pointer in proper way.. besides it deals with double pointer.. I am not getting any help from any source.. – Saugat Sigdel Oct 20 '16 at 11:27
  • why do you recurse in locate? – Kami Kaze Oct 20 '16 at 11:28

2 Answers2

2

In the code to locate the node you want to remove, you have these lines:

else if((*s)->node!=NULL){
    locate(&((*s)->node));
}
return NULL;

Here you call locate recursively, but you don't actually return the result of that call, instead you will always return NULL.

You need to change it to

else if((*s)->node!=NULL){
    return locate(&((*s)->node));  // Return result of recursive call
}
return NULL;
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • gosh you are fast... The recursion seemed fishy to me, but I wasn't sure it was wrong until now and then I see its already done... – Kami Kaze Oct 20 '16 at 11:27
  • Really Joachim sir, you impressed me with such a fast reply.. you are just great and I respect your deep knowledge.. Please suggest me some tips for being perfect like you in c programming.. Thanks a lot sir.. – Saugat Sigdel Oct 20 '16 at 11:31
  • @user3213732 Experience. Programming almost every day in one way or another for around 30 years tends to give lots of experience. You'll get it too some day. :) – Some programmer dude Oct 20 '16 at 11:38
0

A double pointer linked list would typically mean that you have an array of linked lists. There were a few problems in your code around the recursion in locate(...). Here is your code, redacted so that you only have a single pointer to the linked list with a "head" element. Cheers!

#include <iostream>

#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*);

struct linkedList *createlist();





struct linkedList* createlist()
{

     struct linkedList* head = (struct linkedList*) malloc(sizeof(struct  linkedList));
head->node = NULL;
return head;



}

int main(int argc, char *argv[]) {
/*some variables*/
char *str;

int indexer = 0;
/*add data from arguments to linked list combine arguments with no spaces
 * as a single string
 */
struct linkedList* head = createlist();
str = combineWithNoSpaces(argc, argv);
addTolinkedList(str, head, &indexer);
/*diaplay the added data to linked list with no spaces */
printf("your combined argument is \n");
displayWithNoSpaces(head);
printf("\n");
/* Delete specified character */
printf("Now Deleting the node with specified character : \n");
deleteChar(head);
/* Display the data after deleting */
printf("Displaying after deleting..\n");
displayWithNoSpaces(head);
printf("\n");
return 0;
}

struct linkedList *locate(struct linkedList *head){

if (head->node == NULL) return NULL;
if(head->node->ch == 'a'){
    return head;
}

return locate(head->node);

}
void deleteChar(struct linkedList *head){
struct linkedList *temp, *tag;
tag = locate(head);
if(tag!= NULL){
    temp = tag->node->node;
    free(tag->node);
    tag->node = temp;
}
}

void displayWithNoSpaces(struct linkedList *head) {
if (head->node != NULL) {
    printf("%c", head->node->ch);
    displayWithNoSpaces(head->node);
}
return;
}
void addTolinkedList(char *str, struct linkedList *head, int *indexer) {
if (*indexer == strlen(str)) {
    head->node = NULL;
    return;
} else {
    head->node = (struct linkedList *) malloc(sizeof(struct linkedList));
    head->node->ch = *(str + *indexer);

    ++*indexer;
    addTolinkedList(str,head->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;
 }
bc888
  • 141
  • 14