Upload
chennaiahnaik
View
217
Download
0
Embed Size (px)
Citation preview
7/29/2019 Data Structure Management
1/47
DATA STRUCTURE
MANAGEMENT
TUTORIAL
7/29/2019 Data Structure Management
2/47
// Develop a program to insert, delete, edit element in array
#include
#include
#include
main()
{
int array[15];
int no_el,i,choice,pos,key,new_el;
clrscr();
printf("Enter the no of element :");
scanf("%d",&no_el);
for(i=0;i
7/29/2019 Data Structure Management
3/47
printf("Enter the position at which you want to
insert : ");
scanf("%d",&pos);
printf("Enter the new element : ");
scanf("%d",&new_el);
pos--;
for(i=no_el-1;i>=pos;i--)
array[i+1]=array[i];
array[pos]=new_el;
no_el++;
break;
case 2:
printf("Enter the value to be search : ");
scanf("%d",&key);
for(pos=0;pos
7/29/2019 Data Structure Management
4/47
case 3:
printf("Enter the position to be edit : ");
scanf("%d",&pos);
printf("Enter the new value for old position : ");
scanf("%d",&array[pos-1]);
break;
case 4:
printf("\n");
for(i=0;i
7/29/2019 Data Structure Management
5/47
char *ptr3 = "Rajnik";
char* arr[3];
clrscr();
arr[0] = ptr1;
arr[1] = ptr2;
arr[2] = ptr3;
printf("\n [%s]\n", arr[0]);
printf("\n [%s]\n", arr[1]);
printf("\n [%s]\n", arr[2]);
getch();
return 0;
}
// Write simple programs using array of pointer
#include
#include
main() {
clrscr();
int *array[3];
int x = 10, y = 20, z = 30;
7/29/2019 Data Structure Management
6/47
int i;
array[0] = &x;
array[1] = &y;
array[2] = &z;
for (i=0; i< 3; i++) {
printf("The value of %d= %d ,address is %u\t \n", i,
*(array[i]),
array[i]);
}
getch();
return 0;
}
#include
#include
#include
#define MAX 50
int size;
/* Defining the stack structure */
struct stack
{
int AR[MAX];
int top;
};
/* Initializing the stack(i.e., top=-1) */
void init_stk(struct stack *st)
7/29/2019 Data Structure Management
7/47
{
st->top=-1;
}
/* Entering the elements into stack */
void push (struct stack *st,int num)
{
if(st->top == size-1)
{
printf("\nStack overflow(i.e., stack full).");
return;
}
st->top++;
st->AR[st->top] = num;
}
//Deleting an element from the stack.
int pop(struct stack *st)
{
int num;
if(st->top == -1)
{
printf("\nStack underflow(i.e., stack empty).");
return NULL;
}
7/29/2019 Data Structure Management
8/47
num=st->AR[st->top];
st->top--;
return num;
}
void display(struct stack *st)
{
int i;
for(i=st->top;i>=0;i--)
printf("\n%d",st->AR[i]);
}
void main()
{
int ele,opt,val;
struct stack ptr;
clrscr();
init_stk(&ptr);
printf("\nEnter Stack Size :");
scanf("%d",&size);
while(1)
{
printf("\n\n\tSTACK PRIMITIVE OPERATIONS");
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.DISPLAY");
printf("\n4.QUIT");
printf("\n");
printf("\nEnter your option : ");
7/29/2019 Data Structure Management
9/47
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter the element into stack:");
scanf("%d",&val);
push(&ptr,val);
break;
case 2:
ele=pop(&ptr);
printf("\nThe element popped from stack is :
%d",ele);
break;
case 3:
printf("\nThe current stack elements are:");
display(&ptr);
break;
case 4:
getch();
exit(0);
default:
printf("\nEnter correct option!Try again.");
}
}
}
/* Develop an algorithm for insert and delete operations of
queue and implement
using array and pointer data structures. */
7/29/2019 Data Structure Management
10/47
#include
#include
#include
#define size 10
#define true 1
#define false 0
struct q_arr
{
int f,r;
int num;
int a[size];
};
void init(struct q_arr* queue);
int e_que(struct q_arr* queue);
int f_que(struct q_arr* queue);
int add_ele(struct q_arr* queue,int);
int rem_ele(struct q_arr* queue);
void display_ele(struct q_arr* queue);
/*main function*/
void main()
{
int ele,k;
int ch;
7/29/2019 Data Structure Management
11/47
struct q_arr *queue = (struct q_arr*)malloc(sizeof(struct
q_arr));
init(queue);
while(1)
{
clrscr();
printf("\n\n****IMPLEMENTATION OF QUEUE USING ARRAYS****\n");
printf("============================================");
printf("\n\t\tMENU\n");
printf("============================================");
printf("\n\t[1] To insert an element");
printf("\n\t[2] To remove an element");
printf("\n\t[3] To display all the elements");
printf("\n\t[4] Exit");
printf("\n\n\t Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("\nElement to be inserted:");
scanf("%d",&ele);
add_ele(queue,ele);
break;
}
7/29/2019 Data Structure Management
12/47
case 2:
{
if(!e_que(queue))
{
k=rem_ele(queue);
printf("\n%d element is removed\n",k);
getch();
}
else
{
printf("\tQueue is Empty. No element can be removed.");
getch();
}
break;
}
case 3:
{
display_ele(queue);
getch();
break;
}
case 4:
exit(0);
7/29/2019 Data Structure Management
13/47
default:
printf("\tInvalid Choice.");
getch();
break;
}
}
}
/*end main*/
void init(struct q_arr* queue)
{
queue->f = 0;
queue->r = -1;
queue->num = 0;
}
/* Function to check is the queue is empty*/
int e_que(struct q_arr* queue)
{
if(queue->num==0)
return true;
return false;
}
/* Function to check if the queue is full*/
int f_que(struct q_arr* queue)
{
7/29/2019 Data Structure Management
14/47
if(queue->num == size)
return true;
return false;
}
/* Function to add an element to the queue*/
int add_ele(struct q_arr* queue,int j)
{
if(f_que(queue))
return false;
if(queue->r == size - 1)
queue->r = -1;
queue->a[++queue->r] = j;
queue->num++;
return true;
}
/* Function to remove an element of the queue*/
int rem_ele(struct q_arr* queue)
{
int j;
if(e_que(queue))
return -9999;
j = queue->a[queue->f++];
if(queue->f == size)
queue->f = 0;
7/29/2019 Data Structure Management
15/47
queue->num--;
return j;
}
/* Function to display the queue*/
void display_ele(struct q_arr* queue)
{
int j;
if(e_que(queue))
{
printf("Queue is Empty. No records to display.");
return;
}
printf("\nElements present in the Queue are: ");
for(j=queue->f;jr;j++)
printf("%d\t",queue->a[j]);
printf("\n");
}
#include
#include
void append();
int del(int);
void display();
int insert();
int insertn();
int search(int);
7/29/2019 Data Structure Management
16/47
void rev();
int num,loc;
char name[20];
struct node
{
int a;
char n[20];
struct node *next;
};
struct node *first,*last;
//first=last=NULL;
int main()
{
int ch;
clrscr();
do
{
printf("\n Enter the choice \n 1.Insert\n 2.delete\n
3.Search\n 4.Dispaly\n 5.exit\n \n");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
7/29/2019 Data Structure Management
17/47
break;
case 2:
printf("\n enter the value \n");
scanf("%d",&num);
del(num);
break;
case 3:
printf("\n enter the value to find \n");
scanf("%d",&num);
search(num);
break;
case 4:
display();
break;
default:
break;
}
}while(ch!=5);
return 0;
}
int insert()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
printf("\n enter value: ");
scanf("%d",&temp->a);
printf("\n enter the location: ");
7/29/2019 Data Structure Management
18/47
scanf("%s",temp->n);
if(first==NULL)
{
first=temp;
first->next=NULL;
}
else
{
temp->next=first;
first=temp;
}
display();
return 0;
}
int del(int num)
{
struct node *temp,*m;
temp=first;
while(temp!=NULL)
{
if(temp->a==num)
{
if(temp==first)
{
first=temp->next;
7/29/2019 Data Structure Management
19/47
free(temp);
return;
}
else
{
m->next=temp->next;
free(temp);
return;
}
}
else
{
m=temp;
temp=temp->next;
}
}
}
int search(int num)
{
struct node *temp;
temp=first;
while(temp!=NULL)
{
if(temp->a==num)
{
printf("%d is exist",num);
7/29/2019 Data Structure Management
20/47
printf("\t its name is %s",temp->n);
break;
}
else
temp=temp->next;
}
if(temp->a!=num)
{
printf("not exist");
}
}
void display()
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp=first;
printf("\n");
while(temp!=NULL)
{
printf("%d %s\t",temp->a,temp->n);
temp=temp->next;
}
printf("\n");
}
7/29/2019 Data Structure Management
21/47
#include
#include
#include
#include
struct node
{
char data[15];
struct node *left, *right;
};
void insert(struct node *r, struct node *p)
{
if ((r->right == NULL) && (strcmp(p->data, r->data) > 0))
r->right = p;
else if ((r->right != NULL) && (strcmp(p->data, r->data) >
0))
insert(r->right, p);
if ((r->left == NULL) && (strcmp(p->data, r->data) < 0))
r->left = p;
else if ((r->left != NULL) && (strcmp(p->data, r->data) left, p);
}
void tree(struct node *r, int c)
{
int top, flag;
struct node *w, *stack[20];
if (r != NULL)
{
7/29/2019 Data Structure Management
22/47
if (c != 4)
{
if (c == 1)
printf(" %s ", r->data);
tree(r->left, c);
if (c == 2)
printf(" %s ", r->data);
tree(r->right, c);
if (c == 3)
printf(" %s ", r->data);
}
}
if (c == 4)
{
top = 0;
w = r;
flag = 0;
while ((top != - 1) && (w != NULL))
{
while ((flag == 0) && (w->left != NULL))
{
stack[top] = w;
top++;
w = w->left;
}
printf(" %s ", w->data);
if (w->right != NULL)
7/29/2019 Data Structure Management
23/47
{
w = w->right;
flag = 0;
}
else
{
top--;
w = stack[top];
flag = 1;
}
}
}
}
void main()
{
int choice, c, i, flag;
char temp = 'N', temp1[15];
struct node *s, *root, *r, *q;
clrscr();
root = NULL;
do
{
printf("\n 1. Enter");
printf("\n 2. Delete ");
printf("\n 3. Search ");
printf("\n 4. Display");
7/29/2019 Data Structure Management
24/47
printf("\n 5. Exit");
printf("\nEnter Your Choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("***** Data Entry ***** ");
do
{
s = malloc(sizeof(struct node));
s->left = NULL;
s->right = NULL;
printf("\nEnter Data : ");
scanf("%s", &s->data);
if (root == NULL)
root = s;
else
insert(root, s);
printf("\nEnter Your Elements[y/n] : ");
scanf("%c", &temp);
}
while (temp == 'y');
break;
case 2:
printf("****** Delete Operation *******\n");
do
{
7/29/2019 Data Structure Management
25/47
printf("\nEnter Element To Be Deleted : ");
scanf("%s", temp1);
s = root;
i = 0;
flag = 0;
do
{
if (strcmp(s->data, temp1) > 0)
{
r = s;
s = s->left;
i = 2;
}
if (strcmp(s->data, temp1) == 0)
{
flag = 1;
if (i == 0)
{
if (root->right != NULL)
{
q = root->left;
root = root->right;
insert(root, q);
}
if (root->right == NULL)
root = root->left;
}
7/29/2019 Data Structure Management
26/47
else
{
if (i == 1)
{
q = s->left;
r->right = s->right;
if (s->left != NULL)
insert(r, q);
}
if (i == 2)
{
q = s->right;
r->left = s->left;
if (s->right != NULL)
insert(r, q);
}
}
}
}
while (flag == 0 && s != NULL);
printf("\n Delete Any More[Y/N] : ");
scanf("%c", &temp);
}
while (temp == 'y')
;
break;
case 3:
7/29/2019 Data Structure Management
27/47
printf("****** Search Operation *******\n");
do
{
printf("\n Enter Name To Be Searched");
scanf("%s", temp1);
i = 0;
s = root;
while (s != NULL && i == 0)
{
if (strcmp(s->data, temp1) < 0)s = s-
>right;
if (strcmp(s->data, temp1) > 0)
s = s->left;
if (strcmp(s->data, temp1) == 0)
i = 1;
}
if (i == 0)
printf("\nElement Not Found\n");
else
printf("\nElement Found\n");
printf("\nEnter More Elements[Y/N] : ");
scanf("%c", &temp);
}
while (temp == 'y')
;
break;
case 4:
do
7/29/2019 Data Structure Management
28/47
{
printf(
"\n 1. Preorder\n 2. Inorder \n 3. Postorder
\n 4. Non Recursion \n 5. Exit");
printf("\nEnter Your Choice : ");
scanf("%d", &c);
if (root == NULL)
printf("Tree Not Started Yet");
else
tree(root, c);
printf("\n Press Any Key To Continue......");
getch();
}
while (c != 5);
break;
}
}
while (choice != 5)
;
}
// sequential search
#include
void main()
{
int arr[50],k,i,l,pos=0;
7/29/2019 Data Structure Management
29/47
clrscr();
printf("Enter limit For SEQUENTIAL SEARCH :- ");
scanf("%d",&l);
printf("\nEnter %d elements\n",l);
for(i=0;i
7/29/2019 Data Structure Management
30/47
void main()
{
int a[10],n,i,j,temp;
int beg,end,mid,target;
clrscr();
printf("enter the total numbers:");
scanf("%d",&n);
printf("enter the array elements: ");
for(i=0;i
7/29/2019 Data Structure Management
31/47
end=n-1;
mid=(beg+end)/2;
printf("\nEnter the number to be searched:");
scanf("%d",&target);
while(beg
7/29/2019 Data Structure Management
32/47
{
int a[MAX],n,i;
clrscr();
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0;i=up)
return;
printf("Sublist : ");
display(arr,low,up);
/*Loop till pivot is placed at proper place in the sublist*/
while(pivot_placed==FALSE)
{
/*Compare from right to left */
while(arr[piv] arr[right] )
{
temp=arr[piv];
arr[piv]=arr[right];
arr[right]=temp;
piv=right;
}
/*Compare from left to right */
while( arr[piv]>=arr[left] && left!=piv )
left++;
if(piv==left)
pivot_placed=TRUE;
if( arr[piv] < arr[left] )
{
temp=arr[piv];arr[piv]=arr[left];
7/29/2019 Data Structure Management
33/47
arr[left]=temp;
piv=left;
}
}/*End of while */
printf("-> Pivot Placed is %d -> ",arr[piv]);
display(arr,low,up);printf("\n");
quick(arr,low,piv-1);
quick(arr,piv+1,up);
}/*End of quick()*/
display(int arr[],int low,int up)
{
int i;
for(i=low;ilink=NULL;
if(start==NULL) /* Inserting first element */
start=tmp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=tmp;}
}/*End of for*/
7/29/2019 Data Structure Management
34/47
printf("Unsorted list is :\n");
display();
radix_sort();
printf("Sorted list is :\n");
display ();
}/*End of main()*/
display()
{
struct node *p=start;
while( p !=NULL)
{
printf("%d ", p->info);
p= p->link;
}
printf("\n");
}/*End of display()*/
radix_sort(){
int i,k,dig,maxdig,mindig,least_sig,most_sig;
struct node *p, *rear[10], *front[10];
least_sig=1;
most_sig=large_dig(start);
for(k = least_sig; k maxdig)
maxdig=dig;
if(diglink = p ;
rear[dig] = p ;
p=p->link;/*Go to next number in the list*/
}/*End while */
/* maxdig and mindig are the maximum amd minimum
digits of the kth digits of all the numbers*/
printf("mindig=%d maxdig=%d\n",mindig,maxdig);
/*Join all the queues to form the new linked list*/
start=front[mindig];for(i=mindig;i
7/29/2019 Data Structure Management
35/47
{
if(rear[i+1]!=NULL)
rear[i]->link=front[i+1];
else
rear[i+1]=rear[i];
}
rear[maxdig]->link=NULL;printf("New list : ");
display();
}/* End for */
}/*End of radix_sort*/
/* This function finds number of digits in the largest element of the list
*/
int large_dig()
{
struct node *p=start ;
int large = 0,ndig = 0 ;
while(p != NULL)
{
if(p ->info > large)
large = p->info;
p = p->link ;
}
printf("Largest Element is %d , ",large);
while(large != 0)
{
ndig++;
large = large/10 ;
}
printf("Number of digits in it are %d\n",ndig);
return(ndig);
} /*End of large_dig()*/
/*This function returns kth digit of a number*/
int digit(int number, int k)
{
int digit, i ;
for(i = 1 ; i
7/29/2019 Data Structure Management
36/47
scanf("%d",&n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ",i+1);
scanf("%d", &arr[i]);
}
printf("Unsorted list is : \n");for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
/*Selection sort*/
for(i=0;i arr[k])
smallest = k ;
}if(i!=smallest)
{
temp=arr[i];
arr[i]=arr[smallest];
arr[smallest] = temp ;
}
printf("After Pass %d elements are : ",i+1);
for (j = 0; j < n; j++)
printf("%d ", arr[j]);
printf("\n");
}/*End of for*/
printf("Sorted list is : \n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
getch();
}/*End of main()*/
Program of sorting using shell sort
Code:
/* Program of sorting using shell sort */
#include
#define MAX 20
main()
{
int arr[MAX], i,j,k,n,incr;
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0;i
7/29/2019 Data Structure Management
37/47
scanf("%d",&incr);
/*Shell sort*/
while(incr>=1)
{
for(j=incr;j= 0 && k < arr[i]; i = i-incr)
arr[i+incr]=arr[i];
arr[i+incr]=k;
}
printf("Increment=%d \n",incr);
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
incr=incr-2; /*Decrease the increment*/
}/*End of while*/
printf("Sorted list is :\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);printf("\n");
}/*End of main()*/
Heap Sort
Code:
/* HEAP SORT */
/* HEAP.C */
# include
void heap_sort(int *, int );
void create_heap(int *, int);void display(int *, int);
/* Definition of the function */
void create_heap(int list[], int n )
{
int k, j, i, temp;
for(k = 2 ; k 1) && (temp > list[j]))
{
list[i] = list[j];
i = j ;
j = i / 2 ;
if ( j < 1 )
j = 1 ;
}
list[i] = temp ;
}}
7/29/2019 Data Structure Management
38/47
/* End of heap creation function */
/* Definition of the function */
void heap_sort(int list[], int n)
{
int k, temp, value, j, i, p;int step = 1;
for(k = n ; k >= 2; --k)
{
temp = list[1] ;
list[1] = list[k];
list[k] = temp ;
i = 1 ;
value = list[1];
j = 2 ;
if((j+1) < k)
if(list[j+1] > list[j])j ++;
while((j value))
{
list[i] = list[j];
i = j ;
j = 2*i ;
if((j+1) < k)
if(list[j+1] > list[j])
j++;
else
if( j > n)
j = n ;
list[i] = value;
} /* end of while statement */
printf("\n Step = %d ", step);
step++;
for(p = 1; p
7/29/2019 Data Structure Management
39/47
for(i = 1 ; i
7/29/2019 Data Structure Management
40/47
int temp, i, j;
for (i = 0; i < size; i++)
for (j = 0; j < size; j++)
if (array[i] < array[j])
{
temp = array[i];array[i] = array[j];
array[j] = temp;
}
}
void main(void)
{
int values[30], i;
printf("\n Unsorted list is as follows\n");
for (i = 0; i < 10; i++)
{
values[i] = rand() % 100;printf(" %d", rand()%100);
}
bubble_sort(values, 10);
printf("\n Sorted list is as follows\n");
for (i = 0; i < 10; i++)
printf("%d ", values[i]);
}
OUTPUT:
Unsorted list is as follows
67 0 24 58 64 45 27 91 42 36
Sorted list is as follows
27 34 41 61 62 69 78 81 95
Press any key to continue
Insertion Sort
Code:
/* Insertion sort */
/* INSERT.C */
# include
# include
void insertion_sort(int *, int);
void display(int*, int);
void insertion_sort(int l[], int n)
{
int pointer, temp;
int i, k;
l[0] = -0;
for(i = 1 ; i
7/29/2019 Data Structure Management
41/47
{
l[pointer+1] = l[pointer];
pointer --;
}
l[pointer+1] = temp;
printf("Step = %d", i);
for(k = 0; k
7/29/2019 Data Structure Management
42/47
Step = 10 0 5 27 34 41 61 62 69 78 81 95
Sorted list is as follows
5 27 34 41 61 62 69 78 81 95Press any key to continue
Merge Sort
Code:
/* MERGE SORT */
/* merge.c */
# include
# include
void merge_sort(float *, int , int , int );
void merge_pass(float *, int , int );
/* Definition of the function */
void merge_sort(float l[], int top, int size, int bottom){
float temp[1000];
int f = top;
int s = size +1 ;
int t = top ;
int upper;
while(( f
7/29/2019 Data Structure Management
43/47
for(upper = top; upper
7/29/2019 Data Structure Management
44/47
Press any key to continue
1.
#include
#include
void main()
{
char a[25],b[25],j[100];
int i,c,l,pos,k;
clrscr();
printf("Enter Your choice \n1.Find Lenght of String\n2.Copy
One string to other\n3.Concate two string\n4.Find substring
\n\n Your Choice:-");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter String:- ");
scanf("%s",a);
i=0;
while(a[i]!='\0')
i++; //counts no of chars till encountering null
char
printf("string length=%d",i);
break;
case 2:
printf("Enter String:- ");
scanf("%s",a);
7/29/2019 Data Structure Management
45/47
for(i=0;b[i]!='\0';i++)
b[i]=a[i];
b[i]='\0';
printf("Copied Stringb = %s",b);
break;
case 3:
printf("Enter first String:- ");
scanf("%s",a);
printf("\nEnter second String:- ");
scanf("%s",b);
i=0;l=0;
while(a[i])
{
j[i]=a[i];
++i;
}
j[i]=' ';
++i;
while(b[l])
{
j[i]=b[l];
++i;
++l;
}
j[i]='\0';
7/29/2019 Data Structure Management
46/47
printf("\nconcated string= ");
printf(j);
break;
case 4:
printf("Enter Sting= ");
scanf("%s",a);
printf("Enter Positon and lenght of string: ");
scanf("%d%d",&pos,&l);
for(i=0;a[i]!='\0';i++);
if(pos>i)
{
printf("Invalid Position!!");
getch();
exit (0);
}
c=i-pos+1;
if(l>c)
{
printf("Invalid SubString");
getch();
exit (0);
}
c=0;
c=pos-1;
for(k=0;k
7/29/2019 Data Structure Management
47/47
{
j[k]=a[c];
c++;
}
j[k]='\0';
printf("substring= %s",j);
break;
case 5:
break;
default:
printf("Invalid Selection");
break;
}
getch();
}