Thread: having problem with link list code can any1 help?

  1. #1
    Registered User
    Join Date
    Apr 2012
    Location
    Bangalore
    Posts
    5

    having problem with link list code can any1 help?

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #define NULL 0
    struct link_list
    {
    int value;
    struct link_list *next;
    };
    typedef struct link_list node;
    
    
    void main()
    {
    clrscr();
    int n;
    void create(node *t,int n);
    node *head;
    printf("enter the size of link list\n:");
    scanf("%d",&n);
    head=(node*)calloc(n,sizeof(node));
    create(head,n);
    getch();
    }
    void create(node *t,int n)
    {
    int i,a[n];
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    (t+i) ->value=a[i];
    (t+i)->next=((t+i)+1);
    if(i=(n-1))
    {
    (t+(n-1)) ->value=a[n-1];
    (t+(n-1))->next=NULL;}
    else
    {continue;}
    }
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to ask a more specific question. What do you think it's supposed to do? What is it actually doing?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    if(i=(n-1))

    this assigns the value 'n-1' to variable i. it does not check that i equals n-1

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Great, another vanilla newb poster that
    • uses Turbo C, void main, conio.h, and everything else that went out of date ~20 years ago
    • has no indentation whatsoever
    • doesn't actually ask a question, merely dumps code and expects us to know the problem



    ._.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > (t+i) ->value=a[i];
    You seem to be trying to access your list as if it were an array.
    This you cannot do.

    You have two pointers
    - one points to the head of your list - this you keep constant.
    - one points to the 'current' node of the list - this you update as you add/delete/insert/scan the list.

    Linked list - Wikipedia, the free encyclopedia
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List problem
    By Darkinyuasha1 in forum C Programming
    Replies: 2
    Last Post: 05-08-2009, 07:47 PM
  2. a link list code question..
    By transgalactic2 in forum C Programming
    Replies: 29
    Last Post: 10-27-2008, 03:24 PM
  3. Problem in link list
    By Bargi in forum C Programming
    Replies: 2
    Last Post: 07-16-2008, 01:54 AM
  4. Help me ! Link list code in OOP C++
    By phoenix2007 in forum C++ Programming
    Replies: 14
    Last Post: 01-16-2007, 04:48 PM
  5. Problem with Link List
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 04-24-2002, 11:49 PM

Tags for this Thread