You are on page 1of 18

Data Structure And Algorithm

(EC-594B)

A. K. Siromoni

Department of Information Technology


Lecture 2

Lecture Objective 1 : Write programs for complex problems using the


advantages of algorithmic decomposition

Lecture Objective 2 : Apply the knowledge to solve problems related to Link


List

Lecture Objective 3 : Justify the use of different types of Link List in different
situation
Last Lecture
• We have understood Algorithmic Decomposition

Advantages of Algorithmic Decomposition :

1. Increases Reusability of Codes

2. Smaller Code Size , so less complex and easy to debug


Circular Link List

• No Null Pointer . So Any Node Can be the Head.


Implementation through Algorithmic Decomposition

Circular Link List


Implementation in pseudo code

1. Link { Data; Next Link Address} //Data Type


2. Link_address Head, Node // Start Storage
3. Head =createnode()
4. Putdata(Head)
5. Node=Head
6. Node_next = createnode()
7. Node= Node_next
8. Putdata(Node)
9. Ask user if answer = yes coninue, go to 6
10. Node Next = Head //End Storage
11. Showdata(Head) // Related Operation
12. Showdata(Node) // Related Operation
Implementation through Algorithmic Decomposition
Comparison of Algorithm : Linear Link list and Circular Link List
Linear Link List
1. Link { Data; Next Link Address} //Data type
2. Link_address Head, Node // Start Storage
3. Head =createnode()
4. Putdata(Head)
5. Node=Head
6. Node_next = createnode()
7. Node= Node_next
8. Putdata(Node)
9. Ask user if answer = yes coninue, go to 6 //End Storage
10. Showdata(Head) // Related Operation

Circular Link List


1. Link { Data; Next Link Address} //Data type
2. Link_address Head, Node // Start Storage
3. Head =createnode()
4. Putdata(Head)
5. Node=Head
6. Node_next = createnode()
7. Node= Node_next
8. Putdata(Node)
9. Ask user if answer = yes coninue, go to 6
10. Node Next = Head //End Storage
11. Showdata(Head) // Related Operation
12. Showdata(Node) // Related Operation
Advantages of Algorithmic Decomposition
Partial implementation in C language
/* Circular Link List in C Language */

#include<stdio.h>

struct link { int data;


struct link *next; } ;

main() { struct link *head , *node;


head = createnode();
putdata(head);
node=head;
while(getans()){
node->next = createnode();
node=node->next;
putdata(node);}
node->next=head;
showdata(head);
showdata(node);}
Advantages of Algorithmic Decomposition
Comparison of Implementation : Linear Link list and Circular Link List

/* Linear Link List */ /* Circular Link List */


#include<stdio.h> #include<stdio.h>
struct link { int data;
struct link *next; } ; struct link { int data;
struct link *next; } ;
main() { struct link *head , *node;
head = createnode(); main() { struct link *head , *node;
putdata(head); head = createnode();
node=head; putdata(head);
while(getans()){ node=head;
node->next = createnode(); while(getans()){
node=node->next; node->next = createnode();
putdata(node);} node=node->next;
showdata(head);} putdata(node);}
node->next=head;
showdata(head);
showdata(node);}
New Definition of Show Data
( AS No NULL POINTER IS AVAILABLE )
•Analysis of showdata
showdata(head);

Decision : Return data type – void


Argument passed – one
Argument data type – struct link *

Prototype : void showdata( struct link *);

•Definition of showdata

void showdata(struct link * node){


struct link * head = node;
do{
printf (“%d \n”, node->data);
node = node->next;}while (node!=head);
}
• So the final program for creation and traversal of CirCular Link List

#include<stdio.h>
struct link { int data;
struct link *next; } ;
main() { struct link *head , *node;
void showdata(struct link * node){ head = createnode();
struct link * head = node; putdata(head);
do{ node=head;
printf (“%d \n”, node->data); while(getans()) {
node = node->next;}while (node!=head); } node->next = createnode();
node=node->next;
int getans(){int ans; putdata(node);
printf(“put 0 for stop , non zero to }
continue”); node->next=head;
scanf(“%d”, &ans); return ans;} showdata(head);
showdata(node); }
void putdata(struct link * node){ int data;
scanf(“%d”, &data);
node->data = data;
node->next = NULL; }

struct link * createnode()


{ struct link * node;
node = (struct link *) malloc (sizeof(struct
link));
return node; }
Doubly Linked List

• Either HEAD or LAST node address should be remembered


Implementation through Algorithmic Decomposition contd.

Implementation in pseudo code

1. Link { Data; Next Link Address, Previous Link Address} //Data type
2. Link_address Head, Node, Prev = NULL // Start Storage
3. Head =createnode()
4. Putdata(Head, Prev)
5. Node=Head
6. Node_next = createnode()
7. Prev = Node
8. Node= Node_next
9. Putdata(Node, Prev)
10.Ask user if answer = yes coninue, go to 6 //End Storage
11.Showdata(Head) // Related Operation
12.Showdata(Node) // Related Operation
Implementation through Algorithmic Decomposition contd.
Partial implementation in C language
/*Doubly Link List in C Language */
#include<stdio.h>

struct link { int data;


struct link *next;
struct link * previous;} ;

main() { struct link *head , *node , *prev =NULL;


head = createnode();
putdata(head, prev);
node=head;
while(getans()){
node->next = createnode();
prev = node;
node=node->next;
putdata(node, prev);}
showdata1(head);
showdata2(node);}
We have to redefine only putdata
•Analysis of putdata
putdata(head, prev);
putdata(node, prev);

Decision : Return data type void


Argument passed – two
Argument data type ( both )– struct link *

Prototype : void putdata(struct link *, struct link *);

•Definition of putdata

void putdata(struct link * node, struct link * prev){ int data;


scanf(“%d”, &data);
node->data = data;
node->next = NULL;
node->previous=prev; }
Implementation through Algorithmic
Decomposition contd.
•Analysis of showdata
showdata1(head); showdata2(node)

Decision : Return data type – void


Argument passed – one
Argument data type – struct link *

Prototype : void showdata( struct link *);

•Definition of showdata1 (head) •Definition of showdata2 (node)


void showdata(struct link * node){ void showdata(struct link * node){
while (node != NULL){ while (node != NULL){
printf (“%d \n”, node->data); printf (“%d \n”, node->data);
node = node->next;} } node = node->previous;} }
• So the final program for creation and traversal of link list

#include<stdio.h>
struct link { int data;
struct link *next; struct link * createnode() {
struct link * previous;} ; struct link * node;
node = (struct link *) malloc (sizeof(struct link));
void showdata1(struct link * node){ return node; }
while (node != NULL){
printf (“%d \n”, node->data);
node = node->next; } }
main() { struct link *head , *node, *prev=NULL;
void showdata2(struct link * node){ head = createnode();
while (node != NULL){ putdata(head, prev);
printf (“%d \n”, node->data); node=head;
node = node->previous; } } while(getans()) {
node->next = createnode();
prev=node;
int getans(){int ans; node=node->next;
printf(“put 0 for stop , non zero to continue”); putdata(node, prev);
scanf(“%d”, &ans); return ans;} }
showdata1(head);
void putdata(struct link * node, struct link *prev){ showdata2(node)
int data; }
scanf(“%d”, &data);
node->data = data;
node->next = NULL;
node ->previous = prev; }
Assignment 2
1. Josephus Problem
Given a group of n men arranged in a circle under the edict that every mth man
will be executed going around the circle until only one remains, find the
position in which you should stand in order to be the last survivor .
( m and n will be given by user )

2. Doubly Circular Link List :


Create and show the data.

3. Polynomial
Given two polynomial numbers represented by linked list. Write a function that add
these lists means add the coefficients who have same variable powers.

( A polynomial is an expression of finite length constructed from variables,


constants and non-negative integer exponents.
An example of a polynomial is 3x2+2x+7; here, the total number of terms is 3.
The coefficients of each term are 3, 2, 7 and degrees 2, 1, 0 respectively.)
( Every Program should be properly documented )

• Covers LO1, LO2 and LO3


Self Learning
For Assignment 2

1. Data Structure Using C & C++ : Langsum, Augenstein , Tanenbaum ,


Second Edition, Prentice Hall of India

2. Data Structures : Seymour Lipschutz , Indian Adapted Edition


2006, Tata McGaw Hill Education Pvt Ltd

3. https://en.wikipedia.org/wiki/Josephus_problem

4. https:// www.tutorialspoint.com/ data_structures_algorithms

5. https://www.youtube.com/watch?v=3tDBX4-0Jt4

You might also like