#include <stdio.h>
#include<conio.h>
#include<alloc.h>
struct data
{
    int value;
    struct data *next;
};
int main()
{
    int s;
    struct data *m,*i;
    printf("enter the data: ");
    scanf("%d",&s);
    i=(struct data *)malloc(size of(struct data);
    i->value=s;
    i->next=m;
    m=i;
    printf("enter the 2nd data: ");
    scanf("%d",&s);
    i=(struct data *)malloc(size of (struct data);
    i->value=s;
    i->next=m;
    m=i;
    printf("\n list of values: ");
    while(m->value!=null)
    {
        printf("%d",m->value);
        m=m->next;
    }
    return 0;
}

hii deceptikon this is the new topic i learned .here the values are stored like array but the main confusion is
here

i->value=s;
 i->next=m;

confusing values are stored i want to learn more about this topic .could u suggest a good video tutorial link or good links or explain its bit difficult to understand

Recommended Answers

All 14 Replies

Here's a better example, see if it helps:

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

struct node {
    int          data;
    struct node *next;
};

int main(void)
{
    struct node *head = NULL, *temp;
    int data;

    printf("Enter a number (0 to stop): ");
    fflush(stdout);

    while (scanf("%d", &data) == 1 && data != 0) {
        temp = malloc(sizeof *temp);

        if (!temp) {
            perror("Error allocating memory, terminating...");
            break;
        }

        /* Prepend the new node to the list */
        temp->data = data;
        temp->next = head;

        /* Make the new node the head of the list */
        head = temp;
    }

    while (head) {
        printf("%d", head->data);

        if (head->next)
            fputs(" -> ", stdout);

        /* Safely free the current node */
        temp = head->next;
        free(head);
        head = temp;
    }

    putchar('\n');

    return 0;
}

do u have diagramatic explanation on any links ????

I don't have any at the moment. Since you seem to like videos, try searching YouTube for linked list tutorials. I'm sure you'll find something decent enough with plenty of hand waving.

HII deceptikon i saw some videos.i understood something please say if iam correct

for example iam inserting two values say 30,40
1.i->value=30;
2.i->next=null; \ so the link becomes null right???
3.m=i;\\ here i dont understand wats happening here
the value of m is null .wat is the value of i here?????

and again for inserting 40
1.i=(struct data*)malloc(size of(struct of(struct data))
2.i->value=40;
3.i->next=null;
4.m=i;\ please explain wat happens here???

iam not getting a clear view

i mean i dont nderstand wats happening here

 /* Make the new node the head of the list */
head = temp;

I'm sensing a confusion with pointers and aliasing. Have you played with Binky yet?

so linked list always prints number in reverse order???for example iam inserting 30,40,50 in linked list 50 will be header right??? ad start printing 50,40,30????

so linked list always prints number in reverse order???

This list does because new nodes are prepended rather than appended. With some extra work you can walk to the end of the list and append new nodes there, then the list will be in insertion order rather than reverse insertion order.

hii deceptikon i have a small doubt in linked list and pointers

struct node {
int data;
struct node *next;
};
int main(void)
{
struct node *head = NULL, *temp;

my doubt how does struct node *next serves as link to next node??? and why struct node *head does serve as pointer ???bcoz both are defined by structure

both are user defined data types tats wat my doubt is

my doubt how does struct node *next serves as link to next node??? and why struct node *head does serve as pointer ???bcoz both are defined by structure

This question suggests that you don't understand pointers. If you have an instance of struct node, and its next member points to another instance of struct node, wouldn't you describe that as two instances linked together?

both are user defined data types tats wat my doubt is

Would it make any difference if they were integers? Because pointers work the same way regardless.

struct node {
int data;

};
void main()
{
struct node *next;
}

will it do for the next link if i do this deceptikon????

will it do for the next link if i do this deceptikon????

No, because then there's only one next link, ever. The whole point of storing the next link in each node of a list is to have multiple links that can create a chain of nodes, each node linking to the next in the list. If you only have one global next link, that would result in a pretty short list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.