chapter linked list

Post on 03-Jan-2016

32 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

Chapter LINKED LIST. What is Linked List?. Linked List is a common Data structure used to store similar data in memory. Elements are not stored in continuous memory location. Elements are bound to each other by explicit links. Basic Types of Linked List. Contd…. - PowerPoint PPT Presentation

TRANSCRIPT

Chapter

LINKED LIST

What is Linked List?• Linked List is a common Data structure

used to store similar data in memory.

• Elements are not stored in continuous memory location.

• Elements are bound to each other by explicit links

Basic Types of Linked List

Contd…

Difference between Arrays & Linked list

1. Arrays use sequential allocation 2. No pointer used in Array.

3. Accessing kth element is cheaper in arrays.

4. Insertion and Deletion of elements is a difficult operation in Arrays.5. Nodes are statically attached.

6. Merging & Breaking Lists is difficult in case of Arrays.

7. Arrays are goes mostly for static data structures.

1. Linked list uses linked allocation. 2. Linked list uses some extra memory i.e. link pointer. 3. Accessing kth element is costly in

Linked list.4. Insertion and Deletion of elements is a

easy operation in Linked lists 5. Since nodes in Linked list are

dynamically allocated, it has no limitations on growth.

6. Merging & Breaking Lists is easier in case of Linked lists.

7. Linked list is a better data structure in most cases.

Question ?

Write a program to create Singly linked list which will store data of employees. Every Employee has following information

• Employee Number

• Salary

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct emp

{

int empno;

float sal;

struct emp *next;

};

struct emp *first, *newrec, *old;

void insert() { newrec = (struct emp*) malloc (sizeof(struct emp)); printf("enter emp no and salary"); scanf("%d%f",&newrec->empno, &newrec->sal );

newrec->next = NULL; if(first==NULL)

first= newrec; else old->next = newrec; old=newrec; }

void display()

{

newrec = first;

while(newrec!=NULL)

{

printf("%d\t%f \n",newrec->empno,newrec->sal);

newrec=newrec->next;

}

}

void main(){ clrscr(); first = NULL; insert(); insert(); insert();

display(); getch();}

Initial Step: three pointers and no

memory allocated

Insertion Steps

• First Step : Memory allocated using one

Structure created for emp 1

Second Step :

Third Step:

• Forth Step :

Fifth Step :

And So on…..

Doubly Linked List

Each structure in the list will contain some data and two pointers ‘prew’ & ‘next’ which will store the address of previous and next structures. ‘Prew’ pointer of first structure and ‘next’ pointer of last structure are null.

Two additional pointers are also used namely first & last which will point to first and last structure.

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct emp

{

float sal;

int empno;

struct emp *prev, *next;

};

struct emp *first, *newrec, *last;

void insert() { float d; newrec=(struct emp*) malloc (sizeof(struct emp)); printf("enter the salary and employee no:"); scanf("%d%f",&newrec->empno,&d); newrec->sal=d; newrec->next=NULL; if(first==NULL) { newrec->prev =NULL; first=newrec; } else { newrec->prev = last; last->next=newrec; } last = newrec; }

void display() {

newrec = last; while(newrec!=NULL) { printf("%d\t%f\n",newrec->empno,newrec->sal); newrec=newrec->prev; }

}

void main() { first= NULL; insert(); insert(); insert(); display(); getch(); }

Insertion sort

Algorithm:-

Step-1: Compare the current element in the UNSORTED list with each element in the SORTED list. If you find an element in the SORTED list to have just higher value than our current element then go to step-2 or else go to step-3

Step-2: Make the element with that just higher value as your TARGET position. Insert the current element at a position just before the TARGET. Consider the next element, if any and go to step-1 or else quit

Step-3: If you find the current element under consideration in the sorted list as the LAST element then just insert your current element from the unsorted list at the end of the Sorted list or else just move to the next element in the sorted list. Consider the next element, if any and go to step-1 or else quit 

Insert node at any Place

Void add ()

{

struct node *a, *b;

int p ;

newrec = ( struct emp*) malloc ( sizeof (struct emp));

printf (“Enter the data to be added”);

scanf(“%d”, & newrec ->data);

printf (“Enter position :”);

scanf (“%d”, &p);

if (p == 1) { newrec -> next = first; first = newrec; }Else{ a = b = first; for (i=1; i<= p-1; i++) { b = a; a = a-> next; } b -> next = newrec; newrec -> next = a;} / * end else*/

} / * end add */

Delete node from any Place

Void delete(){ int p ; struct node *a , * b; printf (“Enter position :”); scanf (“%d”, &p); a = b = first;

for (i=1; i<= p-1; i++) { b = a; a = a-> next; } b -> next = a -> next; if (p == 1) { first = a -> next; } free (a);} / * end delete */

top related