+ All Categories
Home > Documents > Data structure lab file

Data structure lab file

Date post: 03-Jun-2018
Category:
Upload: deepak-mehta
View: 229 times
Download: 0 times
Share this document with a friend
23
INDEX S.NO. Program Name Remarks 1. Program for insert and delete the Element.  2. Program for insertion sort in array. 3. Program for Bubble sort in array. 4. Program for selection sort in array. 5. Program to print the element of linked list in reverse order. . Program f or s tack i mplementation t hrough a rray. !. Program for circular "ueue implementation. #. Program for implement linked list insertion and deletion. $. Program to traverse a binary t ree i norder% preorder a nd  postorder.
Transcript

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 1/23

INDEX

S.NO. Program Name Remarks

1. Program for insert and delete the Element. 

2. Program for insertion sort in array.

3. Program for Bubble sort in array.

4. Program for selection sort in array.

5. Program to print the element of linked list in reverse order.

. Program for stack implementation through array.

!. Program for circular "ueue implementation.

#. Program for implement linked list insertion and deletion.

$. Program to traverse a binary tree inorder% preorder and

 postorder.

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 2/23

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 3/23

=>:P>:

Enter the maimum number for insert

3Enter the array number

1

23

Enter the position of element

2

Enter the number u 7ant to insert

:he list after insert element

1

2

3Enter the maimum number for delete

3

Enter the array element1

2

3

Enter the position of the number you 7ant to delete2

Element after <elete

12

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 4/23

&& ?@P for insertion sort in array.

'include(stdio.h)

'include(conio.h)void main*+

,

int a-45%n%i%pos%c/ printf*0Enter the number of element of array0+/

scanf*0d0%n+/

 printf*0Enter the array element0+/for*i1/i(n/i66+

scanf*0d0%a-i+/

for*i2/i(n/i66+

,ca-i/

 posi81/

7hile*c(a-pos pos)1+

,a-pos61a-pos/

 pospos81/9

a-pos61c/

9 printf*0:he sorted array are0+/

for*i1/i(n/i66+

 printf*0;nd0%a-i+/

getch*+/9

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 5/23

=>:P>:

Enter the number of element of array

$Enter the array element

12 2 5 1 # 3 4 11

:he sorted array are1

2

3

45

#

1112

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 6/23

&&?@P for Bubble sort in array.

'include(stdio.h)

'include(conio.h)void bubble*int a-% int n+/

int i%A%n%temp%a-25%count/

void main*+,

 printf*0;n;n Enter ho7 many number you 7ant to sort;n;n0+/

scanf*0d0%n+/

 printf*0;n Enter the number;n0+/for*i/i(n/i66+

,

scanf*0d0%a-i+/

9 bubble*a%n+/

 printf*0;n;nCinal sorted list is;n0+/for*i/i(n/i66+

 printf*04d0%a-i+/

 printf*0;nDompleity is d0%count+/getch*+/

9

void bubble*int a-%int n+

,int k/

for*i/i(n8i/i66+

, printf*0;n;npass8)d0%i61+/

for*Ai61/A(n81/A66+

,if*a-i)a-A+

,

tempa-i/

a-ia-A/a-Atemp/

9

count66/9

for*k/k(n/k66+

 printf*0;t4d0%a-k+/&&printf*0;n0+/

9}

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 7/23

=>:P>:

 Enter ho7 many number you 7ant to sort

4

 Enter the number 

5 4 2 !

 pass8)1 2 5 4 !

 pass8)2 2 4 5 !

Cinal sorted list is

  2 4 5 !

Dompleity is 5

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 8/23

&&?@P for selection sort in array.

'include(stdio.h)

'include(conio.h)

void selectionsort*int a-% int n+/int i%A%temp%a-25%n%count/

void main*+

, printf*0;n;nEnter ho7 many number you 7ant to sort;n;n0+/

scanf*0d0%n+/

 printf*0;nEnter the number;n0+/

for*i/i(n/i66+,

scanf*0d0%a-i+/

9

selectionsort*a%n+/ printf*0;n;nCinal sorted list is0+/

for*i/i(n/i66+ printf*0d0%a-i+/

 printf*0;n;nDompleity is d0%count+/

getch*+/9

void selectionsort*int a-% int n+

,

int temp% small%loc%i%A%k/for*i1/i(n81/i66+

,

 printf*0pass8)d;t0%i+/smalla-i81/

loci81/

for*Ai/A(n81/A66+,

if*a-A(small+

,

smalla-A/locA/

9

count66/9

if*loc*i81++

,tempa-i81/

a-i81a-loc/

a-loctemp/

9

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 9/23

for*k/k(n/k66+

 printf*0d0%a-k+/

 printf*0;n0+/9

9

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 10/23

=>:P>:

Enter ho7 many number you 7ant to sort

Enter the number 

4

3

5

5

2

 pass8)1 2 3 5 5 4 pass8)2 2 3 5 5 4

 pass8)3 2 3 4 5 5 pass8)4 2 3 4 5 5

 pass8)5 2 3 4 5 5

Cinal sorted list is 2 3 4 5 5

Dompleity is 15

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 11/23

&&?@P to print the element of linked list in reverse order.'include(stdio.h)

'include(conio.h)

typedef struct node ,int item/

struct node Fnet/

9 node/node Fhead% Ftail% Ftemp/

void add*+/

void reverse*+/

void display*+/void main*+

,

int n%i/

 printf*0;t;tEnter the number of element;t0+/scanf*0;t;td0%n+/

headG>HH/for*i/i(n/i66+

,

add*+/9printf*0;n;t;t=riginal order of linked list;n0+/

display*+/

reverse*+/

 printf*0;n;t;tIeverse of linked list;n0+/display*+/

9

void reverse*+,

node Fp%F"%Fr/

if*headG>HH+,

return/

9

 phead/"p8)net/ p8)netG>HH/

7hile*"G>HH+

,r"8)net/

"8)netp/

 p"/"r/9

headp/

9

void add*+

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 12/23

,

node Ftemp/

temp*nodeF+malloc*siJeof*node++/ printf*0;n;tEnter the data;t0+/

scanf*0d0%temp8)item+/

temp8)nethead/headtemp/

9

void display*+,

node Fcurhead/

 printf*0;n;tKead8)0+/

7hile*curG>HH+,

 printf*0;td0%cur8)item+/

curcur8)net/

9 printf*08)null;n;n0+/

9

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 13/23

=>:P>:

  Enter the number of element 4

  Enter the data 2

  Enter the data 4

  Enter the data

  Enter the data #

 =riginal order of linked list

Kead8) # 4 28)G>HH

  Ieverse of linked list

Kead8) 2 4 #8)G>HH

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 14/23

&&?@P for stack implementation thro7 array.

'include(stdio.h)'include(conio.h)

'include(ctype.h)

'include(stdlib.h)'define L@MNOE 3

int stack-L@MNOE/

int top/void main*+

,

void push*int+/

int pop*+/int choice1%i%num%/

&&clrcsr*+/

7hile*choice1+

, printf*0;n;t;tL@OG LEG>;n;t1.P>NK opration on

stack;n;t2.P=P operation on stack;n;t3.Eit0+/ printf*0;n;tEnter your choice0+/

scanf*0d0%choice+/

s7itch*choice+,

case 1

 printf*0;nEnter the element0+/

scanf*0d0%num+/ push*num+/

 break/

case 2ipop*+/

 printf*0;nQalue returned from pop function isd0%i+/

 break/case 3

eit*+/

default printf*0;n;tOnvalid choice.0+/

9 printf*0;nPress 1 to do further operation or 3 to eit0+/

scanf*0d0%choice+/

99

void push*int y+

,if*top)L@MNOE+

,

 printf*0;nN:@DR C>HH0+/

return/

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 15/23

9

else

,top66/

stack-topy/

 printf*0;n:he top of stack isd0%top+/ printf*0;n:he value of stack top isd0%stack-top+/

9

9int pop*+

,

int a/

if*top(+,

 printf*0;nN:@DR ELP:S0+/

return /

9else

,astack-top/

top88/

 printf*0;n:he top of stack isd0%top+/9

return*a+/

9

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 16/23

=>:P>:

  L@OG LEG>

  1.P>NK opration on stack   2.P=P operation on stack 

  3.Eit

  Enter your choice1

Enter the element2

:he top of stack is1:he value of stack top is2

Press 1 to do further operation or 3 to eit 1

  L@OG LEG>  1.P>NK opration on stack 

  2.P=P operation on stack   3.Eit

  Enter your choice2

:he top of stack is

Qalue returned from pop function is2

Press 1 to do further operation or 3 to eit

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 17/23

&&?@P for circular "ueue implementation.

'include(stdio.h)'include(conio.h)

'define L@MTNOE 2

int "ueue-L@MTNOE%f81%r81/main*+

,

int ch%/ printf*0;nU>E>E =PEI@:O=GN0+/

 printf*0;n 1. insert operation0+/

 printf*0;n 2. delete operation0+/

 printf*0;n 3. eit0+/7hile*1+

,

 printf*0;nEnter your choice0+/

scanf*0d0%ch+/s7itch*ch+

,case 1

 printf*0;nEnter the number to be inserted0+/

scanf*0d0%+/insert*+/

 break/

case 2

delete*+/if*1+

 printf*0;n:he number delete isd0%+/

else printf*0;n:he "ueue is empty0+/

 break/

case 3eit*+/

default

 printf*0;nEnter correct choice0+/

 break/9

9

9insert*int +

,

if*f r*L@MTNOE81+ VV *fr61++,

 printf*0;noverflo70+/

return*81+/

9

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 18/23

if*f81+

,

fr/9

else

,if*rL@MTNOE81+

r/

elserr61/

9

"ueue-r/

return/9

int delete*+

,

int /if*f81+

, printf*0;nunderflo70+/

return/

9"ueue-f/

if*fr+

fr81/

else,

if*fL@MTNOE81+

f/else

ff61/

9return*+/

9

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 19/23

=>:P>:

U>E>E =PEI@:O=GN

 1. insert operation

 2. delete operation 3. eit

Enter your choice1

Enter the number to be inserted1

Enter your choice1

Enter the number to be inserted1

Enter your choice2

:he number delete is1

Enter your choice2

:he number delete is1

Enter your choice2

underflo7

:he "ueue is empty

Enter your choice3

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 20/23

&& for implement linked list insertion and deletion.

'include(stdio.h)

'include(conio.h)struct linkedTlist

,

int info/struct linkedTlist Fnet/

9/

void insertTatTbegin*int+/void insertTatTend*int+/

void insertTatTmiddle*int%int+/

void del*int+/

typedef struct linkedTlist node/node FheadG>HH/

void main*+

,

int i%A%n%n/clrscr*+/

7hile*1+,

 printf*01. Onsertion;n0+/

 printf*02. <eletion;n0+/ printf*03. Eit;n0+/

 printf*0;nEnter your choice0+/

scanf*0d0%i+/

s7itch*i+,

case 1

 printf*01. :o insert at beginning;n0+/ printf*02. :o insert at End;n0+/

 printf*03. :o insert at Liddle;n0+/

 printf*0Enter your choice;n0+/scanf*0d0%A+/

if*A(1 VV A)3+

,

 printf*0Please re8try;n0+/ break/

9

 printf*0Enter the number to be insert;n0+/scanf*0d0%n+/

s7itch*A+

,case 1

insertTatTbegin*n+/

 break/

case 2

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 21/23

insertTatTend*n+/

 break/

case 3 printf*0Enter the number after 7hich the number is to be inserted;n0+/

scanf*0d0%n+/

insertTatTmiddle*n%n+/ break/

9

 break/case 2

 printf*0Enter the number to be delete;n0+/

scanf*0d0%n+/

del*n+/ break/

case 3

eit*+/

 break/default

 printf*0Please re8try;n0+/ break/

9

getch*+/9

9

void insertTatTbegin*int n+

,node Fp/

 p*nodeF+malloc*siJeof*node++/

 p8)infon/ p8)nethead/

headp/

9void insertTatTend*int n+

,

node Fp%F"/

"head/ p*nodeF+malloc*siJeof*node++/

 p8)infon/

 p8)netG>HH/7hile*"8)netG>HH+

""8)net/

"8)netp/9

void insertTatTmiddle*int n%int n+

,

node Fp%F"/

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 22/23

 phead/

"*nodeF+malloc*siJeof*node++/

"8)infon/7hile*p8)infon+

 pp8)net/

"8)netp8)net/ p8)net"/

9

void del*int n+,

node Fp%F"/

"head/

if*headG>HH+return/

if*"8)infon+

,

headhead8)net/free*"+/

return/9

7hile*"8)infon+

, p"/

"p8)net/

9

 p8)net"8)net/free*"+/

9

8/13/2019 Data structure lab file

http://slidepdf.com/reader/full/data-structure-lab-file 23/23


Recommended