Você está na página 1de 82

DATA STRUCTURE

AND
PROGRAMMING
METHODOLOGY

BABA HIRA SINGH BHATTAL INSTITUTE OF


ENGINEERING AND TECHNOLOGY, LEHRAGAGA,
SANGRUR

Submitted By:

Submitted To:

Page 1

SUSHANT RANJAN

MRS.KIRAN ARORA

CSE-3rd semester
100070303895

INDEX
Sr.No.

PROGRAM

DATE

PAGE

CREATING AN ARRAY

25/09/2011

4-5

TRAVERSING AN ARRAY

25/09/2011

6-7

INSERTIONTO AN ARRAY

27/09/2011

7-11

DELETING AN ARRAY

28/09/2011

12-15

LINEAR SEARCH IN AN ARRAY

02/10/2011

16-20

BINARY SEARCH IN AN ARRAY

05/10/2011

21-22

BUBBLE SORT

04/10/2011

23-26

MULTIPLICATIONOF
MATRICES

TWO 28/09/2011

27-37

LINKED LIST(INSERTION)

28/09/2011

38-55

28/09/2011

56-71

29/09/2011

72-80

*AT THE BEGINNING


*AT THE END
*AT ANY POSITION
10

LINKED LIST(DELETION)
* AT THE BEGINNING
*AT THE END
*AT ANY POSITION

11

SEARCHING IN A LINKED LIST

Page 2

SIGNATURE

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

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

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];
}
Page 6

getch();
}

Page 7

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}
Thats 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
Page 8

inserted.
STEP 1: set i=max.
STEP 2: repeat steps 3 & 4 while i>=pos.
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

/*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++)
{
Page
10

cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";
}
getch();
}

Page
11

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.
Thats exactly how deletion is done!

ALGORITHM:

Algorithm for Deletion of an element in an array:


Page
12

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.

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

/*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";
Page
14

for(i=1;i<=n;i++)
{
cout<<"ELEMENT["<<i<<"]="<<a[i]<<"\n";
}
getch();
}

Page
15

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;


Page
16

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.
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).


Page
17

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 = U
2. REPEAT STEPS 3 THROUGH 6 TILL beg<=last
3. mid = ( (beg+last)/2)
4. IF AR[mid] = ITEM THEN
5.
ITEM IS AT POSITION mid
6.
BREAK THE LOOP
7. IF AR[mid] < ITEM THEN
8.
beg = mid+1
9. IF AR[mid] > ITEM
10.
last = mid-1
11. END OF LOOP
12. IF AR[mid] = ITEM THEN
13. SEARCH IS UNSUCCESSFULL

Page
18

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;
Page
19

intloc=0;
while(a[loc]!=num)
{
loc++;
}
if(loc==(n+1))
{
cout<<"\n"<<"SEARCH UNSUCCESSFUL";
}
else
cout<<"\n"<<"LOCATION="<<(loc);
getch();
}

Page
20

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=";
Page
21

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

Page
22

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}
Page
23

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.
Thats exactly how sorting is done!

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.
2.
3.
4.
5.
6.
7.

FOR I = L TO U
FOR J = L TO (U-1)
IF AR[J] > AR[JA1] THEN
temp = AR[J]
AR[J] = AR[J+1]
END OF INNER LOOP
END OF OUTER LOOP

Page
24

/*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++)
{
Page
25

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";
}
getch();
}

Page
26

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.

Page
27

Array name (grade)


2000 2001

Address
2007

B)

2002

2003

2004 2005

2006

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

Row
0
Row

A[1][1]

/* WRITE A PROGRAM TO FIND THE MULTIPLICATION OF TWO


ARRAYS

DESIGNED BY - SUSHANT RANJAN


DATE-

28TH SEPTEMBER,2011

#include<iostream.h>
Page
28

*/

#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++)
{
delay(1000);
cout<<".";
}
exit(0);

void main()
{
clrscr();
int n,i,j;
Page
29

char ch,ch2;
do
{
clrscr();
menu:
cout<<"\n\n\n\t\t\t*********MENU*********";
cout<<"\n\n\n\n\tFIND THE MULTIPLICATION
ARRAY\n\t\tb)2-D ARRAY\n\t\tq)QUIT";

OF

\n\n\t\ta)LINEAR

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


ch=getche();
ch=tolower(ch);
delay(500);
clrscr();

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 ";
Page
30

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++)
{
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++)
Page
31

{
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 ";
start:
cout<<"\n\nNO. OF ROWS OF 1st ARRAY : ";
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;

Page
32

//no.of columns of 2nd

//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 : ";
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];
}
Page
33

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";
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";
Page
34

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();

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;

Page
35

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


ch2=getche();
delay(500);
}while(ch2=='y'||ch2=='Y');
getch();
}

OUTPUT :

Page
36

Page
37

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.
Page
38

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 then
Start=newptr
Step 6: else
{
Save=start
Start=newptr
newptr->link=save
}
Step 7: exit
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 node
Step 4: if NEWPTR=NULL
Print no space available! Aborting!!
exit
Step 5: else
{
NEWPTR->LINK=NULL
}
Step 6: if START=NULL then
Page
39

{
Start=newptr
Rear=newptr
}
Step 7: REAR->LINK=NEWPTR
Step 8: REAR=NEWPTR
Step 9: exit.

/* 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 {
Page
40

*/

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)
{
cout<<"\n\nLINKED LIST : \n";
while(np!=NULL)
{
cout<<" "<<np->data;
np=np->next;
}
}
void insertbeg(node *np)
{
if(start==NULL)
start=rear=np;

Page
41

else
{
save=start;
start=np;
np->next=save;
}
}
void insertend(node *np)
{
if(start==NULL)
start=rear=np;
else
{
rear->next=np;
rear=np;
}
}
void insertbet(int pos,node *np)
{
ptrr=start;
if(start==NULL)
{
start=rear=np;
}
Page
42

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;
}
}
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 : ";
Page
43

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();
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);
Page
44

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
{
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;

Page
45

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";
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;
Page
46

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";
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')
Page
47

{
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);
}

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

Page
48

OUTPUT :

Page
49

Page
50

Page
51

Page
52

Page
53

Page
54

Page
55

Page
56

Page
57

/*

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
58

*/

{
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
59

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
60

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
61

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
62

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
63

{
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;


Page
64

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


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);
Page
65

}while(chdbet=='Y'||chdbet=='y');
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);
}
Page
66

exit(0);

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
67

OUTPUT:

Page
68

Page
69

Page
70

Page
71

Page
72

Page
73

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.
Page
74

Step 2: ptr=START.
Step 3: NEWPTR= new node
Step 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=NEWPTR
Step 8: REAR=NEWPTR
Step 9: exit.

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 doesnt 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
75

/*
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{
Page
76

*/

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)
{
if (start==NULL)
start=rear=ptr;
else
rear->next=ptr;
rear=ptr;
}

void print(node * ptr)


{
cout<<"\n";
Page
77

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;
ptr=start;
if (start==NULL)
cout<<"\nlist is empty";
else
{
while(ptr!=NULL)
{
ncount++;
if(ptr->data==num)
{
flag=1;
Page
78

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]<<" ";
}
else
cout<<"\n\nsearch unsuccessful";
}
void main()
{
clrscr();
start=rear=NULL;
int arr[10],num,info,i=0;
char ch,ch2;
clrscr();
do
Page
79

{
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) : ";
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) : ";


Page
80

ch2=getche();
}while(ch2=='y'||ch2=='Y');
cout<<"\n\nLINKED LIST IS : \n";
print(start);
getch();
}

OUTPUT

Page
81

Page
82

Você também pode gostar