DS Programs
-
Upload
navinkiller -
Category
Documents
-
view
224 -
download
0
Transcript of DS Programs
-
7/30/2019 DS Programs
1/34
https://sites.google.com/site/itstudentjunction/data-structures-lab-programs
Write a menu driven c program which performs the following string operations
- Find the Length of the string- Concatenate two strings
- Compare two string and display if they are equal- Copy one string from the other- Extract a part of the string (starting point and no of characters to be extracted asinput)
Code for Program to compare strings, concatenate strings, copy stringand display part of a string in C Programming
# include void main() { int c; void func1(); void func2(); void func3(); void func4(); void func5(); clrscr(); printf("\n- : User Defined String Functions : -"); printf("\n-------------------------------------"); printf("\n Find String Length : 1"); printf("\n Concatenate two Strings : 2"); printf("\n String Comparison : 3"); printf("\n String Copy : 4"); printf("\n Extract SubString : 5"); printf("\n Enter Your Choice : "); scanf("%d",&c); switch(c) { case 1: func1(); break;
case 2: func2(); break; case 3: func3(); break; case 4: func4(); break; case 5: func5(); break; default:
https://sites.google.com/site/itstudentjunction/data-structures-lab-programshttps://sites.google.com/site/itstudentjunction/data-structures-lab-programs -
7/30/2019 DS Programs
2/34
printf("\nInvalid Choice"); } getch(); } void func1() { char str[50]; int slength(char []); int l; clrscr(); fflush(stdin); printf("\nEnter String : "); scanf("%[^\n]",str); l=slength(str); printf("\nLength = %d",l); } int slength(char x[]) { int i=0; while(x[i]!='\0') i++; return i; } void func2() { char str1[50],str2[50]; void sconcate(char [],char []); clrscr(); fflush(stdin); fflush(stdin); printf("\nEnter String 1 : "); scanf("%[^\n]",str1); fflush(stdin); printf("\nEnter String 2 : "); scanf("%[^\n]",str2); sconcate(str1,str2); printf("%s",str1);
} void sconcate(char s1[],char s2[]) { int i=0,j; j=slength(s1); while(s2[i]!='\0') { s1[j]=s2[i]; j++; i++; } s1[j]='\0'; }
-
7/30/2019 DS Programs
3/34
void func3() { char str1[50],str2[50]; int l; int scomp(char [],char []); clrscr(); fflush(stdin); printf("\nEnter String 1 : "); scanf("%[^\n]",str1); fflush(stdin); printf("\nEnter String 2 : "); scanf("%[^\n]",str2); l=scomp(str1,str2); if(l==0) printf("\nBoth Strings are Equal"); elseif(l>0) printf("\nString 1 is greater than String 2"); else printf("\nString 2 is greater than String 1"); } int scomp(char s1[],char s2[]) { int i,j,z; int l; for(i=0,j=0;(s1[i]!='\0' || s2[i]!='\0');i++,j++) { if(s1[i]!=s2[i]) { if(s1[i]>s2[i]) l=1; else l=-1; break; } } if(s1[i]=='\0' && s2[i]=='\0') l=0;
return l; } void func4() { char str1[50],str2[50]; void scopy(char [],char []); clrscr(); fflush(stdin); printf("\nEnter String : "); scanf("%[^\n]",str1); scopy(str2,str1); printf("\nCopied String : %s",str2); }
-
7/30/2019 DS Programs
4/34
void scopy(char s2[],char s1[]) { int i; i=0; while(s1[i]!='\0') { s2[i]=s1[i]; i++; } s2[i]='\0'; } void func5() { char str[50],sub[50]; int s,n; void substr(char [],int,int,char []); clrscr(); fflush(stdin); printf("\nEnter string : "); scanf("%[^\n]",str); fflush(stdin); printf("\nEnter substring starting character index : "); scanf("%d",&s); fflush(stdin); printf("\nEnter total no. of characters : "); scanf("%d",&n); substr(str,s,n,sub); printf("\nSubstring = %s",sub); } void substr(char str[],int s,int n,char ds[]) { int i,j; j=0; i=s; while( (i
-
7/30/2019 DS Programs
5/34
Singly Linked list with below operations
1.CREATION
2.INSERT AT STARTING3.INSERT AT MIDDLE(USER'S CHOICE)
4.INSERT AT END5.DELETE 1ST NODE6.DELETE LAST NODE7.DELETE MIDDLE NODE(USER'S CHOICE)8.DISPLAY
Code for Singly Linked list with following operations INSERT ATSTARTING, INSERT AT MIDDLE, INSERT AT END, DELETE FIRSTNODE, DELETE LAST NODE, DELETE MIDDLE in C Programming
#include
#include
struct node
{
int i; struct node *next;};
void main()
{
struct node *first; struct node *last; struct node *temp; int ch,user,add,cnt=0,t=0; struct node *p; clrscr(); printf("\n\t 1.CREATION"); printf("\n\t 2.INSERT AT STARTING"); printf("\n\t 3.INSERT AT MIDDLE(USER'S CHOICE)"); printf("\n\t 4.INSERT AT END"); printf("\n\t 5.DELETE 1ST NODE"); printf("\n\t 6.DELETE LAST NODE"); printf("\n\t 7.DELETE MIDDLE NODE(USER'S CHOICE)"); printf("\n\t 8.DISPLAY"); printf("\n\t 10.EXIT"); scanf("%d",&user); while(user!=10) { if(user==1)
-
7/30/2019 DS Programs
6/34
{ printf("\n\t ENTER DATA ::: "); first=(struct node*)malloc(sizeof(struct node)); scanf("%d",&ch); first->i=ch; first->next=0; p=first; cnt=1; } if(user==2) { p=(struct node*)malloc(sizeof(struct node)); printf("\n\t ENTER DATA FOR 1ST NODE"); scanf("%d",&p->i); p->next=first; first=p; cnt++; } if(user==4) { p=(struct node*)malloc(sizeof(struct node)); printf("\n\t ENTER DATA FOR LAST NODE"); scanf("%d",&p->i); temp=first; while(temp->next!=0) { temp=temp->next; } temp->next=p; p->next=0; cnt++; } if(user==3) { printf("\n\t ENTER ANY ADDRESS BETWEEN 1 and %d",cnt); scanf("%d",&add); t=1; p=first; while(t!=add) { p=p->next; t++; } temp=(struct node*)malloc(sizeof(struct node)); printf("\n\t ENTER DATA FOR NODE"); scanf("%d",&temp->i); temp->next=p->next; p->next=temp;
-
7/30/2019 DS Programs
7/34
cnt++; } if(user==5) { p=first; first=p->next; free(p); } if(user==6) { p=first; while(p->next->next!=0) { p=p->next; } p->next=0; free(p->next->next); } if(user==8) { p=first; while(p!=0) { printf("\n\t %d",p->i); p=p->next; } } if(user==7) { printf("\n\t ENTER ANY ADDRESS BETWEEN 1 and %d",cnt); scanf("%d",&add); t=1; p=first; while(tnext; t++; } temp=p->next; p->next=temp->next; free(temp); cnt--; } printf("\n\t 1.CREATION"); printf("\n\t 2.INSERT AT STARTING"); printf("\n\t 3.INSERT AT MIDDLE(USER'S CHOICE)"); printf("\n\t 4.INSERT AT END");
-
7/30/2019 DS Programs
8/34
printf("\n\t 5.DELETE 1ST NODE"); printf("\n\t 6.DELETE LAST NODE"); printf("\n\t 7.DELETE MIDDLE NODE(USER'S CHOICE)"); printf("\n\t 8.DISPLAY"); printf("\n\t 10.EXIT"); scanf("%d",&user); } getch();}
Write a C program using pointers to implement a stackwith all the operations.
Program to implement stack with its operations using pointers
#include#include#include#define MAX 50int size;/* Defining the stack structure */structstack{ int arr[MAX]; int top;};
/* Initializing the stack(i.e., top=-1) */voidinit_stk(structstack*st){ st->top =-1;}/* Entering the elements into stack */voidpush (structstack*st,int num){ if(st->top == size-1) { printf("nStack overflow(i.e., stack full)."); return; } st->top++; st->arr[st->top]= num;}//Deleting an element from the stack.int pop(structstack*st){ int num; if(st->top ==-1) { printf("nStack underflow(i.e., stack empty)."); returnNULL; }
-
7/30/2019 DS Programs
9/34
num = st->arr[st->top]; st->top--; return num;}voiddisplay(structstack*st){ int i; for(i=st->top;i>=0;i--) printf("n%d",st->arr[i]);}intmain(){ int element,opt,val; structstack ptr; init_stk(&ptr); printf("nEnter Stack Size :"); scanf("%d",&size); while(1) { printf("nntSTACK PRIMITIVE OPERATIONS");
printf("n1.PUSH"); printf("n2.POP"); printf("n3.DISPLAY"); printf("n4.QUIT"); printf("n"); printf("nEnter your option : "); scanf("%d",&opt); switch(opt) { case 1: printf("nEnter the element into stack:");
scanf("%d",&val); push(&ptr,val); break; case 2: element = pop(&ptr); printf("nThe element popped from stack is :%d",element); break; case 3: printf("nThe current stack elements are:"); display(&ptr); break
; case 4: exit(0); default: printf("nEnter correct option!Try again."); } }return(0);}
-
7/30/2019 DS Programs
10/34
Output :
Explanation of Program :
We are declaring the stack with MAX size specified by the preprocessor.
struct stack
{
int arr[MAX]; int top;};
As soon as after defining the stack we are initializing the top location of stack to -1. We are
passing the structure to the function using pointer thus we can see the struct stack* as
data type in the function call.
void init_stk(struct stack *st)
{
st->top = -1;}
Whenever we are accessing the member of the structure using the structure pointer then
we are using arrow operator. If we have to access the top element of stack then we arewriting
st->top
-
7/30/2019 DS Programs
11/34
Similarly if we have to access oth element of the stack array then we can write -
st->arr[0]
Since we have to access the topmost element we can write it as -
st->arr[st->top]
Procedure of Pushing :
1.Check whether the size reached upto the maximum or not,
2.Increment the top
3.Insert the element.
Similar steps are depicted in the code -voidpush (structstack*st,int num){ if(st->top == size-1) { printf("nStack overflow(i.e., stack full)."); return; } st->top++; st->arr[st->top]= num;}
Procedure of Poping element :
1.Check whether the size reached upto the minimum or not(underflow)
2.Remove Element
3.Decrement the top
int pop(structstack*st){ int num; if(st->top ==-1) { printf("nStack underflow(i.e., stack empty)."); returnNULL; } num = st->arr[st->top];
st->top--; return num;}
PROGRAM TO CREATE ADD REMOVE & DISPLAY ELEMENT FROM SINGLELINKED LIST using c program or
PROGRAM TO IMPLEMENT SINGLE LINKED LIST IN c language
#include
-
7/30/2019 DS Programs
12/34
#include
#include
struct info
{
char name[30];
int eno;
struct info *next;};
struct info *head=NULL,*temp,*disp;
void addrecord();
void deleterecord();
void disrecord();
void main()
{
int ch;
clrscr();
while (1)
{
printf("\n 1. To add records\n");
printf("\n 2. To delete a records\n");
printf("\n 3. To view the records\n");
printf("\n 4. To exit\n");
printf("\n Enter your choice\n");
scanf("%d",&ch);
fflush(stdin);
switch(ch)
{ case 1:addrecord();
break;
case 2:deleterecord();
break;
case 3: disrecord();
break;
case 4:exit(0);
}
}
-
7/30/2019 DS Programs
13/34
}
void addrecord()
{
struct info *add;
char ans='y';
while (ans=='y')
{
add=(struct info*)malloc(sizeof(struct info));
printf("\n Enter the names:\n");
gets(add->name);
fflush(stdin);
printf("\n Enter the enrollment number:\n");
scanf("%d",&add->eno);
fflush(stdin);
if (head==NULL)
{
head=add;
add->next=NULL;
temp=add;
}
else
{
temp->next=add; add->next=NULL;
temp=add;
}
printf("\n Would you like to enter another name(y\\n): \n");
ans = getchar();
fflush(stdin);
}
}
-
7/30/2019 DS Programs
14/34
void deleterecord()
{
struct info *delete;
int teno, present=0;
if (head==NULL)
{ printf("\n No records to delete\n");
return;
}
printf("\n Enter the enrollment number to be deleted \n");
scanf("%d",&teno);
fflush(stdin);
for (delete=head;delete!=NULL;delete=delete->next)
{
if (delete->eno==teno)
{
if (head->eno==teno)
{
delete=head;
head=head->next;
free(delete);
return;
}
else
{
temp->next=delete->next;
free(delete); return;
}
}
temp=delete;
}
if (present==0)
printf("\nNo such enrollment number present\n");
}
-
7/30/2019 DS Programs
15/34
void disrecord()
{
if (head==NULL)
{
printf("\n No records to view\n");
return;
} for (disp=head;disp!=NULL;disp=disp->next)
{
printf("\n\n Name : %s",disp->name);
printf("\n\n Number : %d",disp->eno);
}
}
PROGRAM TO IMPLEMENT QUEUE USING Pointers in c
#include < stdio.h>
#include < conio.h>
#include < malloc.h>
#include < process.h>
#include < ctype.h>
struct linear_queue
{
int info;
struct linear_queue *next;
}*front,*rear,*newnode,*ptr;
void menu();
void display();
int underflow();
void enqueue(int);
void dequeue();
void main()
{
clrscr();
menu();
}
void menu()
-
7/30/2019 DS Programs
16/34
{
int choice,item;
printf("MENU");
printf("\n1. Insert into the queue");
printf("\n2. Delete from queue");
printf("\n3. Display");
printf("\n4. Exit");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
clrscr();
printf("\nEnter the item tobe inserted: ");
scanf("%d",&item);
enqueue(item);
clrscr();
printf("\nAfter inserting queue is:\n");
display();
getch();clrscr();
menu();
break;
case 2:
clrscr();
if(underflow()==1)
{
dequeue();
if(underflow()==1)
{
printf("\nAfter deletion queue is:\n");
display();
}
}
getch();
clrscr();
menu();
break;
case 3:
clrscr();
if(underflow()==1){
printf("The queue is:\n");
display();
}
getch();
clrscr();
menu();
break;
case 4:
exit(1);
default:
clrscr();
printf("Your choice is wrong\n\n");
-
7/30/2019 DS Programs
17/34
menu();
}
}
int underflow()
{
if((front==NULL)&&(rear==NULL))
{
printf("\nQueue is empty");
return(0);
}
else
{
return(1);
}
}
void enqueue(int item)
{
newnode=(struct linear_queue*)malloc(sizeof(struct linear_queue));newnode->info=item;
if((front==NULL)&&(rear==NULL))
{
front=newnode;
rear=newnode;
newnode->next=NULL;
}
else
{
rear->next=newnode;
newnode->next=NULL;
rear=newnode;
}
}
void dequeue()
{
if(front==rear)
{
front=NULL;
rear=NULL;}
else
{
front=front->next;
}
}
void display()
{
int i;
ptr=front;
i=1;
while(ptr!=NULL)
-
7/30/2019 DS Programs
18/34
{
printf("\nNode %d : %d",i,ptr->info);
ptr=ptr->next;
i++;
}
}
-
7/30/2019 DS Programs
19/34
PROGRAM TO IMPLEMENT BINARY TREEORPROGRAM FOR THE CREATION OF BINARY TREE, PROVIDE INSERTION &DELETION USING C LANGUAGE
#include
#include
#include
struct node
{
int data;
struct node *left,*right;
};
struct node *root;
void insert(int x)
{
struct node *p,*previous,*current;
p=(struct node *)malloc(sizeof(struct node));
if(p==NULL)
{
printf("\n Out of memory");
}
p->data=x;
p->left=NULL;
p->right=NULL;
if(root=NULL)
{
root=p;
return;
}
previous=NULL;
current=root;
while(current!=NULL)
{
previous=current;
if(p->datadata)
current=current->left;
else
-
7/30/2019 DS Programs
20/34
current=current->right;
}
if(p->datadata)
previous->left=p;
else
previous->right=p;
}void inorder(struct node *t)
{
if (t!=NULL)
{
inorder(t->left);
printf("\n %5d",t->data);
inorder (t->right);
}
}
void del(int x)
{
int tright=0,tleft=0;
struct node *ptr=root;
struct node *parent=root;
struct node *t1=root;
struct node *temp=root;
while(ptr!=NULL&& ptr->data!=x)
{
parent=ptr;
if (xdata)
ptr=ptr->left;
else ptr=ptr->right;
}
if (ptr==NULL)
{
printf("\n Delete element not found");
return ;
}
else if(t1->data==x && (t1->left ==NULL || t1->right==NULL))
if(t1->left==NULL)
-
7/30/2019 DS Programs
21/34
t1=t1->right;
else
t1=t1->left;
else if (ptr->left==NULL)
if (xdata)
parent->left=ptr->right;
else parent->right=ptr->right;
else if (ptr->right==NULL)
if (xdata)
parent->left=ptr->left;
else
parent->right=ptr->left;
else
{
temp=ptr;
parent=ptr;
if((ptr->left)>=(ptr->right))
{
ptr=ptr->left;
while(ptr->right!=NULL)
{
tright=1;
parent=ptr;
ptr=ptr->right;
}
temp->data=ptr->data;
if(tright)
parent->right=ptr->left; else
parent->left=ptr->left;
}
else
{
ptr=ptr->right;
while (ptr->left!=NULL)
{
tleft=1;
-
7/30/2019 DS Programs
22/34
parent=ptr;
ptr=ptr->left;
}
temp->data=ptr->data;
if(tleft)
parent->left=ptr->right;
else parent->right=ptr->right;
}
free(ptr);
}
}
void main()
{
int op,n,srchno;
root=(struct node *)malloc(sizeof(struct node));
root->data=30;
root->right=root->left=NULL;
clrscr();
do
{
printf("\n 1.Insertion");
printf("\n 2.Deletion");
printf("\n 3.Inorder");
printf("\n 4.Quit");
printf("\n Enter your choice\n");
scanf("%d",&op);
switch (op)
{
case 1: printf("\n Enter the element to insert\n");
scanf("%d",&n);
insert(n);
break;
case 2: printf("\n Enter the element to be deleted\n");
scanf("%d",&srchno);
del(srchno);
-
7/30/2019 DS Programs
23/34
break;
case 3: printf("\n The inorder elements are\n");
inorder(root);
getch();
break;
default: exit(0);
} }while(opdata=val;
t->right=t->left=NULL;
if (opt==1)
n->left=t;
else
n->right=t;
printf("\n %d is inserted",val);
if (opt==1)
{
printf("\tat the left\n");
getch();
-
7/30/2019 DS Programs
24/34
}
else
{
printf("\tat the right\n");
getch();
}
}
void inser(struct node *t,int x)
{
if (t->data >x)
if (t->left==NULL)
ins(t,x,1);
else
inser(t->left,x);
else if (t->data < x)
if (t->right==NULL)
ins(t,x,2);
else
inser(t->right,x);
else
printf("\n Element is already present in the list\n");
}
void inorder(struct node *p)
{
if (p!=NULL)
{
inorder(p->left);
printf("\n %5d",p->data);
inorder (p->right);
}
}
void preorder(struct node *p)
{
if (p!=NULL)
{
printf("\n %5d",p->data);
preorder(p->left);
-
7/30/2019 DS Programs
25/34
preorder (p->right);
}
}
void postorder(struct node *p)
{
if (p!=NULL)
{
preorder(p->left);
preorder (p->right);
printf("\n %5d",p->data);
}
}
void main()
{
int op,n;
root=(struct node *)malloc(sizeof(struct node));
root->data=30;
root->right=root->left=NULL;
clrscr();
do
{
printf("\n 1.Insertion");
printf("\n 2.Preorder");
printf("\n 3.Inorder");
printf("\n 4.Postorder");
printf("\n 5.Quit");
printf("\n Enter your choice\n");
scanf("%d",&op);
switch (op)
{
case 1: printf("\n Enter the element to insert\n");
scanf("%d",&n);
inser(root,n);
break;
case 2: printf("\n The preorder elements are\n");
preorder(root);
getch();
break;
case 3: printf("\n The inorder elements are\n");
-
7/30/2019 DS Programs
26/34
inorder(root);
getch();
break;
case 4: printf("\n The postorder elements are\n");
postorder(root);
getch();
break;
default: exit(0);
}
}while(op
-
7/30/2019 DS Programs
27/34
for(i=0;i
-
7/30/2019 DS Programs
28/34
}
cout
-
7/30/2019 DS Programs
29/34
printf (\nEnter element %i :,i);
scanf(%d,&elements[i]);
}
printf(\nArray before sorting:\n);
for (i = 0; i < maxsize; i++)
printf([%i], ,elements[i]);
printf (\n);
selection(elements, maxsize);
printf(\nArray after sorting:\n);
for (i = 0; i < maxsize; i++)
printf([%i], , elements[i]);
}
void selection(int elements[], int array_size)
{
int i, j, k;
int min, temp;
for (i = 0; i < maxsize-1; i++)
{
min = i;
for (j = i+1; j < maxsize; j++){
if (elements[j] < elements[min])
min = j;
}
temp = elements[i];
elements[i] = elements[min];
elements[min] = temp;
}
}
PROGRAM TO IMPLEMENT INSERTION SORT USING ARRAYS
#include
#include
void bubble(int a[],int n)
{
int i,j,t;
for(i=n-2;i>=0;i--)
{
for(j=0;ja[j+1])
{
t=a[j];a[j]=a[j+1];
a[j+1]=t;
-
7/30/2019 DS Programs
30/34
}
}
}//end for 1.
}//end function.
void main(){
int a[100],n,i;
clrscr();
printf("\n\n Enter integer value for total no.s of elements to be sorted:
");
scanf("%d",&n);
for( i=0;i
-
7/30/2019 DS Programs
31/34
int partition (int a[],int low,int high)
{
int p,i,j,temp;
p=a[low];
i=low+1;
j=high;
while(1)
{
while((p>=a[i])&&(i
-
7/30/2019 DS Programs
32/34
a[i]=a[j];
a[j]=temp;
}
else
{
temp=a[low];
a[low]=a[j];
a[j]=temp;
return j;
}
}
}
void quicksort(int a[],int low,int high)
{
int s;
if(low
-
7/30/2019 DS Programs
33/34
{
s=partition(a,low,high);
quicksort(a,low,s-1);
quicksort(a,s+1,high);
}
}
void main()
{
int a[MAXSIZE],i,n,k;
clock_t start,end;
double runtime=0;
clrscr();
printf("\n Enter the size of array");
scanf("%d",&n);
for(k=0;k
-
7/30/2019 DS Programs
34/34
srand(1);
for(i=0;i