You are on page 1of 5

ASSIGNMENT

DATA STRUCTURE AND ALGORITHMs

SUBMITTED TO Dr. RAJU G

SUBMITTED BY SAJMA.T.V ROLL NO: 21 2ndsemMSc.Cs

SUBMITTED ON 28/09/2012

PROBLEM
Develop and implement an algorithm that delete from list L1 node whose positions are to be found in an ordered list L2.for example if L1=(A->B->C->D->E) and L2=(2->4->8) then the second and fourth nodes are to be deleted from list L1.(the eight node does not exist) and after deletion L1=(A->C->E).

SOLUTION
We know that a linked list or a one-way list is a linear collection of data elements called nodes where linear order is given by means of pointers.Each node is divided into two parts :Data part which contains the information of the element and link part which contains the address of the next node in the list. Our problem is to develop an algorithm which insert a new node with a value m after every node with value k. We use noadd() method to read integer values to both lists L1 and L2.If L1 contain 8 elements given below then L2 will contain the position of two's power. We use trav() method to travel throughout the list L1 and delete the value at the position of list L2.

89

78

67

56

45

34

23

12

List L1
8 4 2

List L2 fig:lists after calling noadd().


89 78 67 56 45 34 23 12

List L1
8 4 2

List L2 fig:lists during trav() and the arrow marked values are at the position specified in L2, that is to be deleted .

Assignment

89

67

45

34

23

List L1
8 4 2

List L2 fig:lists after calling print(). PROGRAM


#include<iostream> #include<math.h> using namespace std; typedef struct node { int data; node *next; node() { next=NULL; data=0; } }; typedef node *nodeptr; class slist { nodeptr head; public: int x; int count,i; slist() { head=NULL; } void noadd(int x) //this function create lists { nodeptr p1; p1=new node; p1->data=x; p1->next=head; head=p1; } void trav() //this function delete and traverse through list { int count=0,r=0,t=1;

Dept. of IT

Assignment nodeptr p1,p2,prev,cur; prev=cur=head; p1=head; while(cur) { r=pow(2,t); count++; if(r==count) { prev->next=cur->next; cout<<"deleted numbers are="<<cur->data<<"\n"; p1=cur; t++; delete p1; cur=cur->next; } else { prev=cur; cur=cur->next; } } } void print() //this function print list { nodeptr p1; cout<<"list="; p1=head; while(p1) { cout<<p1->data<<"\n"; p1=p1->next; } } }; main() { slist l1,l2; int i=0,x,n,count=0,val=0,t=1,r=0,m=0; cout<<"enter the limit"; cin>>n; while(i<n) { cout<<"enter the no"; cin>>x; l1.noadd(x); count++; val=pow(2,t);

Dept. of IT

Assignment if(count==val) { l2.noadd(count); t++; } i++; } l1.print(); l2.print(); l1.trav(); l1.print(); }

OUTPUT
enter the limit8 enter the no12 enter the no23 enter the no34 enter the no45 enter the no56 enter the no67 enter the no78 enter the no89 list=89 78 67 56 45 34 23 12 list=8 4 2 deleted numbers are=78 deleted numbers are=56 deleted numbers are=12 list=89 67 45 34 23

CONCLUSION
Thus proved that we can develop and implement an algorithm which can deletes from list L1 node whose positions are to be found in an ordered list L2.

Dept. of IT

You might also like