Skip to main content

Posts

Showing posts from May, 2010

Inorder, preorder, post order traversals using C

TRAVERSALS#include<stdio.h>#include<conio.h>typedef struct node{int data;struct node*left;struct node*right;}tree;tree* createtree();tree* insert(tree*,tree*);void preorder(tree*);void inorder(tree*);void postorder(tree*);void main(){int ch;tree*bt;clrscr();bt=createtree();printf("\n \tBINARY TREE TRAVERSAL \n");printf("\n \t1.traversals \n");printf("\n \t2.exit\n");printf("\n \tenter your choice \n");scanf("%d",&ch);if(bt==NULL){printf("\nbinary tree is empty\n");return;}switch(ch){case 1:printf("\n \t preorder traversal \n");preorder(bt);printf("\n \t inorder traversal \n");inorder(bt);printf("\n \t postorder traversal \n");postorder(bt);break;case 2:printf("\n exit");exit(0);}getch();free(bt);}tree* createtree(){char ch;tree *bt=NULL,*temp;do{temp=(tree*)malloc(sizeof(struct node));printf("\n enter the data\n");scanf("%d",&temp->data);temp-&…

Implementation of Singly Linked List

//PROGRAM TO IMPLEMENT SINGLY LINKED LIST\\#include<stdio.h>#include<conio.h>#include<alloc.h>#include<stdlib.h>struct node{int data;struct node *link;};void append(struct node **,int);void add_at_begin(struct node **,int);void del(struct node **,int);void in_middle(struct node **,int,int);int count(struct node *);void display(struct node *);void main(){int num,loc;char choice;struct node *p;p=NULL;do{clrscr();printf("PROGRAM TO IMPLEMENT SINGLY LINKED LIST\n");printf("---------------------------------------\n");printf("\n 1. Create/appending the list");printf("\n 2. Insert node at begining");printf("\n 3. Insert node in middle");printf("\n 4. Deleting a node");printf("\n 5. Counting the no. of nodes");printf("\n 6. Displaying the list");printf("\n 7. Exit");oper:gotoxy(1,15);printf(" ");gotoxy(1,11);printf("\n Enter your choice : ");choice=getch();swit…

Queue implementation using C

The readers are requested to check for any errors in the program (as the program is typed and copied from MS Word)QUEUE IMPLEMENTATION USING ARRAYS#include<stdio.h>#include<conio.h>void create(void);void display(void);void queue_in(void);void queue_out(void);void front_rear(void);int front,rear;int q[25];void create(){int n;printf("Enter the number of elements in the queue \n");scanf("%d",&n);printf("Enter the elements \n");for(rear=0;rear<n;rear++){scanf("%d",&q[rear]);}front=0;}void display(){if(rear!=0){printf("The elements in queue \n");for(front=0;front<rear;front++){printf("%d",q[front]);}front=0;}else if(rear==0)printf("The queue is empty \n");else if(rear==25)printf("The queue is full \n");}void front_rear(){if(rear!=0){printf("the front element is : %d \n",q[front]);printf("the rear element is : %d \n",q[rear-1]);}elseprintf("Queue is empty \n&quo…

Stack Implementation using C

The following two programs for the stack implementation using Array and Linked List, the readers are requested to report if there are any errors (syntax or semantic)//STACK IMPLEMENTATION USING ARRAYS\\#include<stdio.h>#include<conio.h>void create(void);void push(0void);void pop(void);void display(void);void topelement(void);int a[25];int top;void create(){int i;printf("Enter the number of elements in the stack \n");scanf("%d",&top);printf("Enter the elements \n");for(i=0;i<top;++i){scanf("%d",&a[i]);}return;}void display(){int i;if((top!=0) && (top!=25)){printf("The elements in the stack are \n");for(i=top-1;i>=0;--i)printf("%d",a[i]);getch();}}void push(){if(top==25){printf("The stack is full \n");}else{printf("Enter the element \n");scanf("%d",&a[top]);top++;}return;}void pop(){if(top==0){printf("the stack is empty \n");}else{printf("The pop…