Linked List – part 1

           DATA STRUCTURES –LINKED LIST

1

Hai friends,today the topic that we are going to see is about LINKED LIST IN C.While studying data structure the basic is Linked List.LINKED LIST will give the idea to proceed others like trees,graphs.This Linked list is like A,B,C,D,……  for proceeding others .If u thorough this LINKED LIST then other implementation will be very simple for you.

While studying college it will look like a terror program.But here I have tried to simplified it,but not more simple programs I have used somewhat complicated programs and explained it in a simple way.

First you have to ask some simple questions to yourself,If you know the answer for this questions only we can able to continue .

What is a variable?                         2

What is an array?

What is a structure?

What is an pointer?

 

If you don’t know the answer means no problem.Here is the answer for you

Variable-it is something which hold some value.

               int a=10;

Array-store sequence of values of same type with common name of only one single type.

int  a[10]={1,2,3,4,5,6,7,8,9,0};

Structure-like a package kind of it. Eg : pencil box-you can keep anything within that.But you have to decide  what you want to keep.

struct student{int id;

char   *name;

float   percentage;

}

 

Pointer-it is just a another variable which holds an address .That address can be array or structure or variable.The condition is, the type that we are going to store should be the type of pointer.

                        int   *ptr=&a;

               

If you continue reading this tutorial.It will be useful for you,

Why Linked list?

In the world Whatever you  take you can see two  behaviours.

  • Static
  • Dynamic

 

Static:

Some of the characteristics  will not change.eg.humans have some static behaviours i.e.some  of their characters cannot be change .you can understand a person if you  have a study  on him.

Dynamic:

It will change based on place,things,etc.We cannot understand this because it will change dynamically .i.e.we have to sit and study.It will take some time.

When we will go for static?

                      Static                                                               Dynamic

  • Understanding is simple.                  Understanding is difficult.
  • If you know the data size,                 If you don’t  know the data size,

,then it is static.                                   then it is dynamic. Understanding is complex.

  • eg: sum of semester marks              eg. average marks of a   department.
  •                              We can solve problem in two methods ,either statically or dynamically.you have to choose which one is better to  store the data.

We have to choose static or dynamic based on,

Now we will see,how to store data is C,

  • Variable
  • Array
  • Structure
  • pointers

Let us take first problem for static,

Problem 1:  sum of your semester marks.

-variable->declare 8 variable-> int sem1,sem2,sem3,sem4,…..sem8;

-array->declare 1 array->int mark[8];

-structure->define a structure with 8 elements->struct marks

{

int mark1,int mark2,int mark3,…..int mark8

};

-pointers-> int  *marks=(int *) malloc (8*sizeof(int));

->   using variable and structure is not good:

Variable: list of variables will stand separately.

Structure:  list of variables are packed.

So,using variable and structure is not fair for a good program to store data.

 

Let us take second problem for dynamic,

Problem 2: Average marks of a department.

variable->declare 60 variables-> int sem1,sem2,sem3,sem4,…..sem60;

array->declare 1 array->int marks[60];

structure->define a structure with 60 elements->struct marks

{

int marks1,int marks2,int marks3,…..int marks60

};

pointers-> int  *marks=(int *) malloc (60*sizeof(int));

    ->Now we are having only two options.we have to go with either array or pointers.

 

 

 

  -> we can choose array for problem1 i.e. for static.

Reason:

->Memory wastage is less i.e.for static you will know the size,if you are allocating   8 memory space and you are using only 4 memory space,then the wastage is less. if I use pointer here unnecessarily complexity will arise.

->I am choosing array for static due to simplicity.

     ->we can choose pointers for problem2 i.e for dynamic.

Reason:

-> Memory wastage is high i.e.if you are allocating   60 memory space initially and you are using only 10 memory space,then the wastage is high.In pointer,Dynamic memory  allocation is possible ,if you don’t know the size exactly.If you want you can minimize or maximize the memory allocation dynamically,so we can use pointers.

->I am choosing pointers for dynamic due to efficiency.

 

 

 

Solution for  problem 1 and problem 2:

Problem 1:  sum of your semester marks.

            array->declare 1 array->int mark[8];    3

-pointers-> int  *marks=(int *) malloc (8*sizeof(int));

 

Problem 2:  Average  marks of a department.

 

-array->declare 1 array->int mark[60];                                     4

pointers-> int  *marks=(int *) malloc (60*sizeof(int));

 

Ok.with this we can go for linked list.Continue in Linked List -part 2………..

 

————-PRIYANKA LOG(guvi blogs)

For more innovative vidoes follow us on ” http://www.guvi.in “:)  

 

6

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s