91
DATA STRUCTURE AND PROGRAMMING METHODOLOGY BABA HIRA SINGH BHATTAL INSTITUTE OF ENGINEERING AND TECHNOLOGY, LEHRAGAGA, SANGRUR Submitted By : Submitted To: Page 1

DSPM Experiments

Embed Size (px)

Citation preview

Page 1: DSPM Experiments

DATA STRUCTUREAND

PROGRAMMING METHODOLOGY

BABA HIRA SINGH BHATTAL INSTITUTE OF ENGINEERING AND TECHNOLOGY, LEHRAGAGA, SANGRUR

Submitted By: Submitted To:

SUSHANT RANJAN MRS.KIRAN ARORA

CSE-3rd semester

100070303895

Page 1

Page 2: DSPM Experiments

INDEXSr.No. PROGRAM DATE PAGE SIGNATURE

1 CREATING AN ARRAY 25/09/2011 4-5

2 TRAVERSING AN ARRAY 25/09/2011 6-7

3 INSERTIONTO AN ARRAY 27/09/2011 7-11

4 DELETING AN ARRAY 28/09/2011 12-15

5 LINEAR SEARCH IN AN ARRAY 02/10/2011 16-20

6 BINARY SEARCH IN AN ARRAY 05/10/2011 21-22

7 BUBBLE SORT 04/10/2011 23-26

8 MULTIPLICATIONOF TWO MATRICES

28/09/2011 27-37

9 LINKED LIST(INSERTION)

*AT THE BEGINNING

*AT THE END

*AT ANY POSITION

28/09/2011 38-55

10 LINKED LIST(DELETION)

* AT THE BEGINNING

*AT THE END

*AT ANY POSITION

28/09/2011 56-71

11 SEARCHING IN A LINKED LIST 29/09/2011 72-80

Page 2

Page 3: DSPM Experiments

PROGRAM 1: OPERATIONS ON AN ARRAY (INSERTION, DELETION, SEARCHING AND SORTING)

An array is the collection of different variables under the same name. when elements of linear structure are represented by means of sequential or contiguous memory locations, these linear structures are called arrays. It is one of the simplest data structures and are very easy to traverse, search, sort, insertion and deletion.

Page 3

Page 4: DSPM Experiments

LINEAR ARRAY

/*NAME OF THE PROGRAM=CREATING AN ARRAY DESIGNED BY= SUSHANT RANJAN DATE=25\09\2011*/

#include<iostream.h>#include<conio.h>void main(){clrscr();inti,n,a[6];cout<<"\n"<<"\t\t\tENTER THE NUMBER OF ELEMENTS=";cin>>n;cout<<"ENTER THE SERIES"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]=";cin>>a[i];}cout<<"\n"<<"THE SERIES IS"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";}getch();}

Page 4

Page 5: DSPM Experiments

Page 5

Page 6: DSPM Experiments

TRAVERSING AN ARRAY

/*NAME OF THE PROGRAM=TRAVERSING AN ARRAY THE PROGRAM ADDS 10 TO EACH ELEMENT OF THE ARRAY DESIGNED BY=SUSHANT RANJAN DATE=25/09/2011*/

#include<iostream.h>#include<conio.h>#include<dos.h>void main(){clrscr();intn,a[6];int i;cout<<"\n"<<"\t\t\tENTER THE ELEMENTS OF THE ARRAY=";cin>>n;for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]=";cin>>a[i];}delay(500);cout<<"\n"<<"ELEMENTS OF THE ARRAY ARE="<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";}for(i=0;i<6;i++){a[i]=a[i]+10; //ELEMENTS ARE INCREAMENTED BY 10}delay(500);cout<<"\n"<<"ELEMENTS AFTER OPERATION";for(i=1;i<=n;i++){cout<<"\n"<<"ELEMENT["<<i<<"]="<<a[i];}getch();}

Page 6

Page 7: DSPM Experiments

Page 7

Page 8: DSPM Experiments

INSERTION INTO AN ARRAY

TOPIC: Insertion of an element in an Array.

Insertion in an array means addition of a new element in a data structure. Insertion of an element in array can be done in two ways:

1- If the array to which the element is to be inserted is unordered (unsorted).

2- If the array to which the element is to be inserted is ordered (sorted).

For insertion in arrays, we need to have two information, the element to be inserted and the position to which it will be inserted.

EXAMPLE:

Suppose we have the following array:

arr[5]={5,7,2,1,3}

And we need to insert the element 6 at the 2nd position, after insertion:

arr[5]={5,6,7,2,1,3}

That’s exactly how insertion is done!

ALGORITHM:

Algorithm for Insertion of an element in an array

Suppose, the array to be ‘arr[max]’, ‘pos’ to be the position at which the element ‘num’ has to be inserted. For insertion, all the elements starting from the position pos are shifted towards their right to make a vacant space where the element num is inserted.

STEP 1: set i=max.

STEP 2: repeat steps 3 & 4 while i>=pos.

Page 8

Page 9: DSPM Experiments

STEP 3: set arr[i+1]=arr[I].

STEP 4: set i=i-1;

STEP 5: set arr[i] = num.

STEP 6: set max=max+1;

STEP 7: exit.

Page 9

Page 10: DSPM Experiments

/*NAME OF THE PROGRAM=INSERTING AN ELEMENT TO ARRAY DESIGNED BY=SUSHANT RANJAN DATE=27\09\2011*/

#include<iostream.h>#include<conio.h>void main(){clrscr();int i;int a[6];intn,pos;intnum;cout<<"\n"<<"\t\t\tENTER THE SIZE OF THE ARRAY=";cin>>n;cout<<"ENTER THE SERIES"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT ["<<i<<"]=";cin>>a[i];}cout<<"\n"<<"THE SERIES IS"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";}cout<<"\n"<<"ENTER THE POSITION=";cin>>pos;cout<<"\n"<<"ENTER THE NUMBER=";cin>>num;n++;for(i=n;i>=(pos);i--){a[i]=a[i-1];}a[pos]=num;cout<<"\n"<<"THE NEW SERIES IS"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";}getch();}

Page 10

Page 11: DSPM Experiments

Page 11

Page 12: DSPM Experiments

DELETION FROM AN ARRAY

TOPIC: Deletion of an element from an Array.

Deletion means removal of a data element from a data structure. The data element is searched for before its removal. The position of the element to be deleted is inserted and the element is removed and rest of the elements are shifted so as to keep the order of array undisturbed.

EXAMPLE:

Suppose we have the following array:

arr[5]={5,6,7,2,1}

Now we wish to delete the element at position 3rd.

After deletion array is:

arr[5]={5,6,2,1,0}

All the elements after the 3rd have shifted to their left and the vacant space is filled with 0.

That’s exactly how deletion is done!

ALGORITHM:

Algorithm for Deletion of an element in an array:

Suppose, the array to be ‘arr[max]’, ‘pos’ to be the position from which the element has to be deleted. For deletion, all the elements to the right of the element at position pos are shifted to their left and the last vacant space is filled with 0.

Page 12

Page 13: DSPM Experiments

STEP 1: repeat steps 2 & 3for j=pos to j=max-1.

STEP2: set arr[j]=arr[j+1].

STEP3: set j=j-1;

STEP 4: set max=max-1;

STEP 5: exit.

Page 13

Page 14: DSPM Experiments

/*NAME OF THE PROGRAM=DELETING AN ELEMENT FROM THE ARRAY DESIGNED BY=SUSHANT RANJAN DATE=28/09/2011*/

#include<iostream.h>#include<conio.h>void main(){clrscr();int a[6];intn,i;intpos;cout<<"\n"<<"\t\t\tENTER THE SIZE OF THE ARRAY=";cin>>n;cout<<"ENTER THE SERIES"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]=";cin>>a[i];}cout<<"\n"<<"THE SERIES IS"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";}cout<<"\n"<<"ENTER THE POSITION=";cin>>pos;for(i=(pos+1);i<=n;i++){a[i-1]=a[i];a[i]=0;}n--;cout<<"\n"<<"THE NEW SERIES IS"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";}getch();}

Page 14

Page 15: DSPM Experiments

Page 15

Page 16: DSPM Experiments

SEARCHING

TOPIC: Searching an element in an array.

Searching is the operation of searching or finding a specified data element in a data structure. The two most common searching techniques are:

1- Linear search.2- Binary search.

Linear Search:

In linear search, each element of the array is compared with the given ITEM to be searched for, one by one. This method, which traverses the array sequentially to locate the given ITEM, is called linear or sequential search.

ALGORITHM:

Algorithm for linear search.

Algorithm for searching ITEM in an array arr[max] with lower bound ‘L’ and upper bound ‘U’. as soon as the search is successful, it jumps out of the loop otherwise continues till the last element.

STEP 1: Set k=0;

STEP 2: Repeat steps 3 and 4 until k>max.

STEP 3: if ITEM==arr[k]

{

Print “SEARCH SUCCESSFUL”

Print “position of element is”, k.

Page 16

Page 17: DSPM Experiments

Break. /* end loop */

}

STEP 4: k=k+1

STEP 5: If k>max then

Print “SEARCH UNSUCCESSFUL”.

STEP 6: Exit.

Binary Search:

Binary search method is popular for searching a specific item in an ordered array (sorted). It can perform the search in minimum possible comparisons, but it needs the array to be sorted in any order. In binary search , the ITEM is searched for in smaller segments (nearly half the previous segment) after every stage.

ALGORITHM:

Algorithm for Binary search.

Suppose,

The array to be AR[SIZE] having SIZE number of elements. L is the index number of the lower element. We take it to be 0.

U is the index number of the upper (last) element. It will be (SIZE-1).

ITEM is the data that needs to be searched.

beg, last and mid are variables of type int(eger).

Here is the algorithm:

1. Set beg = L AND last = U2. REPEAT STEPS 3 THROUGH 6 TILL beg<=last3. mid = ( (beg+last)/2)4. IF AR[mid] = ITEM THEN

Page 17

Page 18: DSPM Experiments

5. ITEM IS AT POSITION mid6. BREAK THE LOOP7. IF AR[mid] < ITEM THEN8. beg = mid+19. IF AR[mid] > ITEM10. last = mid-111. END OF LOOP12. IF AR[mid] = ITEM THEN13. SEARCH IS UNSUCCESSFULL

Page 18

Page 19: DSPM Experiments

LINEAR SEARCH

/*NAME OF THE PROGRAM=LINEAR SEARCH DESIGNED BY=SUSHANT RANJAN DATE=2/10/2011*/

#include<iostream.h>#include<conio.h>void main(){clrscr();int a[6];intn,i;intnum;cout<<"\n"<<"\t\t\tENTER THE SIZE OF THE ARRAY=";cin>>n;cout<<"ENTER THE SERIES"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]=";cin>>a[i];}cout<<"\n"<<"THE SERIES IS"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";}cout<<"\n"<<"ENTER THE ELEMENT=";cin>>num;a[n+1]=num;intloc=0;while(a[loc]!=num){loc++;}if(loc==(n+1)){cout<<"\n"<<"SEARCH UNSUCCESSFUL";}elsecout<<"\n"<<"LOCATION="<<(loc);

Page 19

Page 20: DSPM Experiments

getch();}

Page 20

Page 21: DSPM Experiments

BINARY SEARCH

/*NAME OF THE PROGRAM=BINARY SEARCH DESIGNED BY=SUSHANT RANJAN DATE=5/10/2011*/

#include<iostream.h>#include<conio.h>void main(){clrscr();int a[5];inti,n;intnum,beg,end,mid;cout<<"\n"<<"\t\tENTER THE NUMBER OF ELEMENTS IN THE SERIES=";cin>>n;cout<<"ENTER THE SERIES"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]=";cin>>a[i];}cout<<"\n"<<"THE SERIES IS"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";}cout<<"\n\n"<<"ENTER THE NUMBER FOR THE SEARCH OPERATION=";cin>>num;beg=1;end=n;while(beg<=end){mid=int((beg+end)/2);if(a[mid]==num){cout<<"\n"<<"THE LOCATION IS="<<(mid);break;}if(num<a[mid])

Page 21

Page 22: DSPM Experiments

end=(mid-1);else//if(num>a[mid])beg=(mid+1);}if(a[mid]!=num)cout<<"SEARCH UNSUCCESSFUL";getch();}

Page 22

Page 23: DSPM Experiments

BUBBLE SORTING

TOPIC: Sorting an Array using Bubble Sort.

Sorting is the method of arranging the elements of an array in an order (ascending or descending).The basic idea behind bubble sort method of sorting is to keep on comparing adjoining elements of the array from the first until the last and interchanging them if they are not in proper order. The whole sequence is repeated several times when the array becomes sorted.

EXAMPLE:

Suppose we have the following array:

arr[5]={5,1,7,2,8}

Now we wish to sort the array using bubble sort.

Pass1: {1, 5, 2, 7, 8}

Pass2: {1, 2, 5, 7, 8}

Pass3: {1, 2, 5, 7, 8}

Pass4: {1, 2, 5, 7, 8}

After sorting array is:

arr[5]={1, 2, 5, 7, 8}

During each pass in bubble sorting, one largest number in an unsorted array is brought to its correct position.

That’s exactly how sorting is done!

Page 23

Page 24: DSPM Experiments

ALGORITHM:

Algorithm for sorting an array using Bubble Sort.

The array (to be sorted) to be AR[SIZE] having SIZE number of elements.

L is the index number of the lower element. We take it to be 0, since the whole array has to be sorted.

U is the index number of the upper (last) element. It will be (SIZE-1).

Here is the algorithm of sorting the array using bubble sort

1. FOR I = L TO U2. FOR J = L TO (U-1)3. IF AR[J] > AR[JA1] THEN4. temp = AR[J]5. AR[J] = AR[J+1]6. END OF INNER LOOP7. END OF OUTER LOOP

Page 24

Page 25: DSPM Experiments

/*NAME OF THE PROGRAM=BUBBLE SORTING DESIGNED BY=SUSHANT RANJAN DATE=4/10/11*/

#include<iostream.h>#include<conio.h>void main(){clrscr();int a[8];inti,n,j;cout<<"\n"<<"\t\t\tENTER THE SIZE OF THE ARRAY=";cin>>n;cout<<"ENTER THE SERIES"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]=";cin>>a[i];}cout<<"\n"<<"THE SERIES IS"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";}

for(i=1;i<=(n-1);i++){for(j=1;j<=(n-i);j++){if(a[j]>a[j+1]){int temp;temp=a[j];a[j]=a[j+1];a[j+1]=temp;}}}cout<<"\n"<<"THE SORTED SERIES IS"<<"\n";for(i=1;i<=n;i++){cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";}

Page 25

Page 26: DSPM Experiments

getch();}

Page 26

Page 27: DSPM Experiments

PROGRAM-02: PERFORM MULTIPLICATION OF TWO MATRICES LINEAR OR MULTIDIMENSIONAL).

Introduction to arrays:

An array is a collection of variables of the same type that are referenced by the same name. When elements of linear structure are represented by means of sequential or contiguous memory locations, these linear structures are called arrays.

Two types of arrays:

a)- one-dimensional arrays b)-multi-dimensional arrays

A) Single dimensional array : It is the simplest form of array. The data type of an array is known as the BASE TYPE of the array. The elements of the array are referred to by only a single subscript. It is also called ‘VECTOR’.

B) Multi dimensional array : It is an array in which each element is itself an array. It is also known as ‘array of arrays’. The no. of elements in a 2-D array is determined by multiplying the number of rows and number of columns i.e. M*N. It is also called ‘MATRIX’.

column 0 1 2 3

Page 27

Array name (grade)

Address 2000 2001 2002 2003 2004 2005 2006 2007

Row 0

Row 1

Page 28: DSPM Experiments

A[1][1]

/* WRITE A PROGRAM TO FIND THE MULTIPLICATION OF TWO ARRAYS

DESIGNED BY - SUSHANT RANJAN

DATE- 28TH SEPTEMBER,2011 */

#include<iostream.h>

#include<conio.h>

#include<dos.h>

#include<ctype.h>

#include<process.h>

int i;

void exit()

{

cout<<"\n\n\n\n\n\n\n\t\t\t\t";

cout<<"PROGRAM TERMINATING ";

for(i=0;i<6;i++)

{

Page 28

Page 29: DSPM Experiments

delay(1000);

cout<<".";

}

exit(0);

}

void main()

{

clrscr();

int n,i,j;

char ch,ch2;

do

{

clrscr();

menu:

cout<<"\n\n\n\t\t\t*********MENU*********";

cout<<"\n\n\n\n\tFIND THE MULTIPLICATION OF : \n\n\t\ta)LINEAR ARRAY\n\t\tb)2-D ARRAY\n\t\tq)QUIT";

cout<<"\n\n\n\tENTER YOUR CHOICE : ";

ch=getche();

ch=tolower(ch);

delay(500);

clrscr();

Page 29

Page 30: DSPM Experiments

switch(ch)

{

case 'a':int arr[10],arr1[10],mul=0;

cout<<"\n\n\n\nYOU CHOSE LINEAR ARRAY ";

star:

cout<<"\n\n\nNO. OF ROWS OF 1st ARRAY :"; //no.of columns of 2nd array=no. of rows of 1st array

cin>>n;

if(n<=0)

{

cout<<"\n\nwrong choice ";

goto star;

}

//taking the values of elements of two arrays

cout<<"\n\n\n\nENTER ELEMENTS OF 1st ARRAY\n";

for(i=0;i<n;i++)

{

cout<<"\nelement["<<i+1<<"] :";

cin>>arr[i];

}

clrscr();

cout<<"\n\n\n\nENTER ELEMENTS OF 2nd ARRAY\n";

for(i=0;i<n;i++)

{

Page 30

Page 31: DSPM Experiments

cout<<"\nelement["<<i+1<<"] :";

cin>>arr1[i];

}

clrscr();

//printing two arrays

cout<<"\n\n\n\nTWO ARRAYS ARE :";

cout<<"\n\n\t1st ARRAY\t\t\t\t2nd ARRAY\n";

for(i=0;i<n;i++)

{

cout<<"\n\t\t"<<arr[i]<<"\t\t\t\t\t"<<arr1[i];

}

//multiplication of linear arrays

for(i=0;i<n;i++)

{

mul=mul+(arr[i]*arr1[i]);

}

cout<<"\n\n\n\nMULTIPLICATION OF TWO ARRAYS IS : "<<mul;

break;

case 'b':int a[10][10],a1[10][10],mul1[10][10],m,r;

cout<<"\n\n\n\nYOU CHOSE 2-D ARRAY ";

Page 31

Page 32: DSPM Experiments

start:

cout<<"\n\nNO. OF ROWS OF 1st ARRAY : "; //no.of columns of 2nd array=no. of rows of 1st array

cin>>n;

cout<<"\nNO. OF COLUMN OF 1st ARRAY : ";

cin>>m;

if(n>10||m>10) //check for correct input

{

cout<<"wrong choice (insert values < 10 )";

goto start;

}

//taking the values of elements of 1st array

cout<<"\n\n\nENTER ELEMENTS OF 1st ARRAY\n";

for(i=0;i<n;i++)

{

for(j=0;j<m;j++)

{

cout<<"\nelement["<<i+1<<"]["<<j+1<<"] :";

cin>>a[i][j];

}

}

delay(500);

clrscr();

cout<<"\n\n\nNO.OF COLUMNS OF 2nd ARRAY : ";

Page 32

Page 33: DSPM Experiments

cin>>r;

cout<<"\n\n\n\nENTER ELEMENTS OF 2nd ARRAY\n";

for(i=0;i<m;i++)

{

for(j=0;j<r;j++)

{

cout<<"\nelement["<<i+1<<"]["<<j+1<<"] :";

cin>>a1[i][j];

}

}

clrscr();

//printing two arrays

cout<<"\n\n\n\nTWO ARRAYS ARE :";

cout<<"\n\n\t1st ARRAY";

for(i=0;i<n;i++)

{

cout<<"\n";

for(j=0;j<m;j++)

cout<<"\t"<<a[i][j];

}

cout<<"\n\n\t2nd ARRAY";

Page 33

Page 34: DSPM Experiments

for(i=0;i<m;i++)

{

cout<<"\n";

for(j=0;j<r;j++)

cout<<"\t"<<a1[i][j];

}

//multiplication of linear arrays

cout<<"\n\n\nARRAY AFTER MULTIPLICATION :\n\n\n";

for(i=0;i<n;i++)

{

cout<<"\n\t";

for(j=0;j<r;j++)

{

for(int s=0;s<m;s++)

mul1[i][j]+=a[i][s]*a1[s][j];

cout<<mul1[i][j]<<" ";

}

}

break;

case 'q':clrscr();

exit();

Page 34

Page 35: DSPM Experiments

default :char ch1;

cout<<"\n\n\n\n\nWRONG CHOICE";

cout<<"\n\n wanna return to main menu(y/n) : ";

ch1=getche();

if(ch1=='y'||ch1=='Y')

goto menu;

}

cout<<"\n\nU WANNA CONTINUE (Y/N) : ";

ch2=getche();

delay(500);

}while(ch2=='y'||ch2=='Y');

getch();

}

Page 35

Page 36: DSPM Experiments

OUTPUT :

Page 36

Page 37: DSPM Experiments

Page 37

Page 38: DSPM Experiments

PROGRAM-3: INSERTION OPERATIONS ON A LINKED LIST (INSERTION AT THE BEGINNING, AT THE END AND IN BETWEEN THE LINKED LIST).

Introduction to Linked Lists

A linked list is a linear collection of data elements, called nodes pointing to the next nodes by means of pointers. Each node is divided into two parts: (i) First part containing the information of the element and the (ii) second part called the ‘link’ or ‘next pointer’ containing the address of the next node in the list.

In linked list the number of elements need not to be predetermined, more memory can be allocated or released during the processing as and when required.

TOPIC: Insertion of a new node at the beginning of a linked list.

The process of insertion require to allocate memory for the new node which will be added to the beginning of the linked list.

ALGORITHM:

This algorithm deals with the insertion in the beginning of the linked list.

Step 1: ptr=start;Step 2: newptr= new node;Step 3: if newptr=NULL

Print “no space available! aborting!!”Step 4: else

{ Newptr->info=ITEM.

Newptr->link=NULL.}

Step 5: if start=NULL thenStart=newptr

Step 6: else{

Save=startStart=newptrnewptr->link=save

}Step 7: exit

Page 38

Page 39: DSPM Experiments

TOPIC: Insertion of a new node at the end of a linked list.

ALGORITHM:

This algorithm deals with the insertion of a new node at the end of the linked list.

Step 1: declare pointers START, PTR, NEWPTR, REAR.Step 2: ptr=START.Step 3: NEWPTR= new nodeStep 4: if NEWPTR=NULL

Print “no space available! Aborting!!”exit

Step 5: else{ NEWPTR->LINK=NULL}

Step 6: if START=NULL then{ Start=newptr Rear=newptr}

Step 7: REAR->LINK=NEWPTRStep 8: REAR=NEWPTRStep 9: exit.

Page 39

Page 40: DSPM Experiments

/* WRITE A PROGRAM IN C++ TO IMPLEMENT LINKED LIST AND PERFORM OPERATIONS (INSERTION AT THE END, INSERTION IN BEGINING AND INSERTION IN BETWEEN IN A LINKED LIST)

DESIGNED BY- SUSHANT RANJAN

DATE- 28TH SEPTEMBER,2011 */

#include<iostream.h>

#include<conio.h>

#include<dos.h>

#include<process.h>

#include<ctype.h>

struct node {

int data;

node *next;

} *start,*ptrr,*newptr,*ptr,*save,*rear;

node * newnode(int inf)

{

ptr=new node;

ptr->data=inf;

ptr->next=NULL;

return ptr;

}

void print(node *np)

{

Page 40

Page 41: DSPM Experiments

cout<<"\n\nLINKED LIST : \n";

while(np!=NULL)

{

cout<<" "<<np->data;

np=np->next;

}

}

void insertbeg(node *np)

{

if(start==NULL)

start=rear=np;

else

{

save=start;

start=np;

np->next=save;

}

}

void insertend(node *np)

{

if(start==NULL)

start=rear=np;

else

Page 41

Page 42: DSPM Experiments

{

rear->next=np;

rear=np;

}

}

void insertbet(int pos,node *np)

{

ptrr=start;

if(start==NULL)

{

start=rear=np;

}

else if(pos==1)

insertbeg(np);

else

{

for(int i=0;i<pos-2;i++)

{

ptrr=ptrr->next;

}

np->next=ptrr->next;

ptrr->next=np;

}

}

Page 42

Page 43: DSPM Experiments

void main()

{

char ch,ch1,ch2,ch3,c=175;

int i=0,info;

start=rear=NULL;

menu:

do

{

clrscr();

cout<<"\n\n\n\t\t**********MENU**********";

cout<<"\n\n\ta)-CREATE A NEW NODE\n\tb)-PRINT THE LINKED LIST";

cout<<"\n\n\t\tenter ur choice : ";

ch=getche();

delay(500);

clrscr();

switch(ch)

{

case 'a':cout<<"\n\n\n\t\tU CHOSE INSERTING A NEW NODE IN THE LINKED LIST ";

cout<<"\n\n\n\t\t*******INSERTION MENU*******";

cout<<"\n\n\ta)-INSERTION AT THE BEGINING\n\tb)-INSERTION AT THE END";

cout<<"\n\tc)-INSERTION IN BETWEEN THE LIST\n\tr)-MAIN MENU";

cout<<"\n\n\t\tenter ur choice : ";

ch1=getche();

Page 43

Page 44: DSPM Experiments

delay(500);

clrscr();

switch(ch1)

{

case 'a':char chinb;

cout<<"\n\n\nU CHOSE INSERTION AT THE BEGINING";

do

{

i++;

cout<<"\n\ndata of "<<i<<" node : ";

cin>>info;

newptr=newnode(info);

insertbeg(newptr);

print(start);

cout<<"\n\n\tU WANNA ENTER MORE NODES(Y/N) : ";

chinb=getche();

delay(500);

}while(chinb=='Y'||chinb=='y');

break;

case 'b':char chine;

cout<<"\n\n\nU CHOSE INSERTION AT THE END";

do

{

Page 44

Page 45: DSPM Experiments

i++;

cout<<"\n\ndata of "<<i<<" node : ";

cin>>info;

newptr=newnode(info);

insertend(newptr);

print(start);

cout<<"\n\n\tU WANNA ENTER MORE NODES(Y/N) : ";

chine=getche();

delay(500);

}while(chine=='Y'||chine=='y');

break;

case 'c':cout<<"\n\n\nU CHOSE INSERTION IN BETWEEN THE LIST";

do

{

i++;

int pos;

ret:

cout<<"\n\nPOSITION WHERE NODE TO BE INSERTED : ";

cin>>pos;

if(pos>i||pos<=0)

{

cout<<"\n\nWRONG VALUE OF POS";

cout<<"\n"<<i+1<<"NODES PRESENT";

Page 45

Page 46: DSPM Experiments

goto ret;

}

cout<<"\n\ndata of "<<i<<" node : ";

cin>>info;

newptr=newnode(info);

insertbet(pos,newptr);

print(start);

cout<<"\n\n\tU WANNA ENTER MORE NODES(Y/N) : ";

chine=getche();

delay(500);

}while(chine=='Y'||chine=='y');

break;

case 'r':cout<<"\n\n\nRETURN TO MAIN MENU ";

goto menu;

}

break;

case 'b':print(start);

break;

default:for(int i=0;i<10;i++)cout<<"\n";

cout<<"\n\t\t\tThank you for using the program!\n\n";

cout<<"\t\t\t "<<c<<"---> RAUSHAN <---"<<c;

cout<<"\n\n\t\t\tPlease wait while program exits.\n\t\t\t";

Page 46

Page 47: DSPM Experiments

for(i=0;i<10;i++)

{

cout<<c<<c<<c;

delay(200);

}

exit(0);

}

cout<<"\n\n\tU WANNA CONTINUE(Y/N) : ";

ch3=getche();

delay(500);

ch3=toupper(ch3);

if(ch3!='Y')

{

clrscr();

for(int i=0;i<10;i++)cout<<"\n";

cout<<"\n\t\t\tThank you for using the program!\n\n";

cout<<"\t\t\t "<<c<<"---> RAUSHAN <---"<<c;

cout<<"\n\n\t\t\tPlease wait while program exits.\n\t\t\t";

for(i=0;i<10;i++)

{

cout<<c<<c<<c;

delay(200);

}

exit(0);

Page 47

Page 48: DSPM Experiments

}

}while(ch3=='y'||ch3=='Y');

getch();

}

Page 48

Page 49: DSPM Experiments

OUTPUT :

Page 49

Page 50: DSPM Experiments

Page 50

Page 51: DSPM Experiments

Page 51

Page 52: DSPM Experiments

Page 52

Page 53: DSPM Experiments

Page 53

Page 54: DSPM Experiments

Page 54

Page 55: DSPM Experiments

Page 55

Page 56: DSPM Experiments

/* WRITE A PROGRAM IN C++ TO CREATE A NEW NODE,INSERT THE NEW NODE AT THE END AND DELETE NODES FROM THE BEGINNING,END OR ANY POSITION IN THE LINKED LIST

DESIGNED BY- SUSHANT RANJAN.

DATE- 18TH SEPTEMBER,2011 */

#include<iostream.h>

#include<conio.h>

#include<dos.h>

#include<ctype.h>

#include<process.h>

struct node{

int data;

node *next;

} *start,*newptr,*rear,*ptr,*save;

node * newnode(int n)

{

ptr=new node;

ptr->data=n;

ptr->next=NULL;

return(ptr);

}

void insback(node* ptr)

Page 56

Page 57: DSPM Experiments

{

if (start==NULL)

start=rear=ptr;

else

rear->next=ptr;

rear=ptr;

}

void print(node * ptr)

{

cout<<"\n";

while(ptr!=NULL)

{

cout<<" "<<ptr->data;

ptr=ptr->next;

}

}

void delbeg()

{

if(start==NULL)

cout<<"\n\n\tUNDERFLOW";

else

ptr=start;

Page 57

Page 58: DSPM Experiments

start=ptr->next;

delete ptr;

}

void delend()

{

ptr=start;

if(start==NULL)

cout<<"\n\n\tUNDERFLOW";

else if(start->next==NULL)

delbeg();

else

{

while(ptr!=rear)

{

if(ptr!=rear)

save=ptr;

ptr=ptr->next;

}

}

rear=save;

rear->next=NULL;

delete ptr;

}

Page 58

Page 59: DSPM Experiments

void delbet(int pos)

{

int i;

ptr=start;

if (start==NULL)

cout<<"\n\n\tUNDERFLOW";

else if (pos==1)

delbeg();

else

{

for(i=2;i<pos;i++)

{

ptr=ptr->next;

}

ptr->next=ptr->next->next;

}

}

void main()

{

clrscr();

start=rear=NULL;

Page 59

Page 60: DSPM Experiments

int info,i=1;

char ch,ch2,ch1,ch3,chd;

clrscr();

do

{

start:

cout<<"\n\nU WANNA ENTER A NODE(y/n) : ";

ch2=getche();

ch2=tolower(ch2);

while(ch2=='y')

{

cout<<"\n\nDATA FOR "<<i++<<" RECORD : ";

cin>>info;

newptr=newnode(info);

insback(newptr);

print(start);

cout<<"\n\nWANNA ENTER MORE RECORDS (y/n) : ";

ch2=getche();

ch2=tolower(ch2);

delay(500);

}

clrscr();

Page 60

Page 61: DSPM Experiments

menu:

cout<<"\n\n\t\t\t********MENU********";

cout<<"\n\n\ta)-DELETE A NODE\n\tb)-PRINT";

cout<<"\n\n\t\tenter ur choice : ";

ch=getche();

ch=tolower(ch);

delay(500);

clrscr();

switch(ch)

{

case 'a': cout<<"\n\n\t\t********DELETION MENU********";

cout<<"\n\n\ta)-DELETE FROM BEGINING\n\tb)-DELETE AT THE END";

cout<<"\n\tc)-DELETE IN BETWEEN\n\tr)-RETURN TO MAIN MENU";

cout<<"\n\n\t\tenter ur choice : ";

chd=getche();

chd=tolower(chd);

delay(500);

clrscr();

switch(chd)

{

case 'a':char chdbeg;

cout<<"\n\n\nU CHOSE DELETION AT THE BEGINING";

do

{

Page 61

Page 62: DSPM Experiments

i--;

delbeg();

print(start);

cout<<"\n\nU WANNA DELETE MORE NODES(Y/N) : ";

chdbeg=getche();

delay(500);

}while(chdbeg=='Y'||chdbeg=='y');

break;

case 'b':char chdend;

cout<<"\n\n\nU CHOSE DELETION AT THE END";

do

{

i--;

delend();

print(start);

cout<<"\n\n\tU WANNA DELETE MORE NODES(Y/N) : ";

chdend=getche();

delay(500);

}while(chdend=='Y'||chdend=='y');

break;

case 'c':char chdbet;

cout<<"\n\n\nU CHOSE DELETION IN BETWEEN THE LIST";

Page 62

Page 63: DSPM Experiments

do

{

i--;

int pos;

ret:

cout<<"\n\nPOSITION TO BE DELETED : ";

cin>>pos;

if(i<=0)

{

cout<<"\n\nno nodes available for deletion";

goto start;

}

else if(pos>i||pos<=0)

{

cout<<"\n\nWRONG VALUE OF POS";

cout<<"\n"<<i<<"NODES PRESENT";

goto ret;

}

delbet(pos);

print(start);

cout<<"\n\n\tU WANNA DELETE MORE NODES(Y/N) : ";

chdbet=getche();

delay(500);

}while(chdbet=='Y'||chdbet=='y');

Page 63

Page 64: DSPM Experiments

break;

case 'r':cout<<"\n\n\nRETURN TO MAIN MENU ";

goto menu;

}

break;

case 'b': cout<<"\n\nU CHOSE PRINTING";

print(start);

break;

default: char c=175;

for(int i=0;i<10;i++)cout<<"\n";

cout<<"\n\t\t\tThank you for using the program!\n\n";

cout<<"\t\t\t "<<c<<"---> RAUSHAN <---"<<c;

cout<<"\n\n\t\t\tPlease wait while program exits.\n\t\t\t";

for(i=0;i<10;i++)

{

cout<<c<<c<<c;

delay(200);

}

exit(0);

Page 64

Page 65: DSPM Experiments

}

cout<<"\n\ndo u wanna continue (y/n) : ";

ch3=getche();

}while(ch3=='y'||ch3=='Y');

cout<<"\n\nLINKED LIST IS : \n";

print(start);

getch();

}

Page 65

Page 66: DSPM Experiments

OUTPUT:

Page 66

Page 67: DSPM Experiments

Page 67

Page 68: DSPM Experiments

Page 68

Page 69: DSPM Experiments

Page 69

Page 70: DSPM Experiments

Page 70

Page 71: DSPM Experiments

Page 71

Page 72: DSPM Experiments

PROGRAM-4: SEARCHING IN A LINKED LIST.

Introduction to Linked Lists

A linked list is a linear collection of data elements, called nodes pointing to the next nodes by means of pointers. Each node is divided into two parts: (i) First part containing the information of the element and the (ii) second part called the ‘link’ or ‘next pointer’ containing the address of the next node in the list.

In linked list the number of elements need not to be predetermined, more memory can be allocated or released during the processing as and when required.

TOPIC: Insertion of a new node at the end of a linked list.

ALGORITHM:

This algorithm deals with the insertion of a new node at the end of the linked list.

Step 1: declare pointers START, PTR, NEWPTR, REAR.Step 2: ptr=START.Step 3: NEWPTR= new nodeStep 4: if NEWPTR=NULL

Print “no space available! Aborting!!”exit

Step 5: else{ NEWPTR->LINK=NULL}

Step 6: if START=NULL then{ Start=newptr Rear=newptr}

Step 7: REAR->LINK=NEWPTRStep 8: REAR=NEWPTRStep 9: exit.

Page 72

Page 73: DSPM Experiments

TOPIC: Searching a node in a linked list by comparing data elements of each node with the given item.

ALGORITHM: This algorithm finds the location LOC of the nodes N which contains the ITEM. If ITEM doesn’t appear in the list, then procedure sets LOC=NULL.

Step 1: If START=NULL, then:Set LOC=NULL and return;

Step 2: If INFO->START=ITEM, then:Set LOC=START and return;

Step 3: Set PTR=START and PTR=LINK[PTR].Step 4: Repeat steps 5 and 6 while PTR!=NULL.Step 5: If INFO[PTR]=ITEM, then:

Set LOC=PTR and return.Step 6: PTR=LINK[PTR].Step 7: Set LOC=NULL [search unsuccessful].Step 8: Exit.

Page 73

Page 74: DSPM Experiments

/* WRITE A PROGRAM IN C++ TO CREATE A NEW NODE,INSERT THE NEW NODE AT THE END AND SEARCH THE NODE.

DESIGNED BY- SUSHANT RANJAN

DATE- 18TH SEPTEMBER,2011 */

#include<iostream.h>

#include<conio.h>

#include<ctype.h>

#include<process.h>

#include<dos.h>

struct node{

int data;

node *next;

} *start,*newptr,*ptr,*rear;

int flag=0,n=0,j=0,c=0;

node * newnode(int n)

{

ptr=new node;

ptr->data=n;

ptr->next=NULL;

return(ptr);

}

void insback(node* ptr)

Page 74

Page 75: DSPM Experiments

{

if (start==NULL)

start=rear=ptr;

else

rear->next=ptr;

rear=ptr;

}

void print(node * ptr)

{

cout<<"\n";

while(ptr!=NULL)

{

cout<<" "<<ptr->data;

ptr=ptr->next;

}

}

void search(int num)

{

int *arr,i;

arr=new int[c];

flag=0; j=0;

int ncount=0;

Page 75

Page 76: DSPM Experiments

ptr=start;

if (start==NULL)

cout<<"\nlist is empty";

else

{

while(ptr!=NULL)

{

ncount++;

if(ptr->data==num)

{

flag=1;

j++;

arr[j]=ncount;

}

ptr=ptr->next;

}

}

if(flag==1)

{

cout<<"\n\n\tSEARCH SUCCESSFUL";

cout<<"\npresent at position : ";

for(i=1;i<=j;i++)

cout<<arr[i]<<" ";

}

Page 76

Page 77: DSPM Experiments

else

cout<<"\n\nsearch unsuccessful";

}

void main()

{

clrscr();

start=rear=NULL;

int arr[10],num,info,i=0;

char ch,ch2;

clrscr();

do

{

cout<<"\n\nU WANNA ENTER A NODE(y/n) : ";

ch=getche();

ch=tolower(ch);

while(ch=='y')

{

c++;

cout<<"\n\nDATA FOR "<<++i<<" RECORD : ";

cin>>info;

newptr=newnode(info);

insback(newptr);

print(start);

cout<<"\n\nWANNA ENTER MORE RECORDS (y/n) : ";

Page 77

Page 78: DSPM Experiments

ch=getche();

ch=tolower(ch);

delay(500);

}

clrscr();

cout<<"\n\nNO. TO BE SEARCHED : ";

cin>>num;

search(num);

cout<<"\n\ndo u wanna continue (y/n) : ";

ch2=getche();

}while(ch2=='y'||ch2=='Y');

cout<<"\n\nLINKED LIST IS : \n";

print(start);

getch();

}

Page 78

Page 79: DSPM Experiments

OUTPUT

Page 79

Page 80: DSPM Experiments

Page 80