Another link list problem

I'm getting a program error at the end part of the program. I did some tracing and I didn't see anything wrong in the code but there could be since I still have limited knowledge in link lists. Mind if you help me trace the error?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
#include <stdlib.h>

typedef struct Cell
{
    char Elem;
    struct Cell *Next;
}Celltype, *LIST;

int main(void)
{
    LIST L;
    Celltype StructArray[5];
    int i=0;

    L = &StructArray[i];
    for(;i<5;i++)
    {
        if(i>5)
        {
            StructArray[i].Next = NULL;
        }

        printf("Enter a character: ");
        scanf("%c", &StructArray[i].Elem);
        fflush(stdin);

        StructArray[i].Next = &StructArray[i+1];
    }

    while(L->Next != NULL)
    {
        printf("%c", L->Elem);
        L = L->Next;
    }

    return (0);
}


Thanks in advance and have a happy day! :)
Last edited on
First

loop is for i < 5 ,so it will run 0 ... 4

1
2
3
4
   if(i>5)// this never going to happen
   {
            StructArray[i].Next = NULL;
   }



the above never going to happen and in wrong place..

first do input scanf...

then
i should be like
1
2
3
4
5
6
7
8
9
10

if( i == (size -1)) 
{
       StructArray[i].Next = NULL;
}
else
{
   StructArray[i].Next = &StructArray[i+1];
}
Last edited on
Topic archived. No new replies allowed.