Linked List implementation in C

Discussion in 'C' started by DeepikaNS, Apr 5, 2010.

  1. DeepikaNS

    DeepikaNS New Member

    Joined:
    Apr 5, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hello Guys,
    I have found many source codes to implement linked list. But all are inserting or deleting the node values after user's choice. I dont want that. Insertion should happen dynamically.
    I have a client program running on another system. I fetch the data from that system through my server program.As and when i receive the data, I shd insert them into my linked list.
    Kindly help me dng this.

    Thanks in advance.
    Deepika
     
  2. micsom

    micsom New Member

    Joined:
    Oct 13, 2008
    Messages:
    39
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    I*N*D*I*A*
    u need to design the system accordingly...

    like keeping a provision for function pointer in your server program, as and when you receive data in server program, call this function ,which can have access to the LL,along with the data ..
     
  3. DeepikaNS

    DeepikaNS New Member

    Joined:
    Apr 5, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    //////////////////////////////
    CISM_CASM_RECV_REVERSAL_ORDER_ID_01234560000000000100000000010002501230000000000100000000019633789956387452
    CISM_CASM_RECV_REVERSAL_ORDER_ID_01234560000000000100000000010002501230000000000100000000019633789956387452
    CISM_CASM_RECV_REVERSAL_ORDER_ID_01234560000000000100000000010002501230000000000100000000019633789956387452
    CISM_CASM_RECV_REVERSAL_ORDER_ID_01234560000000000100000000010002501230000000000100000000019633789956387452
    //////////////////////////
    this is the value in my buffer which has to be inserted to the linked list.the whole contents come in a single buffer. but i should insert each line in a different node in the linked list. How can I do it?
    This is my code for insertion.
    Code:
     
    
    [LIST=1]
    [*]void insertion(char buff[1024])
    [*]        {
    [*]            new=(N*)malloc(sizeof(N));
    [*]            new->info=buff;
    [*]             printf("contents of buffer %s\n",buff);
    [*]            new->link=NULL;
    [*]              printf("value is %s\n",new->info);
    [*]            if(front==NULL)
    [*]            {
    [*]                front = new;
    [*]                rear = new;
    [*]            }
    [*]            else
    [*]            rear->link=new;
    [*]            rear=new;
    [*]        }     
    [/LIST]
     
  4. tech_aks

    tech_aks New Member

    Joined:
    Apr 5, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    kolkata
    here is d implementation of linked list in c





    Basic linked list example .. interactive

    #include <stdio.h>
    #include <stdlib.h>

    struct NODE {
    int number;
    struct NODE *next;
    };

    int search_value(struct NODE *llist, int num);
    void append_node(struct NODE *llist, int num);
    void display_list(struct NODE *llist);
    void delete_node(struct NODE *llist, int num);

    int main(void) {
    int num = 0;
    int input = 1;
    int retval = 0;
    struct NODE *llist;

    llist = (struct NODE *)malloc(sizeof(struct NODE));
    llist->number = 0;
    llist->next = NULL;

    while(input != 0) {
    printf("\n-- Menu Selection --\n");
    printf("0) Quit\n");
    printf("1) Insert\n");
    printf("2) Delete\n");
    printf("3) Search\n");
    printf("4) Display\n");
    scanf("%d", &input);

    switch(input) {
    case 0:
    default:
    printf("Goodbye ...\n");
    input = 0;
    break;
    case 1:
    printf("Your choice: `Insertion'\n");
    printf("Enter the value which should be inserted: ");
    scanf("%d", &num);
    append_node(llist, num);
    break;
    case 2:
    printf("Your choice: `Deletion'\n");
    printf("Enter the value which should be deleted: ");
    scanf("%d", &num);
    delete_node(llist, num);
    break;
    case 3:
    printf("Your choice: `Search'\n");
    printf("Enter the value you want to find: ");
    scanf("%d", &num);
    if((retval = search_value(llist, num)) == -1)
    printf("Value `%d' not found\n", num);
    else
    printf("Value `%d' located at position `%d'\n", num, retval);
    break;
    case 4:
    printf("You choice: `Display'\n");
    display_list(llist);
    break;
    } /* switch */
    } /* while */

    free(llist);
    return(0);
    }

    void display_list(struct NODE *llist) {
    while(llist->next != NULL) {
    printf("%d ", llist->number);
    llist = llist->next;
    }

    printf("%d", llist->number);
    }

    void append_node(struct NODE *llist, int num) {
    while(llist->next != NULL)
    llist = llist->next;

    llist->next = (struct NODE *)malloc(sizeof(struct NODE));
    llist->next->number = num;
    llist->next->next = NULL;
    }

    void delete_node(struct NODE *llist, int num) {
    struct NODE *temp;
    temp = (struct NODE *)malloc(sizeof(struct NODE));

    if(llist->number == num) {
    /* remove the node */
    temp = llist->next;
    free(llist);
    llist = temp;
    } else {
    while(llist->next->number != num)
    llist = llist->next;

    temp = llist->next->next;
    free(llist->next);
    llist->next = temp;
    }
    }

    int search_value(struct NODE *llist, int num) {
    int retval = -1;
    int i = 1;

    while(llist->next != NULL) {
    if(llist->next->number == num)
    return i;
    else
    i++;

    llist = llist->next;
    }

    return retval;
    }
     
  5. DeepikaNS

    DeepikaNS New Member

    Joined:
    Apr 5, 2010
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    Thanks for the code. But i don't want it to be interactive. It should happen dynamically on button click. On each button click, I get 1 line of data which has to be inserted into new node. How do i do it.
     
  6. tech_aks

    tech_aks New Member

    Joined:
    Apr 5, 2010
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    kolkata
    will u plz let me knw the exact query of urs ...from scratch....thnks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice