Vous êtes sur la page 1sur 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 as input)

Code for Program to compare strings, concatenate strings, copy string and display part of a string in C Programming
# include<stdio.h> 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 printf("\n String Comparison printf("\n String Copy printf("\n Extract SubString 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: : 1"); : 3"); : 4"); : 5"); : "); printf("\n Concatenate two Strings : 2");

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'; }

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

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<(s+n)) && (str[i]!='\0')) { ds[j]=str[i]; i++; j++; } ds[j]='\0'; }

Singly Linked list with below operations 1.CREATION 2.INSERT AT STARTING 3.INSERT AT MIDDLE(USER'S CHOICE) 4.INSERT AT END 5.DELETE 1ST NODE 6.DELETE LAST NODE 7.DELETE MIDDLE NODE(USER'S CHOICE) 8.DISPLAY

Code for Singly Linked list with following operations INSERT AT STARTING, INSERT AT MIDDLE, INSERT AT END, DELETE FIRST NODE, DELETE LAST NODE, DELETE MIDDLE in C Programming
#include<conio.h> #include<stdio.h> 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)

{ printf("\n\t ENTER DATA 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; ::: "); first=(struct node*)malloc(sizeof(struct node));

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(t<add-1) { p=p->next; 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");

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 stack with all the operations.
Program to implement stack with its operations using pointers
#include<stdio.h> #include<conio.h> #include<stdlib.h> #define MAX 50 int size; /* Defining the stack structure */ struct stack { int arr[MAX]; int top; }; /* Initializing the stack(i.e., top=-1) */ void init_stk(struct stack *st) { 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->arr[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; }

num = st->arr[st->top]; st->top--; return num; } void display(struct stack *st) { int i; for(i=st->top;i>=0;i--) printf("n%d",st->arr[i]); } int main() { int element,opt,val; struct stack 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); }

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 are writing st->top

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 void push (struct stack *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(struct stack *st) { int num; if(st->top == -1) { printf("nStack underflow(i.e., stack empty)."); return NULL; } num = st->arr[st->top]; st->top--; return num; }

PROGRAM TO CREATE ADD REMOVE & DISPLAY ELEMENT FROM SINGLE LINKED LIST using c program or PROGRAM TO IMPLEMENT SINGLE LINKED LIST IN c language

#include<stdio.h>

#include<stdlib.h> #include<string.h> 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); } }

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

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

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

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

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)

{ printf("\nNode %d : %d",i,ptr->info); ptr=ptr->next; i++; } }

PROGRAM TO IMPLEMENT BINARY TREE OR PROGRAM FOR THE CREATION OF BINARY TREE, PROVIDE INSERTION & DELETION USING C LANGUAGE
#include<stdio.h> #include<conio.h> #include<alloc.h> 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->data<current->data) current=current->left; else

current=current->right; } if(p->data<previous->data) 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 (x<ptr->data) 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)

t1=t1->right; else t1=t1->left; else if (ptr->left==NULL) if (x<parent->data) parent->left=ptr->right; else parent->right=ptr->right; else if (ptr->right==NULL) if (x<parent->data) 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;

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

break; case 3: printf("\n The inorder elements are\n"); inorder(root); getch(); break; default: exit(0); } }while(op<4); getch();

} PROGRAM FOR PRE-ORDER,POST-ORDER & IN-ORDER TRAVERSALS OF A BINARY TREE USING NON RECCUSIVE. #include<stdio.h> #include<conio.h> #include<alloc.h>

struct node { int data; struct node *left,*right; }; struct node *root;

void ins(struct node *n,int val,int opt) { struct node *t; t=(struct node *)malloc(sizeof(struct node)); t->data=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();

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

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

inorder(root); getch(); break;

case 4: printf("\n The postorder elements are\n"); postorder(root); getch(); break; default: exit(0); } }while(op<5); getch(); }

PROGRAM TO IMPLEMENT BINARY SEARCH USING POINTERS IN C LANGUAGE


#include<stdio.h> void main() { int *a[100],i,no,*srchno,top,bottom,mid,j,*temp; clrscr(); printf("\n Enter the number of elements\n"); scanf("%d",&no); printf("\n Enter %d numbers\n",no); for(i=0;i<no;++i) scanf("%d",&a[i]); printf("Enter the search number\n"); scanf("%d",&srchno); for(i=0;i<no-1;++i) for(j=i+1;j<no;++j) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } printf("\n Sorted array in ascending order\n");

for(i=0;i<no;++i) printf("%5d",a[i]); bottom=0; top=no-1; while(top!=bottom+1) { mid=(bottom+top)/2; if (a[mid]<=srchno) bottom=mid; else top=mid; } if(a[bottom]==srchno) printf("\n search number is present"); else printf("\n Search number is not present"); }

PROGRAM TO IMPLEMENT BINARY SEARCH USING ARRAYS IN C language


#include<stdio.h> #include<conio.h> void main() { clrscr(); int a[100],n,i,j,item,temp; printf("Enter the no.of elements in array "); scanf(" "); cout<<"\n Enter the elements \n"; for(i=1;i<=n;i++) { scanf('%d",a[i]); } for(i=1;i<=n-1;i++) { for(j=1;j<=n-i;j++) { if (a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp;} }

} cout<<"\nSorted array: ";for(i=1;i<=n;i++) { printf("%d"a[i]); } printf(" \nEnter the element to be searched \n"); scanf("%d", item); int beg,end,mid,loc=-1; beg=1; end=n; mid=int(beg+end)/2; while((beg<=end) && (loc==-1)) { if (a[mid]==item) { loc=mid; } else if (item<a[mid]) { end=mid-1; } else{ beg=mid+1; } mid=int(beg+end)/2; } if (loc==-1) printf("search unsuccessfull"); else printf('search successfull"); printf("search location =%d", loc); }

SELECTION SORT PROGRAM USING ARRAYS

#include <stdio.h> #define MAXSIZE 500 void selection(int elements[], int maxsize); int elements[MAXSIZE],maxsize; int main() { int i; printf(\nHow many elements you want to sort: ); scanf(%d,&maxsize); printf(\nEnter the values one by one: ); for (i = 0; i < maxsize; i++) {

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<stdio.h> #include<conio.h> void bubble(int a[],int n) { int i,j,t; for(i=n-2;i>=0;i--) { for(j=0;j<=i;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t;

} } }//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<=n-1;i++) { printf("\n\n Enter integer value for element no.%d : ",i+1); scanf("%d",&a[i]); } bubble(a,n); printf("\n\n Finally sorted array is: "); for( i=0;i<=n-1;i++) printf("%3d",a[i]); } //end program.

#include<stdio.h> #include<conio.h> #include<time.h> #include<stdlib.h>

#define MAXSIZE 30000 #define NTIMES 5000

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<high)) i++; while((p<a[j])&&(j>=low)) j--;

if(i<j) { temp=a[i];

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

{ 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<NTIMES;k++) {

srand(1); for(i=0;i<n;i++) a[i]=rand();

start=clock(); quicksort(a,0,n-1); end=clock(); runtime=runtime+((end-start)/CLK_TCK); } runtime=runtime/NTIMES; printf("\n Sorted elements are \n"); for(i=0;i<n;i++) printf("%d \n",a[i]); printf("Time taken for sorting is %lf seconds",runtime); getch(); }

Vous aimerez peut-être aussi