anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list...

76
Chapter 2 Data Structures Using C LISTS ADT 2.1. INTRO Lists are very common structure in the real as well as computing world. A list is simply a sequential arrangement of items or element. It is an ADT object contains dynamic collection of elements of size N, which are arranged in a linear sequence. For Example, consider a List of elements E 1, E 2, E 3, …………….E n. Here E 1 is a first element, E 2 is second, E 3 is third and E n is n th or last element. Where, E i i th element in the List E i-1 Predecessor of E i E i+1 Successor of E i TYPES OF LISTS ADT Empty list - A list has no elements or its length is Zero. Ordered list - Elements are arranged based on some sort of ordering criteria. Unordered list - No clear fashion of arrangements. Stack - If the elements are deleted & inserted in the same end of the list then the List is referred as a Stack. Queue - If the elements are deleted at one end and inserted at the other end of the list then the List is referred as Queue. BASIC OPERATION OF LISTS ADT 1. Creation of List 2. Insert an element onto the List 3. Deletion an element from the List 4. Traversal (or) Display the List 5. Modify an element in the List 6. Search an element in the List 7. Count the elements in the List 8. Order the elements in List 21

Transcript of anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list...

Page 1: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Chapter

2

Data Structures Using C

LISTS ADT

2.1. INTRODUCTION

Lists are very common structure in the real as well as computing world. A list is simply a sequential arrangement of items or element. It is an ADT object contains dynamic collection of elements of size N, which are arranged in a linear sequence. For Example, consider a List of elements E1,E2,E3,…………….En.

Here E1 is a first element, E2 is second, E3 is third and En is nth or last element.Where, Ei ith element in the List Ei-1 Predecessor of Ei

Ei+1 Successor of Ei

TYPES OF LISTS ADT Empty list - A list has no elements or its length is Zero. Ordered list - Elements are arranged based on some sort of ordering criteria. Unordered list - No clear fashion of arrangements. Stack - If the elements are deleted & inserted in the same end of the list then the List is

referred as a Stack. Queue - If the elements are deleted at one end and inserted at the other end of the list

then the List is referred as Queue.

BASIC OPERATION OF LISTS ADT 1. Creation of List2. Insert an element onto the List3. Deletion an element from the List4. Traversal (or) Display the List5. Modify an element in the List6. Search an element in the List7. Count the elements in the List8. Order the elements in List

IMPLEMENTATION OF LISTS ADT A List can be implemented by three ways, they are

1. Using Array Data Structure2. Using Linked List Data Structure3. Using Cursor implementation

All are having some advantages and also some disadvantages. Based on the requirement of application, a List is implemented either by Array or Linked List or Cursor based.

21

Page 2: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Student[5]

3

6

01234

N=1

Data Structures Using C

2.2. LIST ADT USING ARRAY DATA STRUCTURE It is a simplest way of representing the Lists ADT. This data structure is more suitable

when the number of elements going to be stored in a List is known in advance (i.e.) List ADT is fixed in size.

OPERATION FUNCTIONS

1. CREATION OF LIST

Algorithm

Step-1: Memory will be allocated for a List with the size of “LIST_SIZE” using Array DS. For example, if the name of List is “Student” and LIST_SIZE is “5” then the Array declaration will be Student[5] .Step-2: Declare the integer variable N which denotes the actual number of elements in a list and Initialize ‘N’ by (-1) for indicating the emptiness of List.Step-3: Check the List is Full or Not by N = = (LIST_SIZE-1). If it is not FULL then read the input elements (one by one) from the user and store that elements on (N+1) th location of the array (i.e. first element is stored on 0th index and second will be on 1st index so on).

Example Consider the input elements are {3, 6}. Before creating a list N = -1 and after inserting the elements into the list, the LIST_SIZE=5 and N=1.The List became,

IMPLEMENTATION IN C #define LIST_SIZE 7 // Macro Assignment Statementint Student[LIST_SIZE], N; // Global variable declaration

void create_list( ) { int choice=1,N= -1; while (choice) // Always True, if & only if the value of choice is 1 { printf (“Enter the element to insert\n”); scanf(“%d”,&Student[++N]); if(N<=(LIST_SIZE-1)) {

22

Page 3: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

4

3

6

3

6InsertFirst(4)

N=2

21

Data Structures Using C

printf(“If you wants to enter another element? Then press’1’ \n”); scanf(“%d”,&choice); } else { printf (“List is full\n”); } }}

2. INSERT AN ELEMENT ON TO THE LIST An element can be inserted in the list at three different places.

a) Insert at firstb) Insert at lastc) Insert at Intermediate(by giving the position)

A) INSERT FIRST

Algorithm

Step-1: Check the List is full or not. If List is Full then terminate the operation otherwise increment N by 1 and proceed the next step.Step 2: Right shift (or) down the existing elements from (N-1)th position to 0th position roll by one, for freeing the first position of List.Step-3: Read the data to be inserted &assign it to the 0th position of List.

IMPLEMENTATION IN C void insert_first ( ){ int ele,i; if(N = = (LIST_SIZE-1)) printf(“List is full\n”); else { N=N+1; printf(“enter the element to insert first\n”);

23

Page 4: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

4

3

6

InsertLast(5)4

3

6

5 N=3

Data Structures Using C

scanf(“%d”,&ele); for(i=N-1;i>=0;i--) Student[i+1]=Student[i]; Student[0]=ele; }}

B) INSERT LAST

Algorithm

Step-1: Check the list is Full or not. If so then terminate the operation otherwise increment N by 1 and proceed the next step.Step-2: Read the data to be inserted & assign it to Nth position of List.

IMPLEMENTATION IN C void insert_last( ){ int ele; if(N==(LIST_SIZE-1)) printf(“List is full\n”); else { N=N+1; printf(“enter the element to insert\n”); scanf(“%d”,&ele); Student[N]=ele; }}

C) INSERT INTERMEDIATE

Algorithm

Step-1: Check the list is full or not. If so then terminate the process, otherwise increment N

24

Page 5: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

5

4

2

3

6

6

4

3

5

321

InsertInter(1,2)

N=4

Data Structures Using C

by 1 and proceed the next step.Step-2: Read the position and data to be inserted from the user. List Position starts from Zero to (List_Size – 1).Step-3: Right shift the data from (N-1)th position to the user given position roll by one.Step-4: Assign the data to be inserted at the user given position

IMPLEMENTATION IN C void insert_inter( ){ int ele,pos,i; if(N= =(LIST_SIZE-1)) printf(“List is full\n”); else { N=N+1; printf(“enter the element and position to insert\n”); scanf(“%d%d”,&ele,&pos); for(i=N-1;i>=pos;i++) Student[i+1]= Student [i]; Student [pos]=ele; }}

3. DELETE AN ELEMENT FROM THE LIST An element can be deleted from the list at three different places.

a) Delete firstb) Delete lastc) Delete Intermediate(by giving the position)

A) DELETE FIRST

Algorithm

Step-1: Check the List is Empty or not by (N= = -1). If so terminate the process otherwise follow the next step.Step-2: Left shift or Up the element from 1st position to Nth position roll by one.

25

Page 6: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

3

5

4

2

6 5

2

3

6

1234

DeleteFirst( )

N=3

6

2

3

5

2

3

6

DeleteLast( )

N=2

Data Structures Using C

Step-3: Decrement N by 1.

IMPLEMENTATION IN C void delete_first( ) { int i; if(N= = -1) printf(“List empty\n”); else { for(i=1;i<=N;i++) Student[i-1]=Student[i]; N=N-1; }}

Note:

In deletion operation, actually the deleted element resides in memory only (i.e. the value in array of index will not be deleted just by decrementing the N value by one). To avoid confusion, it will not be shown in diagrams. Traversal of list is performed by displaying the elements in array from 0th to Nth index alone, so we didn’t mind the N+1th index values.

B) DELETE LAST

AlgorithmStep-1: Check the List is Empty or not. If so then terminate the process otherwise decrement N by 1.

26

Page 7: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

6

2

32

6DeleteInter(1)

N=1

Data Structures Using C

IMPLEMENTATION IN C void delete_last( ){ if(N= = -1) printf(“List is empty\n”); else N=N-1;

}

C) DELETE INTERMEDIATE

Algorithm

Step-1: Check the List is Empty or not. If so then terminate the process, otherwise follow the next step.Step-2: Read the position of an element to be deleted in the List.Step-3: Left shift the elements from (user given position +1) to Nth Position.Step-4: Decrement N by 1.

IMPLEMENTATION IN C void delete_inter( ){ int pos,i; if(N= = -1) printf(“List is empty\n”); else { printf(“enter the position of element to be deleted\n”); scanf(“%d”,&pos); for(i=pos+1;i<=N;i++) Student[i-1]=Student[i]; N=N-1; }}

27

Page 8: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

2

6

7

6Modify(2,7)

N=1

Data Structures Using C

4. TRAVERSAL OF A LIST

AlgorithmStep-1: Check the list is empty or not. If so then terminate the process otherwise follow the next stepStep-2: Display the element present in the list from 0 to N.

IMPLEMENTATION IN C void display( ){ int i; if(N = = -1) printf(“List if Empty\n”); else { printf(“Elements are…\n); for(i=0;i<=N;i++)

printf(“%d\t”,Student[i]); }}

5. MODIFY AN ELEMENT IN THE LIST

Algorithm

Step-1: Check the List is Empty or not. If so then terminate the process otherwise follow the next step.Step-2: Read the old element to be modified and new element to replace from the user.Step-3: By traversing the list, check the old element is found in the list or not.Step-4: If found then update with the new element, otherwise display the error message as “element not found”

IMPLEMENTATION IN C void modify( ){

28

Page 9: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

int old,new_ele,i; if(N= = -1) printf(“List is empty\n”); else { printf(“enter the old & new element to update\n”); scanf(“%d%d”,&old,&new_ele); for(i=0;i<=N;i++) { if(Student[i]= =old) { printf(“Element updated\n”); Student[i]=new_ele; break; } } if(i>N) printf(“Element not found\n”); }}

6. SEARCH AN ELEMENT IN THE LIST

Algorithm

Step-1: Check the List is Empty or not. If so then terminate the process otherwise follow the next step.Step-2: Read the search element from the userStep-3: By traversing the list, check the search element is matched with the element in the list or not. If the element found then display its position, otherwise display element not found.

IMPLEMENTATION IN C void search( ){ int search_ele,i; if(N= = -1) printf(“List is Empty\n”); else { printf(“Enter the element to search\n”); scanf(“%d”,&search_ele); for(i=0;i<=N;i++) {

29

Page 10: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

if(Student[i]==search_ele) { printf(“Element found in %d position”,i); break; } } if(i>N) printf(“Element not found\n”); }}

7. COUNT THE ELEMENTS IN THE LIST

Algorithm

Step-1: Check the List is Empty or not. If empty then display the count is 0 otherwise displays the value of (N+1)

IMPLEMENTATION IN C void count( ){ if(N= = -1) printf(“Count is 0”); else printf(“count is %d”,(N+1));}

8. ORDER THE ELEMENTS IN THE LIST Ordering the elements can be either ascending or descending order.

ALGORITHM (USING INSERTION SORT TECHNIQUE)

Insertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list. Insertion sort consist of N-1 passes, where N is the number of elements to be sorted. The ‘ith’ pass of insertion sort will insert ‘ith’ element in a[i] into its rightful place among a[1],a[2]…a[i-1]. After doing this insertion the records occupying a[1],a[2]…a[i] are in sorted order.

IMPLEMENTATION IN C void order( ){int i,j,key;

30

Page 11: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

for(i=1;i<=N;i++){ key=Student[i]; for(j=i;j>=1;j--) { if(key<Student[j-1]) Student[j]=Student[j-1]; else break; } Student[j]=key;}printf(“Ascending order of List becomes..\n”);for(i=0;i<=N;i++) printf(“%d\t”,Student[i]);printf(“Descending order of List becomes..\n”);for(i=N;i>=0;i--) printf(“%d\t”,Student[i]);}

ADVANTAGES OF ARRAY DATA STRUCTURE 1. Easy to implement.2. Take less time while searching an element in List.3. No more extra space is needed for processing the elements.

DISADVANTAGES OF ARRAY DATA STRUCTURE 1. It is a Static data structure. So Memory storage space is wasted (i.e.) reserved space for

the elements cannot be used for any purpose.2. The size of the array must be known before the compilation time. It can’t be changed

after its declaration.3. Insert and Delete an element becomes complex and consumes more time because it

requires shifting other elements in the Array.

31

Page 12: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Node with address F001 (in Hex Code Format)

15 F012

Data Link to the node “F012”

Data Fields Link Field

(One or More Data with Single Link)

Data Structures Using C

2.3. LIST ADT USING LINKED LIST DATA STRUCTURE Limitations of Array DS can be overcome by using Linked list DS. Linked list is a linear

collection of elements called Nodes where the linear order is maintained by means of Links.

The elements of a Linked list are not stored in adjacent location as in Array DS. Thus, a Linked list has been defined to consist of linear elements which may increase or decrease as and when required.

STRUCTURE OF A NODE Basically a ‘NODE’ contains two fields. They are,1. Data Field: Used to store the Element values. A node may contain one or more Data

fields.2. Link Field (or) Address Field: Store the Address of another Node (also called as

Internal Address) which holds the next or adjacent element of this Node. This field is mainly used for making the Linked List. A node must contain atleast one Link field.

Note:

Nodes in a linked list are not organized by their physical placement in memory (i.e.) they may or may not be placed in a continuous memory location. Thus, the Nodes are logically related with each other by storing the address of another node in its link field. So a node can be identified by the address stored on its predecessor or its successor Node.

BASIC TERMS Head pointer: It holds the address of the first node in the linked list. So the entire list can be accessed only with the help of this head pointer. This is also called as External Address.Tail Pointer: It holds the address of the Last node in Linked list. Usage of this pointer is optional.NULL pointer: To indicate the end of list, NULL value is stored in the address field of a node. This is called Null Address.

TYPES OF LINKED LIST Based on the structure of a node, A Linked list is classified as

1. Singly Linked List

32

Page 13: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

5 0002 10 0004 15 NULL000F

Head 000F 0002 0004

NULL Pointer (End of List)Data Link

000F

Head 000F 0002 0004

5 0002NULL

BLink Data FLink

6 0004000F 8 NULL0002

Data Structures Using C

2. Doubly Linked List3. Circular Linked List

Singly Linked List (SLL) It is a Basic form of linked list. In this, a Node contains one or more data fields and only

one Link field for holding the address of next node i.e. successor node. NULL address is stored in the Last node link field, to indicate the end of List. Traversing of singly linked list is done only from Head to Tail node. The reverse (Tail to

Head) is not possible.

Doubly Linked List (DLL) It is an advanced form of SLL. For some applications, especially those where it is

necessary to traverse a List in both direction (from head to tail & from tail to head), it much works better than the singly linked list.

It contains one or more data fields and two Link fields named as predecessor and successor field used to hold the address of predecessor node and the successor node respectively.

In doubly linked list, two NULL addresses are stored in List, one is in Predecessor link field of the first node and another one is in Successor link field of the Last node.

Circular Linked list (CLL) It is an option to represent a List which is naturally circular. In some application, such as

for a pool of buffers that are used and released in FIFO order, for a set of processes that should be time-shared in round-robin order, needs a circular list.

With a circular list, a pointer to the last node gives easy access also to the first node, by following one link. Thus in applications that requires access to both ends of the list, this circular list is used by a single pointer, instead of two pointers.

There is no NULL address in any Node (i.e.) Nodes are connected like a circular fashion. This will be further classified as,

1. Circular Single Linked list2. Circular Doubly Linked list

33

Page 14: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

5 0002 10 0004 15 000F000F

Head 000F 0002 0004

000F

Head 000F 0002 0004

5 00020004 6 0004000F 8 000F0002

Data Structures Using C

1. Circular Singly Linked List (CSLL)It is same as singly linked list, but in circular fashion (i.e.) the link field of last node holds

the address of first node instead of NULL value. So the last node is again linked with a first node.

2. Circular Doubly Linked List (CDLL)It is same as doubly linked list, but the successor field of last node holds the address of

first node and the predecessor link field of first node holds the address of last node instead of NULL value.

2.3.1. OPERATION FUNCTIONS USING SLL

1. NODE CREATION

Algorithm

Step-1: Define a structure of a node using Structure DS. Step-2: Allocate a dynamic memory by using malloc( ) library function with a specified size of node.Step-3: Read the data & assign it to the data field of a node. Also assign NULL value to its link field

IMPLEMENTATION IN C struct node // Skeleton of Node

{int data;struct node *next;};

struct node *head,*tail,*prev,*temp,*newnode; //Structure Pointershead=NULL;

34

Page 15: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

tail=NULL;

void create_node( ) //Memory allocation and initialization of New Node{newnode=(struct node *)malloc(size of(struct node));if (newnode= =NULL) { printf(“Memory is not allocated for a node\n”); break; }else { printf(“Enter the data value\n”); scanf(“%d”,&newnode->data); newnode->next=NULL; }}

2. CREATION OF A LIST

Algorithm

Step-1: Make a function call to node creation function (i.e) create_node( ).Step-2:Check the List is empty or not by the head pointer i.e. (head = =NULL). If it is NULL then assign the newnode address to head & tail pointer.Step-3:Otherwise assign newnode address to the link field of tail node and to the tail node.

IMPLEMENTATION IN C void create_list( ){ int choice=1; while(choice) { create_node(); if(head= =NULL) tail=head=newnode; else { tail->next=newnode; tail=newnode; } printf(“Do u want to enter one more element press 1 \n”); scanf(“%d”,&choice); }}

35

Page 16: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Insert a node at first

7 NULL

0009

Before Insertion

5 0002 10 0004 15 NULL000F

Head 000F 0002 0004

After Insert First

7 000F 5 0002 10 0004 15 NULL0009

0009Head 0004000F 0002

Data Structures Using C

3. INSERT AN ELEMENT IN THE LIST An element can be inserted in the list at three different places.

a) Insert at firstb) Insert at lastc) Insert at Intermediate (by giving the position)

A) INSERT AT FIRST

AlgorithmStep-1: Call to create_node function.Step-2: Check the head pointer is NULL or not (List Empty). If it is NULL then assign newnode address to head and tail pointer, otherwise assign head value to the link field of newnode & also assign newnode address to the head .

IMPLEMENTATION IN C void insert_first( ) { create_node(); if(head = = NULL) head=tail=newnode; else { newnode->next=head; head=newnode; }}

B) INSERT AT LAST

AlgorithmStep-1: Check the head pointer is NULL or not (List Empty). If it is NULL then assign

36

Page 17: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Before Insertion

5 0002 10 0004 15 NULL000F

Head 000F 0002 0004

Insert a node at Last

7 NULL

0009

After Insert Last

5 0002 10 0004 15 0009 7 NULL000F

000FHead 00090002 0004

Data Structures Using C

newnode address to head as well as tail pointer, otherwise follow the next steps.Step-2: Call to create_node function.Step-3: Assign new node address to the link field of Tail node and also to the tail node.

IMPLEMENTATION IN C void insert_last( ) { if(head= =NULL) head=tail=newnode; else { create_node(); tail->next=newnode; tail=newnode; }}

C) INSERT AT INTERMEDIATE

AlgorithmStep-1: Call to create_node functionStep-2: Check the head pointer is NULL or not (List Empty). If it is NULL then assign a newnode address to the head as well as tail pointer, otherwise follow the next steps.Step-3: Read the data & position in which the new element is going to be inserted.Step-4: By traversing the list, find the address of node in (position-1)th place and name it as tempStep-5: Link field of temp node is assigned to the link field of newnode and also assign

37

Page 18: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

5 0002 10 0004 15 NULL000F

Head 000F 0002 0004

Insert a node at position 2

7 NULL

0009

Before Insertion

After Insert at position 2

5 0009 7 0002 10 0004 15 NULL000F

000FHead 00040009 0002

Data Structures Using C

newnode address to the link field of temp node.

IMPLEMENTATION IN C void insert_intermediate( ){ int i=1,pos; create_node(); printf(“enter the position to insert\n”); scanf(“%d”,&pos); temp=head; if(head= =NULL) head=tail=newnode; else { while(temp!=NULL && i<pos-1) { temp=temp->next; i=i+1; } newnode->next=temp->next; temp->next=newnode; }}

4. DELETE AN ELEMENT FROM THE LIST An element can be deleted from the list at three different places.

a) Delete firstb) Delete lastc) Delete Intermediate(by giving the position)

38

Page 19: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Before Delete First

5 0009 7 0002 10 0004 15 NULL000F

000FHead 00040009 0002

After Delete First

7 0002 10 0004 15 NULL0009

Head 00040009 0002

Data Structures Using C

A) DELETE FIRST

Algorithm

Step-1: Check the List is empty or not. If empty then terminate the process, otherwise assign the link field of first node (i.e. head pointer) to the head pointer.Step-2: Freed the memory space of deleted node by using free( ) library function

IMPLEMENTATION IN C void delete_first( ) { if(head= =NULL) printf(“List empty\n”); else { temp=head; head=head->next; free(temp); }}

B) DELETE LAST

Algorithm

Step-1: Check the emptiness of the list by (head==NULL), if empty terminate the process otherwise follow next.Step-2: Check the link field of first node is NULL or not. If so, then assign NULL value to the head and tail pointer, otherwise follow next step.Step-3: By traversing the list, find the address of node which is a predecessor of last node & name it as temp.Step-4: Assign NULL to the link field of temp and freed the memory space of deleted node.

39

Page 20: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Before Delete Last

5 0009 7 0002 10 0004 15 NULL000F

000FHead 00040009 0002

After Delete Last

5 0009 7 0002 10 NULL000F

Head 0002000F 0009

Data Structures Using C

IMPLEMENTATION IN C void delete_last( ) { if(head= =NULL) printf(“list is empty\n”); else { temp=head; if(head->next= =NULL) { head=tail=NULL; free(temp); } else { while(temp->next->next!=NULL) { temp=temp->next; prev=temp->next; } temp->next=NULL; free(prev); } } }

C) DELETE INTERMEDIATE

AlgorithmStep-1: Check whether the list is empty or not, If empty then terminate the process, otherwise do next.

40

Page 21: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

After Delete at position 2

5 0002 10 0004 15 NULL000F

Head 0004000F 0002

Before Delete position 2

5 0009 7 0002 10 0004 15 NULL000F

000FHead 00040009 0002

Data Structures Using C

Step-2: Read the position of node going to be deletedStep-3: Check the next field of first/head node is NULL or user given position is 1, if so perform delete first.Step-4: Otherwise, find the corresponding node in (position -1)th place by traversing the list & name it as tempStep-5: Assign the link field of link field of temp node to the link field of temp and deallocate the memory for that deleted node

IMPLEMENTATION IN C void delete_intermediate( ){ int pos,i=1; if(head= =NULL) printf(“List is empty\n”); else { printf(“Enter the position of the node to delete\n”); scanf(“%d”,&pos); if((pos= =1)||(head->next= =NULL)) { temp=head; head=head->next; free(temp); } else { temp=head; while(temp!=NULL && i<(pos-1)) { temp=temp->next; i=i+1; } prev=temp->next;

41

Page 22: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

temp->next=temp->next->next; free(prev); } }}

5. TRAVERSAL OF LIST & COUNT THE NO OF ELEMENTS IN THE LIST

Algorithm

Step-1: Check the list is empty or not. if empty then display count is 0 and terminate the process else do nextStep-2: Starting from head node to the last node, visit each node with the help of nodes link field. Step-3: After visiting a node, Display the data field value of that node and also increment the count by 1.

IMPLEMENTATION IN C void traversal( ) { int i; if(head= =NULL) printf(“List empty\n”); else { i=0; temp=head; printf(“elements in the list are\n); while(temp!=NULL) { printf(“%d\t”,temp->data); i=i+1; temp=temp->next; } printf(“\n The count is %d”,&i); }}

6. SEARCH AN ELEMENT IN THE LIST

Algorithm

Step-1: Check the list is empty or not. If empty then display list is empty and terminate the

42

Page 23: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

process else do nextStep-2: Get the search element from the user and perform traversing of a node from head to last nodeStep-3: Match the data field value of a node with the search element, if equal then terminate the process with the user information as element found.

IMPLEMENTATION IN C void search( ) { int ele; if(head= =NULL) printf(“list empty\n”); else { printf(“enter the element to search\n”); scanf(“%d”,&ele); temp=head; while(temp!=NULL) { if(temp->data= =ele) { printf(“element is found\n”); break; } temp=temp->next; } if(temp= =NULL) printf(“element not found\n”); }}

7. MODIFY AN ELEMENT IN THE LIST

Algorithm

Step-1: Check the list is empty or not. If so terminate the process else do nextStep-2: Get the element from the user to be modified. Step-3: Perform the traversing operation & search the element is found or not. If found then read the new element from the user & update itStep-4: Otherwise display element not found.

IMPLEMENTATION IN C void modify( ){

43

Page 24: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

int ele; if(head= =NULL) printf(“List is empty\n”); else { printf(“enter the element to modify”); scanf(“%d”,&ele); temp=head; while(temp!=NULL) { if(temp->data= =ele) { printf(“Enter the new data\n”); scanf(“%d”,&ele); temp->data=ele; break; } temp=temp->next; } if(temp= =NULL) printf(“Element not found”); }}

ADVANTAGES OF LINKED LIST DATA STRUCTURE 1. Insertion and Deletion becomes easy and consumes very less time compare to Array DS

because it needs to change only the link values rather than shifting other elements like Array.

2. Utilization of memory is high because of its dynamic memory allocation.3. It is suitable for the application where the list size is not fixed.

DISADVANTAGES OF LINKED LIST DATA STRUCTURE 1. A node requires memory space not only for storing elements but also needs extra spaces

for storing the link value of next adjacent node. So double amount of memory space is needed in SLL and triple amount is needed in DLL when compared to Array DS.

2. Search operation become complex, because if you wants to identify a node then we need to know the previous node address. So that, only sequential search is possible in Linked List DS. It needs O(N) time to search an element.

44

Page 25: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

2.3.2. OPERATION FUNCTIONS USING DLL

1. NODE CREATION

AlgorithmStep-1: Define a structure of a node using Structure DS. Step-2: Allocate a dynamic memory by using malloc( ) library function with a specified size of node.Step-3: Read the data & assign it to the data field of a node. Also assign NULL value to its Blink and Flink field

IMPLEMENTATION IN C struct node // Skeleton of Node

{struct node *Blink;int data;struct node *Flink;};

struct node *head,*tail,*prev,*temp,*newnode; //Structure Pointershead=NULL;

tail=NULL;

void create_node( ) //Memory allocation and initialization of New Node{newnode=(struct node *)malloc(size of(struct node));if (newnode= =NULL) { printf(“Memory is not allocated for a node\n”); break; }else { printf(“Enter the data value\n”); scanf(“%d”,&newnode->data); newnode->Blink=NULL; newnode->Flink=NULL; }}

2. CREATION OF A LIST

AlgorithmStep-1: make a function call to node creation function (i.e) create_node( ).Step-2: Check the List is empty or not by the head pointer i.e. (head = =NULL). If it is NULL then assign the newnode address to head & tail pointer.

45

Page 26: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Head 000F 0002 0004

000F 5 0002NULL 6 0004000F 8 NULL0002

Before Insertion

Insert a node at first 0009

7 NULLNULL

Data Structures Using C

Step-3: Otherwise assign newnode address to the Flink of tail node, and assign tail node address to Blink of newnode.Step-4: Assign newnode address to the tail node.

IMPLEMENTATION IN C void create_list( ){ int choice=1; while(choice) { create_node(); if(head= =NULL) tail=head=newnode; else { tail->Flink=newnode; newnode->Blink=tail; tail=newnode; } printf(“Do u want to enter one more element press 1 \n”); scanf(“%d”,&choice); }}

3. INSERT AN ELEMENT IN THE LIST An element can be inserted in the list at three different places.

a) Insert at firstb) Insert at lastc) Insert at Intermediate (by giving the position)

A) INSERT AT FIRST

AlgorithmStep-1: Call to create_node function.Step-2: Check the head pointer is NULL or not (List Empty). If it is NULL then assign newnode address to head and tail pointer, otherwise assign head value to the Flink of newnode and newnode address to Blink of headStep-3: Assign newnode address to the head .

46

Page 27: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

After Insert First

0009 0004000F 0002

5 00020009 6 0004000F 8 NULL00027 000FNULL

Head0009

0009

Before Insertion

Insert a node at Last 0009

7 NULLNULL

000F 5 0002NULL 6 0004000F 8 NULL0002

Head 000F 0002 0004

After Insert Last

000F 00090002 0004

6 0004000F 8 00090002 7 NULL00045 0002NULL

Head0009

000F

Data Structures Using C

IMPLEMENTATION IN C void insert_first( ) { create_node(); if(head = = NULL) head=tail=newnode; else { newnode->Flink=head; head->Blink=newnode; head=newnode; }}

B) INSERT AT LAST

AlgorithmStep-1: Check the head pointer is NULL or not (List Empty). If it is NULL then perform insert first operation, otherwise follow the next steps.Step-2: Call to create_node function.Step-3: Assign tail address to the Blink of newnode.Step-3: Assign new node address to the Flink field of Tail node and also to the tail node.

47

Page 28: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Head 000F 0002 0004

000F 5 0002NULL 6 0004000F 8 NULL0002

Before Insertion

Insert a node at 2 0009

7 NULLNULL

After Insert at 2 position

000F 00040009 0002

7 0002000F 6 00040009 8 NULL00025 0009NULL

Head0009

000F

Data Structures Using C

IMPLEMENTATION IN C void insert_last( ) { if(head= =NULL) insert_first( ); else { create_node( ); newnode->Blink=tail; tail->Flink=newnode; tail=newnode; }}

C) INSERT AT INTERMEDIATE

AlgorithmStep-1: Check the head pointer is NULL or not (List Empty). If it is NULL then perform insert first operation, otherwise follow the next steps.Step-2: Call to new node creation function (i.e. create_node)Step-3: Read the data & position in which the new element is going to be inserted.Step-4: By traversing the list, find the address of node in (position-1)th place and name it as tempStep-5: Flink of temp node is assigned to the Flink of newnode and also assign temp to the Blink of newnode.Step-6: Assign newnode address to the Blink of Flink of temp node and also to Flink of temp node.

48

Page 29: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Before Delete first

000F 00040009 0002

7 0002000F 6 00040009 8 NULL00025 0009NULL

Head0009

000F

Data Structures Using C

IMPLEMENTATION IN C void insert_intermediate( ){ int i=1,pos; create_node(); printf(“enter the position to insert\n”); scanf(“%d”,&pos); temp=head; if(head= =NULL) head=tail=newnode; else { while(temp!=NULL && i<pos-1) { temp=temp->Flink; i=i+1; } newnode->Flink=temp->Flink; newnode->Blink=temp; temp->Flink->Blink=newnode; temp->Flink=newnode; }}

4. DELETE AN ELEMENT FROM THE LIST An element can be deleted from the list at three different places.

a) Delete firstb) Delete lastc) Delete Intermediate(by giving the position)

A) DELETE FIRST

AlgorithmStep-1: Check the List is empty or not. If empty then terminate the process, otherwise check Flink of head is not equal to NULL. If so then assign NULL to Blink of Flink of head node.Step-2: Assign head to team and Flink of head to head node.Step-3: Freed the memory space of deleted node by using free( ) library function

49

Page 30: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

After Delete first

0009 7 0002NULL 6 00040009 8 NULL0002

Head 0009 0002 0004

Before Delete last

000F 00040009 0002

7 0002000F 6 00040009 8 NULL00025 0009NULL

Head0009

000F

After Delete last

000F 5 0009NULL 7 0002000F 6 NULL0009

Head 000F 0009 0002

Data Structures Using C

IMPLEMENTATION IN C void delete_first( ) { if(head= =NULL) printf(“List empty\n”); else { If(head->Flink!=NULL) head->Flink->Blink=NULL; temp=head; head=head->Flink; free(temp); }}

B) DELETE LAST

AlgorithmStep-1: Check the emptiness of the list by (head==NULL), if empty terminate the process otherwise follow next.Step-2: Check the Flink field of first node is NULL or not. If so, then assign NULL value to the head and tail pointer, otherwise follow next step.Step-3: By traversing the list, find the address of node which is a predecessor of last node & name it as temp.Step-4: Assign NULL to the Flink field of temp and freed the memory space of deleted node.

50

Page 31: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Before Delete 2 position

000F 00040009 0002

7 0002000F 6 00040009 8 NULL00025 0009NULL

Head0009

000F

Data Structures Using C

IMPLEMENTATION IN C void delete_last( ) { if(head= =NULL) printf(“list is empty\n”); else { temp=head; if(head->Flink= =NULL) { head= NULL; free(temp); } else { while(temp->Flink->Flink!=NULL) { temp=temp->Flink; prev=temp->Flink; } temp->Flink=NULL; free(prev); } } }

C) DELETE INTERMEDIATE

AlgorithmStep-1: Check whether the list is empty or not, If empty then terminate the process, otherwise do next.Step-2: Read the position of node going to be deletedStep-3: Check the Flink of first/head node is NULL or user given position is 1, if so perform delete first.Step-4: Otherwise, find the corresponding node in (position -1)th place by traversing the list & name it as tempStep-5: Assign the Flink of Flink of temp node to the Flink of temp and also assign temp to Blink of Flink of temp node.Step-6: De-allocate the memory for that deleted node

51

Page 32: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

After Delete 2 position

000F 5 0002NULL 6 0004000F 8 NULL0002

Head 000F 0002 0004

Data Structures Using C

IMPLEMENTATION IN C void delete_intermediate( ){ int pos,i=1; if(head= =NULL) printf(“List is empty\n”); else { printf(“Enter the position of the node to delete\n”); scanf(“%d”,&pos); if((pos= =1)||(head->Flink= =NULL)) { temp=head; head=head->Flink; free(temp); } else { temp=head; while(temp!=NULL && i<(pos-1)) { temp=temp->Flink; i=i+1; } prev=temp->Flink; temp->Flink=temp-> Flink -> Flink; temp-> Flink->Blink=temp; free(prev); } }}

Note:

Traversal, Search, Modify and Count operations of DLL is same as SLL except that instead of using “next” as Link, here use “Flink”

52

Page 33: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

5. TRAVERSAL OF LIST & COUNT THE NO OF ELEMENTS IN THE LIST

AlgorithmStep-1: Check the list is empty or not. if empty then display count is 0 and terminate the process else do nextStep-2: Starting from head node to the last node, visit each node with the help of nodes link field. Step-3: After visiting a node, Display the data field value of that node and also increment the count by 1.

IMPLEMENTATION IN C void traversal( ) { int i; if(head= =NULL) printf(“List empty\n”); else { i=0; temp=head; printf(“elements in the list are\n); while(temp!=NULL) { printf(“%d\t”,temp->data); i=i+1; temp=temp->Flink; } printf(“\n The count is %d”,&i); }}

6. SEARCH AN ELEMENT IN THE LIST

AlgorithmStep-1: Check the list is empty or not. If empty then display list is empty and terminate the process else do nextStep-2: Get the search element from the user and perform traversing of a node from head to last nodeStep-3: Match the data field value of a node with the search element, if equal then terminate the process with the user information as element found.

IMPLEMENTATION IN C void search( ) {

53

Page 34: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

int ele; if(head= =NULL) printf(“list empty\n”); else { printf(“enter the element to search\n”); scanf(“%d”,&ele); temp=head; while(temp!=NULL) { if(temp->data= =ele) { printf(“element is found\n”); temp=temp->Flink; break; } temp=temp->Flink; } if(temp= =NULL) printf(“element not found\n”); }}

7. MODIFY AN ELEMENT IN THE LIST

AlgorithmStep-1: Check the list is empty or not. If so terminate the process else do nextStep-2: Get the element from the user to be modify Step-3: Perform the traversing operation & search the element is found or not. If found then read the new element from the user & update itStep-4: Otherwise display element not found.

IMPLEMENTATION IN C void modify( ){ int ele; if(head= =NULL) printf(“List is empty\n”); else { printf(“enter the element to modify”); scanf(“%d”,&ele); temp=head; while(temp!=NULL) {

54

Page 35: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

if(temp->data= =ele) { printf(“Enter the new data\n”); scanf(“%d”,&ele); temp->data=ele; break; } temp=temp->Flink; } if(temp= =NULL) printf(“Element not found”); }}

55

Page 36: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

2.3.3. OPERATION FUNCTIONS USING CSLL

1. NODE CREATION

IMPLEMENTATION IN C (SAME AS SLL) struct node // Skeleton of Node

{int data;struct node *next;};

struct node *head,*tail,*prev,*temp,*newnode; //Structure Pointershead=NULL;

tail=NULL;

void create_newnode( ) //Memory allocation and initialization of New Node{newnode=(struct node *)malloc(size of(struct node));if (newnode= =NULL) { printf(“Memory is not allocated for a node\n”); break; }else { printf(“Enter the data value\n”); scanf(“%d”,&newnode->data); newnode->next=NULL; }}

2. CREATION OF A LIST

IMPLEMENTATION IN C void create_list( ){ int choice=1; while(choice) { create_node(); if(head= =NULL) tail=head=newnode; else { tail->next=newnode;

56

Page 37: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Before InsertionHead 000F 0002 0004

5 0002 10 0004 15 000F000F

Insert a node at first

7 NULL

0009

After Insert First0009Head 0004000F 0002

7 000F 5 0002 10 0004 15 00090009

Head 000F 0002 0004

5 0002 10 0004 15 000F000F

Data Structures Using C

tail=newnode; } newnode->next=head; printf(“Do u want to enter one more element press 1 \n”); scanf(“%d”,&choice); }}

3. INSERT AN ELEMENT IN THE LIST An element can be inserted in the list at three different places.

a) Insert at firstb) Insert at lastc) Insert at Intermediate (by giving the position)

A) INSERT AT FIRST

IMPLEMENTATION IN C void insert_first( ) { create_node(); newnode->next=head; head=newnode; tail->next=head; }

B) INSERT AT LAST

57

Page 38: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Before Insertion

Insert a node at Last

7 NULL

0009

After Insert Last

5 0002 10 0004 15 0009 7 000F000F

000FHead 00090002 0004

5 0002 10 0004 15 000F000F

Head 000F 0002 0004

Insert a node at position 2

7 NULL

0009

Before Insertion

After Insert at position 2

5 0009 7 0002 10 0004 15 000F000F

000FHead 00040009 0002

Data Structures Using C

IMPLEMENTATION IN C void insert_last( ) { if(head= =NULL) insert_first( ); else { create_node(); tail->next=newnode; tail=newnode; newnode->next=head; }}

C) INSERT AT INTERMEDIATE

58

Page 39: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Before Delete First

5 0009 7 0002 10 0004 15 000F000F

000FHead 00040009 0002

After Delete First

15 00097 0002 10 00040009

Head 00040009 0002

Data Structures Using C

IMPLEMENTATION IN C void insert_intermediate( ){ int i=1,pos; printf(“enter the position to insert\n”); scanf(“%d”,&pos); if((head= =NULL) || (pos==1)) insert_first();

else if(head->next= =head) insert_last( ); else {

temp=head; do { if(i<pos-1) { temp=temp->next; i=i+1; } } while(temp!=head); newnode->next=temp->next; temp->next=newnode; }}

4. DELETE AN ELEMENT FROM THE LIST

A) DELETE FIRST

59

Page 40: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Before Delete Last

5 0009 7 0002 10 0004 15 000F000F

000FHead 00040009 0002

After Delete Last

5 0009 7 0002 10 000F000F

Head 0002000F 0009

Data Structures Using C

IMPLEMENTATION IN C void delete_first( ) { if(head= =NULL) printf(“List empty\n”); else { temp=head; if(head->next= =head) { head=NULL; free(temp); } else { head=head->next; free(temp); tail->next=head; } } }

B) DELETE LAST

C)

IMPLEMENTATION IN C void delete_last( ) { if(head= =NULL) printf(“list is empty\n”); else

60

Page 41: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

After Delete at position 2

5 0002 10 0004 15 000F000F

Head 0004000F 0002

Before Delete position 2

5 0009 7 0002 10 0004 15 000F000F

000FHead 00040009 0002

Data Structures Using C

{ if(head->next= =head) { temp=head; head= NULL; free(temp); } else { temp=head; while(temp->next->next!=head) temp=temp->next; prev=temp->next; temp->next=head; free(prev); } } }

D) DELETE INTERMEDIATE

IMPLEMENTATION IN C void delete_intermediate( ){ int pos,i=1; if(head= =NULL) printf(“List is empty\n”); else { printf(“Enter the position of the node to delete\n”); scanf(“%d”,&pos); if((pos= =1)||(head->next= =head))

61

Page 42: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

delete_first( ); else { temp=head; do { if(i<pos-1) { temp=temp->next; i=i+1; } } while(temp!=head); prev=temp->next; temp->next=temp->next->next; free(prev); } }}

Note:

Traversal, Search, Modify and Count operations of CSLL is same as SLL except that wherever we specify “NULL” in SLL program, here we change that as “Head” and instead of using “while loop” here use “do-while loop” for traversing a list.

62

Page 43: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

2.4. CURSOR IMPLEMENTATION OF LISTS ADT

NEED Many languages (such as BASIC, FORTRAN) do not support dynamic memory

allocation and the pointer concepts. If an application needs to implement linked list Data structure without using pointers and dynamic memory allocation concepts then an alternate approach is used, which is named as “Cursor Implementation”

It combines the concept of both Array & Linked list data structures.

The two important features of Linked list ADT are,1. Each node has data as well as Pointer to next node. Just by changing Link value, a

node can be connected to or disconnected from the list.2. Using malloc( ) and free( ) function, we can able to allocate or deallocate the

memory space whenever is needed for a node.

These features can be implemented with the help of Array of Structures. Each array of structure elements can be identified by its index like Array data structures. This index is called as Cursor.

An element referred as ‘Node’ holds a data value as well as link value. The link value is an index value of the next element in array of structure. It is a positive integer, not an address or pointer. This array of structures is called as “Cursor Space”.

Since the size of array of structure is fixed (i.e. Static), this list is referred as Static Linked List. Because of using index value as a link, the accessing of element in list becomes faster compared to pointers in dynamic linked list.

In cursor based implementation, just by changing the link value, a node can be inserted or deleted (like linked list), but the creation of new node becomes static only (like array).

We need two more variables to hold the header node value (Index value of starting node in linked list) and free node value (first empty node index).

Based on the value of header node, we can able to traverse the linked list and based on the value of free node, we can able to identify the index of new node which forms like one more linked list.

INITIALIZATION OF CURSOR LIST

The array of structure has been initialized before storing data on to it. The link value of a node contains the index of immediate node that is a node with index 1 has a link value 2; a node with index 2 has a link value 3; and so on).

The last node link value is initialized as ‘0’ to indicate the end of List. Initialize the head node as -1 and free node as 0 (first index).

ExampleConsider an array of structure ‘Student’ with size ‘6’. (To avoid confusion always the

index 0 will be left it as free because 0th index is used for indicating end of list)

63

Page 44: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

1234 5

Student

End of List

Before initialization of Cursor List After Initialization

Data Link

Student

12345

Before Insert First After Insert First

12345

Head=1 Freenode= 3

12345

Head=3 Freenode= 4

InsertFirst(10)

Data Structures Using C

struct node{ int data; int link;}Student[6];

int head= 0;int freenode=0;

2.4.1. OPERATION FUNCTIONS 1. CREATE NODE

AlgorithmStep-1: Check the fullness of list (freenode= =0). If the list is full then terminate the process otherwise follow next step.Step-2: Assign freenode value to newnode and link value of freenode to freenode.Step-3: Read the data from user and assign it to the data field of newnode. Also assign 0 to the link value of newnode.

2. INSERT FIRST

AlgorithmStep-1: Call to create node function.Step-2: Assign head value to the link field of newnode and newnode value to the head.

64

Page 45: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Before Insert Last After Insert Last

12345

Head=3 Freenode= 4

12345

Head=3 Freenode= 5

InsertLast(15)

Before Delete first After Delete First

12345

Head=3 Freenode= 5

12345

Head=1 Freenode= 3

DeleteFirst( )

Data Structures Using C

3. INSERT LAST

Algorithm

Step-1: Check the head value is zero. If so then assign newnode to the head, otherwise follow next step.Step-2: Traverse the list by using link field value of a node from head to the last node which has zero in its link field.Step-3: Assign newnode value to the link field of last node.

4. DELETE FIRST

Algorithm

Step-1: Check the emptiness of list (head= =0). If so then terminate the process otherwise do next.Step-2: Assign link of head node to the head node.Step-3: Assign the freenode value to the link of deleted node and also assign zero to the data field of deleted node.Step-4: Assign deletednode index value to the freenode.

65

Page 46: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Before Delete Last After Delete Last

12345

Head=1 Freenode= 3

12345

Head=1 Freenode= 4

DeleteLast( )

Data Structures Using C

5. DELETE LAST

Algorithm

Step-1: Check the emptiness of list or link value of head is zero. If so then perform the delete first process else do next.Step-2: Traverse the list and identify the index of previous node of last node and assign zero to the link field of previous node.Step-3: Assign the freenode value to the link of deleted node and also assign zero to the data field of deleted node.Step-4: Assign deletednode index value to the freenode.

IMPLEMENTATION IN C #include<stdio.h>#include<conio.h>#define max 6struct list{ int data; int next;}sp[max];

int head=0,newnode,delnode,freenode,temp;void initialize();int crete_node();void del_node();void insertfirst();void insertlast();int callcount();void display();void delfirst();void dellast();

void main(){

66

Page 47: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

int choice; clrscr(); initialize(); display(); while(1) { printf("\n1.Insert First\n2.Insert Last\n"); printf("3.Delete First\n4.Delete Last\n5.Display\n"); printf("6.Exit\nEnter U'r Choice?\n"); scanf("%d",&choice); switch(choice) { case 1: insertfirst(); break; case 2: insertlast(); break; case 3: delfirst(); break; case 4: dellast();

break; case 5:

display(); break;

case 6: exit(0);

default: printf("Invalid Choice\n"); break;

} }}

void initialize(){ int i; for(i=1;i<max-1;i++) sp[i].next=i+1; sp[max-1].next=0; freenode=1;}

void display()

67

Page 48: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

{ int i; printf("The Information Present in the List are..\n"); printf("Head:%d\tFree Node:%d\n",head,freenode); printf("\t----------\t-------\t\t-------\n"); printf("\tPosition\tData\t\tLink\n"); printf("\t----------\t-------\t\t-------\n"); for(i=1;i<=max-1;i++) printf("\t%d\t\t%d\t\t%d\n",i,sp[i].data,sp[i].next); printf("\t----------\t-------\t\t-------\n");}

int create_node(){ int ele; if(freenode!=0) { newnode=freenode; freenode=sp[freenode].next; printf("Enter the element\n"); scanf("%d",&ele); sp[newnode].data=ele; sp[newnode].next=0; return 1; } printf("List is FULL\n"); return 0;}

void insertfirst(){ if(create_node()) { sp[newnode].next=head; head=newnode; display(); } return;}

void insertlast(){ int i; if(create_node()) { if(head==0)

68

Page 49: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

head=newnode; else { i=head; while(sp[i].next!=0) i=sp[i].next; sp[i].next=newnode; } display(); } return;}

void del_node(){ sp[delnode].data=0; sp[delnode].next=freenode; freenode=delnode;}

int callcount(){ int cnt=0; if(head!=0) { temp=head; while(temp!=0) { temp=sp[temp].next; ++cnt; } return cnt; } else return 0;}

void delfirst(){ if(head!=0) { delnode=head; head=sp[head].next; del_node(); display(); return;

69

Page 50: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

} printf("The List is Empty\n");}

void dellast(){ int prev,temp; if(head!=0&&sp[head].next==0) delfirst(); else { temp=prev=head; while(sp[temp].next!=0) { prev=temp; temp=sp[temp].next; } sp[prev].next=0; delnode=temp; del_node(); display(); }}

Sample Input & OutputThe Information Present in the List are..Head:0 Free Node:1 ---------- ------- ------- Position Data Link ---------- ------- ------- 1 0 2 2 0 3 3 0 4 4 0 5 5 0 0 ---------- ------- -------1.Insert First2.Insert Last3.Delete First4.Delete Last5.Display6.ExitEnter U'r Choice? 1Enter the element 10The Information Present in the List are..Head:1 Free Node:2 ---------- ------- -------

Head:1 Free Node:3 ---------- ------- ------- Position Data Link ---------- ------- ------- 1 10 2 2 30 0 3 0 4 4 0 5 5 0 0 ---------- ------- -------Enter U'r Choice? 3The Information Present in the List are..Head:2 Free Node:1 ---------- ------- ------- Position Data Link ---------- ------- ------- 1 0 3 2 30 0 3 0 4 4 0 5 5 0 0 ---------- ------- -------

70

Page 51: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

Position Data Link ---------- ------- ------- 1 10 0 2 0 3 3 0 4 4 0 5 5 0 0 ---------- ------- -------Enter U'r Choice? 2Enter the element 30The Information Present in the List are..

Enter U'r Choice? 4The Information Present in the List are..Head:0 Free Node:2 ---------- ------- ------- Position Data Link ---------- ------- ------- 1 0 3 2 0 1 3 0 4 4 0 5 5 0 0 ---------- ------- -------

2.5. APPLICATION OF LIST ADT 1. Polynomial manipulation 2. Multi list 3. Radix sort

2.5.1. POLYNOMIAL MANIPULATION Polynomials are expressions containing terms with “non-zero co efficient & exponents”.

Example P(x) = a0 xn+a1 xn-1 +a2x n-2+…..a n-1x1+an

Where p(x) is a polynomial in ‘x’, (a0,a1,a2,…..an) are co-efficient or constants of x & n is an exponent of x, which is a positive integer.

Linked List is used to represent & manipulate the polynomials. Each terms in a linked list referred as node. A node contains 3 fields for representing the polynomial.

1. Co-efficient field2. Exponent field3. Link field

NODE STRUCTURE struct node { int coeff,exp; struct node *next; };

Consider the polynomial p(x)=75x2+36x+55 which is represented in linked list as follows,

71

Page 52: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

0004 000875 2 001236 1 NULL55 0

Head 0004 0008 0012

Co-effi Expo Link

Poly 1 4x3+2x2 +5 Poly 2 5x4+3x3 +3x+2 (+) 5x4+7x3+2x2+3x+7 Resultant PolyTerms of Poly 1 are, 4x3, 2x2 and 5x0

Head 0004 0008 0012

0004 00084 3 00122 2 NULL5 0

(Linked List of Polynomial 1)

0024 00285 4 00323 3 00343 1 NULL2 0

Head 0024 0028 0032 0034

(Linked List of Polynomial 2)

Data Structures Using C

Polynomial Manipulations are,1. Addition2. Subtraction

POLYNOMIAL ADDITION If the exponent of x in two polynomial are same, then the co-efficient of x in two

polynomial are added and the resultant co-efficient with the exponent of x in polynomial one are became a new term in the resultant polynomial.

If the exponent of x in first/second polynomial is not at all a term in second/first polynomial then that term in first/second polynomial becomes directly written in resultant polynomial.

For example, take P1(x) = 4x3+2x2+5, P2(x) = 5x4+3x3+3x+2 for addition

Three linked lists are used for performing the polynomial addition (i.e.) two linked list for representing the input polynomial expressions & one is for representing the resultant polynomial expression.

72

Page 53: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

30007 3 40002 2 50003 1 NULL7 020005 4

3000 4000 5000

1000Head

1000 2000

(Resultant Polynomial Linked List)

Data Structures Using C

ALGORITHM (ADDITION / SUBTRACTION OF TWO POLYNOMIALS USING SLL)

1. CREATION OF POLYNOMIAL LIST

Step-1: Declare two head pointer (head1, head2) for making two linked list and initialize them as NULL.Step-2: Allocate memory for a newnode using malloc( )Step-3: Read co-eff & exponent value for the first polynomial and store it in the co-eff & exponent field of newnode.Step-4: Insert the newnode to the last position of first linked list by using head1 pointer.Step-5: Repeat Step-2 to Step-4 until the user gives input terms of first polynomial.Step-6: Repeat Step-2 to Step-5 for second polynomial and use head2 pointer for making linked list

2. MANIPULATION

Step-1: Check the exponent of head1& exponent of head2a) If both are equal then,

Step-1: Declare head3 pointer for making resultant linked list and initialize them to NULL.Step-2: Allocate memory for a newnode using malloc( ) Step-3: Add the co-eff value of head1 and head2 & store it in the co-eff field of newnodeStep-4: Assign the exponent value of head1 to exponent field of newnode & also assign NULL to the link field of newnodeStep-5: Increment head1 &head2 by its next field value (i.e. head1=head1-> next & head 2=head2->next)Step-6: Link or insert the newnode at last to the linked list using head3 pointer

b) If exponent of head1 is greater than the exponent of head2 thenStep-1: Allocate memory for a newnode using malloc( )Step-2: Assign the co-eff value of head1 to the newnode’s co-eff field & also assign the exponent of head1 to newnode’s exponent fieldStep-3: Link the newnode to the last of linked list using head3 pointerStep-4: Increment the head1 pointer to point the next node

c) If exponent of head2 is greater than the exponent of head1 then,

73

Page 54: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

Step-1: Allocate memory for a newnode using malloc( )Step-2: Assign the co-eff value of head2 to the newnode’s co-eff field & also assign the exponent of head2 to newnode’s exponent fieldStep-3: Link the newnode at last to the linked list using head3Step-4: Increment the head2 pointer to point the next node

Step-2: Repeat Step-1 until any one of the polynomial reaches end of linked list i.e. NULL value in its next field of nodeStep-3: Insert the remaining nodes of linked list in head1 or head2 pointers which don’t reach the NULL value, to the linked list of head3 pointer.

IMPLEMENTATION IN C #include<stdio.h>#include<conio.h>void create_node();void create_poly();void poly_add();void traversal();

struct node{int co_eff,exp;struct node *next;}*head1=NULL,*head2=NULL,*tail2=NULL,*head3=NULL,*newnode=NULL,*temp=NULL;

void main(){clrscr();create_poly();poly_add();getch();}

void create_node(){newnode=(struct node *)malloc(sizeof(struct node));scanf("%d%d",&newnode->co_eff,&newnode->exp);newnode->next=NULL;}

void create_poly(){

74

Page 55: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

int choice,i;for(i=1;i<=2;i++){choice=1;while(choice){printf("Enter coefficient & exponent for the polynomial %d:\n",i);create_node();if(head2==NULL)tail2=head2=newnode;else{tail2->next=newnode;tail2=newnode;}printf("Do you want to enter more term for %d polynomial? press 1\n",i);scanf("%d",&choice);}if(i==1){head1=head2;head2=NULL;}}traversal();}

void traversal(){int i;for(i=1;i<=2;i++){if(i==1)tail2=head1;elsetail2=head2;printf("Polynomial %d:\t",i); while(tail2!=NULL) { printf("%d|%d\t",tail2->co_eff,tail2->exp); tail2=tail2->next; } printf("\n\n");}}

75

Page 56: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

Data Structures Using C

void poly_add(){while(head1!=NULL&&head2!=NULL){if(head1->exp==head2->exp){head1->co_eff=head1->co_eff+head2->co_eff;newnode=head1;temp=head2;head1=head1->next;head2=head2->next;free(temp);newnode->next=NULL;}else if(head1->exp>head2->exp){newnode=head1;head1=head1->next;newnode->next=NULL;}else{newnode=head2;head2=head2->next;newnode->next=NULL;}if(head3==NULL)head3=tail2=newnode;else{tail2->next=newnode;tail2=newnode;}}if(head1!=NULL)tail2->next=head1;elsetail2->next=head2;printf("Resultant polynomial:\t");tail2=head3; while(tail2!=NULL) { printf("%d|%d\t",tail2->co_eff,tail2->exp); tail2=tail2->next; }

76

Page 57: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

430

2 10 ---------> Position of number 430

Data Structures Using C

printf("\n\n");}

Sample I/OEnter coefficient & exponent for the polynomial 1: 4 3Do you want to enter one more term for 1 polynomial? press 1: 1Enter coefficient & exponent for the polynomial 1: 2 2Do you want to enter one more term for 1 polynomial? press 1: 1Enter coefficient & exponent for the polynomial 1: 5 0Do you want to enter one more term for 1 polynomial? press 1: 0Enter coefficient & exponent for the polynomial 2: 5 4Do you want to enter one more term for 2 polynomial? press 1: 1Enter coefficient & exponent for the polynomial 2: 3 3Do you want to enter one more term for 2 polynomial? press 1: 1Enter coefficient & exponent for the polynomial 2: 3 1Do you want to enter one more term for 2 polynomial? press 1: 1Enter coefficient & exponent for the polynomial 2: 2 0Do you want to enter one more term for 2 polynomial? press 1: 0Polynomial 1: 4|3 2|2 5|0Polynomial 2: 5|4 3|3 3|1 2|0Resultant polynomial: 5|4 7|3 2|2 3|1 7|0

2.5.2 RADIX SORT It is used to sort the old-style punch-cards. It also called as bucket sort or card sort, because it follows a mechanism to sort N

elements using 10 buckets of array. Initially all buckets are empty and named as 0th bucket, 1st bucket … 9th bucket. To sort N

integers, we know the range or maximum numbers in the whole number of N integers (i.e.) an integer may be the range or size of (0 to M-1).

For example, take an integer 430. It is the range of M=3, because 430 have 3 numbers on it (i.e. 4, 3, 0). Take 5321. It is the range of M=4, because 5321 have 4 numbers on it (i.e. 5, 3, 2, 1)

ALGORITHM WITH EXAMPLE Step-1: Read the data from user to sort N integers

Input series = {64, 8, 216, 512, 27, 729, 0, 1, 343, 125}

Step-2: Find the maximum range or size for a given input numbers

77

Page 58: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9

0 1 2 3 4 5 6 7 8 9

Data Structures Using C

Maximum range of given input number is M=3 (i.e. Loop from 0 th position value to M-1th

position value.)

Step-3: Place the numbers in bucket according to 0th position value in its range

(Underlined number indicates the 0th position & its corresponding bucket number)

Step-4: Retrieve all N integers from 0th bucket to 9th bucket in the order of bottom to top and replace this as an input series to sort.

Input Series = {0, 1, 512, 343, 64, 125, 216, 27, 8, 729}

Step-5: Repeat Step-3 to Step-4 up to M-1 according to its position value.

When position value = 1

Input Series = {0, 1, 8, 512, 216, 125, 27, 729, 343, 64}

When position value = 2

Input series = {0, 1, 8, 27, 64, 125, 216, 343, 512, 729}

Step-6: write the input series to output.Sorted Output = {0, 1, 8, 27, 64, 125, 216, 343, 512, 729}

78

Page 59: anandhavalli.files.wordpress.com  · Web viewInsertion sort works by taking elements from the list one by one and inserting them in their current position into a new-sorted list.

…..S1 S2 S3 S4 S5

C1

C1

C1

C1

……

Student List Header

CourseListHeader

Data Structures Using C

2.5.3 MULTILIST A university with 40,000 students & 2,500 courses needs to generate two types of report,

1. List of registered students for each course2. List of allotted course for each students

To generate those combinations of list report array data structure is not suitable due to its implementation complexity. Using linked list, it is possible to combine the student & course list into a new structure called as Multilist with less implementation complexity.

In this Multilist, all linked lists are combined by circularly connected fashion including the header pointers. This structure requires double the memory space when compared with array data structure, but it would simplify the implementation difficulties as well as execute the operations with high speed.

79