Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
57 views

Data Structure

The program first takes input of number of elements and inserts those elements into an array. It then takes input for location and item to be inserted. It uses a while loop to shift elements from the given location to the end. The item is inserted at the given location and size of array is incremented. Finally, resultant array is printed.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views

Data Structure

The program first takes input of number of elements and inserts those elements into an array. It then takes input for location and item to be inserted. It uses a while loop to shift elements from the given location to the end. The item is inserted at the given location and size of array is incremented. Finally, resultant array is printed.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Algorithm For insert an Element In Queue

Step 1: If (Rear = n-1)

(a) Write “Queue Full”


(b) goto step 3.

Step 2: If Front = Rear =-1 then

(a) Set Front 0


(b) Set Rear 0
(c) Set (Q)Rear val

Else

(a) Set Rear Rear +1


(b) Set Q(Rear) val

Step 3: Stop.
Algorithm for Inserting A Node at Desire Location

Step 1: Set PTR FIRST

Step 2: Set Z AVAIL

Step 3: Set INFO (Z) DATA

Step 4: If (LOC=1)

(i) Set NEXT(Z) FIRST


(ii) Set FIRST Z
(iii) Go to Step 10

Step 5: For I=2 to (LOC-1) repeat step 6,7

Step 6: Set PTR NEXT (PTR)

Step 7: If (PTR=NULL)

(i) Write there are less number of nodes so


Insertion at desired location is not possible.
(ii) Go to Step 10

Step 8: Set NEXT (Z) NEXT (PTR)

Step 9: Set NEXT (PTR) Z

Step 10: Stop.


Algorithm For Deleting A Node In Link list

Step 1: If (FIRST=NULL)

Write ‘List empty’ Go to Step 8

Step 2: If (INFO (FIRST) = ‘VAL’) then

(i) Set PTR FIRST.


(ii) Set FIRST NEXT (FIRST)
(iii) Delete PTR.
(iv) Go to step 8.

Else

(i) Set BACK FIRST


(ii) Set PTR NEXT (FIRST)

Step 3: While (PTR not equal to NULL) repeat step 4 to 7

Step 4: If (INFO (PTR) = ‘VAL’) then

(i) Set NEXT (BACK) NEXT (PTR)


(ii) Delete PTR.
(iii) Go to Step 8.

Step 5: Set BACK PTR

Step 6: Set PTR NEXT (PTR)

Step 7: if (PTR = NULL)

Write ‘Node not Found’

Step8: Stop.
Program For Fibonacci Serise

#include "stdio.h"

#include "conio.h"

void main()

int a,b,c,i,n;

clrscr();

a=0;

b=1;

printf("\n enter n for how many times generate series");

scanf("%d",&n);

printf("\nFIBONACCI SERIES\n");

printf(" %d %d",a,b);

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

c=a+b;

a=b;

b=c;

printf(" %d",c);

getch();

Output:

enter n for how many times generate series:5..FIBONACCI SERIES:1 1 2 3 5


Program For Copy String

#include<stdio.h>

void stringCopy(char[],char[]);

int main(){

char str1[100],str2[100];

printf("Enter any string: ");

scanf("%s",str1);

stringCopy(str1,str2);

printf("After copying: %s",str2);

return 0;

void stringCopy(char str1[],char str2[]){

int i=0;

while(str1[i]!='5'){

str2[i] = str1[i];

i++;

str2[i]='5';

Output:

Enter any string:abc

After copying:abc
Program For Push and Pop Operation

# include<stdio.h>

# include<malloc.h>

struct node

int info;

struct node *link;

} *top=NULL;

main()

int choice;

while(1)

printf("1.Push\n");

printf("2.Pop\n");

printf("3.Display\n");

printf("4.Quit\n");

printf("Enter your choice : ") ;

scanf("%d", &choice);

switch(choice)

case 1:

push();

break;

case 2:

pop();
break;

case 3:

display();

break;

case 4:

exit(1);

default :

printf("Wrong choice\n");

push()

struct node *temp;

int pushed_item;

temp = (struct node *)malloc(sizeof(struct node));

printf("Input the new value to be pushed on the stack : ");

scanf("%d",&pushed_item);

temp->info=pushed_item;

temp->link=top;

top=temp;

}/*End of push()*/

pop()

struct node *temp;

if(top == NULL)
printf("Stack is empty\n");

else

temp=top;

printf("Popped item is %d\n",temp->info);

top=top->link;

free(temp);

}/*End of pop()*/

display()

struct node *ptr;

ptr=top;

if(top==NULL)

printf("Stack is empty\n");

else

printf("Stack elements :\n");

while(ptr!= NULL)

printf("%d\n",ptr->info);

ptr = ptr->link;

}
Compare String

#include<stdio.h>

int stringCompare(char[],char[]);

int main(){

char str1[100],str2[100];

int compare;

printf("Enter first string: ");

scanf("%s",str1);

printf("Enter second string: ");

scanf("%s",str2);

compare = stringCompare(str1,str2);

if(compare == 1)

printf("Both strings are equal.");

else

printf("Both strings are not equal");

return 0;

int stringCompare(char str1[],char str2[]){

int i=0,flag=0;

while(str1[i]!='9' && str2[i]!='9'){

if(str1[i]!=str2[i]){

flag=1;

break;

i++;

}
if (flag==0 && str1[i]=='10' && str2[i]=='10')

return 1;

else

return 0;

Output:

Enter first string::abc

Enter second string:abc

Both strings are equal.


Liner Search

#include<stdio.h>

main()

int array[100], search, c, number;

printf("Enter the number of elements in array\n");

scanf("%d",&number);

printf("Enter %d numbers \n", number);

for ( c = 0 ; c < number ; c++ )

scanf("%d",&array[c]);

printf("Enter the number to search\n");

scanf("%d",&search);

for ( c = 0 ; c < number ; c++ )

if ( array[c] == search ) /* if required element found */

printf("%d is present at location %d.\n", search, c+1);

break;

if ( c == number )

printf("%d is not present in array.\n", search);

return 0;

}
Bubble sort

#include <stdio.h>

void bubble_sort(long [], long);

int main()

long array[100], n, c, d, swap;

printf("Enter number of elements:");

scanf("%ld", &n);

printf("Enter %ld longegers\n", n);

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

scanf("%ld", &array[c]);

bubble_sort(array, n);

printf("Sorted list in ascending order:n");

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

printf("%ld\n", array[c]);

return 0;

void bubble_sort(long list[], long n)

long c, d, t;

for (c = 0 ; c < ( n - 1 ); c++)

for (d = 0 ; d < n - c - 1; d++)

if (list[d] > list[d+1])


{

t = list[d];

list[d] = list[d+1];

list[d+1]= t;

}
Program:- To inserted an element in an array

#include<stdio.h>

#include<conio.h>

void main()

int a[100],k,item,n,j,I;

clrscr();

printf(“Enter how many elements \n”);

scanf(“%d,&n”);

printf(“Enter the %d elements\n”,n);

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

scanf(“%d,&a[i]”);

printf(“Enter the location\n”);

scanf(“%d,&k”);

printf(“Enter the item to be inserted at a[%d]\n”,k);

scanf(“%d,&item”);

j=n-1;

while(j>=k)

a[j+1]=a[j];

j--;

a[k]=item;

n++;

printf(“Resultant array is…….\n”);

for(i=0;i<n;i++)
printf(“%4d,a[i]”);

getch();

Output of the program is:

Enter how many elements

Enter the % Elements

11 22 33 44 55

Enter the location

Enter the item to be inserted at a[3]

66

Resultant array is…..

11 22 33 66 44 55

You might also like