data staructure assignment

34
DATA STARUCTURE ASSIGNMENT NAME: - SUMEDH HEDAOO BTECH (CSE) 2 nd SEM ROLL NO: - 07016403214

Upload: sumedh-hedaoo

Post on 07-Nov-2015

228 views

Category:

Documents


2 download

DESCRIPTION

CODINGS FOR DATA STRUCTURE

TRANSCRIPT

  • DATA STARUCTURE

    ASSIGNMENT

    NAME: - SUMEDH HEDAOO

    BTECH (CSE) 2nd SEM

    ROLL NO: - 07016403214

  • ACKNOWLEDGEMENT

    Every project big or small is successful largely due to the effort of a number of wonderful people who have always given their valuable advice or lent a helping hand. I sincerely appreciate the inspiration; support and guidance of all those people who have been instrumental in making this project a success. I, Your Name, the student of GGSIPU (BTECH-CSE), am extremely grateful to COMPANY for the confidence bestowed in me and entrusting my project entitled At this juncture I feel deeply honored in expressing my sincere thanks to ANURAG AGGRAWAL for making the resources available at right time and providing valuable insights leading to the successful completion of my project. I express my gratitude to College for arranging the summer training in good schedule. I also extend my gratitude to my Project Guide who assisted me in compiling the project. I would also like to thank all the faculty members of GGSIPU for their critical advice and guidance without which this project would not have been possible. Last but not the least I place a deep sense of gratitude to my family members and my friends who have been constant source of inspiration during the preparation of this project work.

  • INDEX S.no TITLE 1. Program to implement a stack using array 2. Program to implement a singly linked list 3. Program to create doubly linked list 4. Program to add two big numbers 5. Program to create binary search tree 6. Program to implement mini heap 7. Program to implement merge sort 8. Program to implement insertion sort 9. Program to implement quick sort 10. Program to implement Breadth first

    search for graphs 11. Program to implement Depth first search

    for graphs

  • Program to implement a stack using array

    #include

    #include

    #define max 30

    void push();

    void pop();

    void display();

    int stack[max],top=-1,item,i,choice;

    void main()

    { clrscr();

    do

    {

    printf("\n\t\tMENU");

    printf("\n1.DISPLAY");

    printf("\n2.push");

    printf("\n3.pop");

    printf("\n4.exit");

    printf("\nchoice is:");

    scanf("%d",&choice);

    switch(choice)

    { case 1:display();

    break;

    case 2:push();

    break;

    case 3:pop();

    break;

    case 4:exit(0);

    default:printf("wrong choice");

    }

    getch();

    }while(choice!=4);

  • } void display()

    { if(top==-1)

    printf("empty!!!");

    else

    { for(i=top;i>=0;i--)

    { printf("%d",stack[i]);

    }

    }

    }

    void push()

    { if(top==max-1)

    printf("overflow!!!!!");

    else

    { printf("the number to be pushed is:");

    scanf("%d",&item);

    top++;

    item=stack[top];

    }

    }

    void pop()

    { if(top==-1)

    printf("underflow!!!!!");

    else

    { item=stack[top];

    top--;

    printf("the popped number is:%d",item);

    }

    }

  • Program to implement a singly linked list

    #include

    #include

    #include

    struct node

    { int info;

    struct node *ptr;

    }*temp,*top1;

    struct node *top=NULL;

    void insert();

    void del();

    void display();

    int i,choice,data;

    void main()

    { clrscr();

    do

    { printf("\n\t\tMENU");

    printf("\n1.DISPLAY");

    printf("\n2.insert");

    printf("\n3.remove");

    printf("\n4.exit");

    printf("\nchoice is:");

    scanf("%d",&choice);

    switch(choice)

    { case 1:display();

    break;

    case 2:insert();

    break;

    case 3:del();

    break;

    case 4:exit(0);

  • default:printf("wrong choice");

    }

    getch();

    }while(choice!=4);

    }

    void insert()

    { printf("the number you want to insert:");

    scanf("%d",&data);

    if(top==NULL)

    { top=(struct node*)malloc(sizeof(struct node));

    top->ptr=NULL;

    top->info=data;

    }

    else

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

    temp->ptr=top;

    temp->info=data;

    top=temp;

    }

    }

    void display()

    { top1=top;

    if(top1==NULL)

    { printf("empty!!!");

    }

    while(top1!=NULL)

    { printf("%d",top1->info);

    top1=top1->ptr;

    }}

    void del()

    { if(top==NULL)

  • { printf("underflow!!!!!");

    }

    else

    { top1=top;

    top1=top1->ptr;

    printf("popped value is:%d",top->info);

    free(top);

    top=top1;

    }

    }

  • Program to create doubly linked list

    #include

    #include

    struct node

    { struct node *previous;

    struct node *next;

    int data;

    }*head,*rear,*temp,*flag,*head1,*rear1,*temp1;

    int value;

    void insert_beg()

    { printf("the value you want to insert is:");

    scanf("%d",&value);

    if(head==NULL)

    { head=(struct node*)malloc(sizeof(struct node));

    head->data=value;

    head->next=NULL;

    head->previous=NULL;

    }

    else

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

    temp->previous=NULL;

    temp->data=value;

    temp->next=head;

    head=temp;

    }

    }

    void insert_end()

    { printf("the value you want to insert is:");

    scanf("%d",&value);

    if(head==NULL)

    { head=(struct node*)malloc(sizeof(struct node));

  • head->data=value;

    head->next=NULL;

    head->previous=NULL;

    }

    else

    { flag=(struct node*)malloc(sizeof(struct node));

    flag->data=value;

    flag->next=NULL;

    flag->previous=rear;

    rear=flag;

    }

    }

    void del_beg()

    { if(head==NULL && rear==NULL)

    { printf("underflow!!!!");

    }

    else

    { head1=head;

    head1=head1->next;

    printf("the value deleted is:%d",head->data);

    free(head);

    head=head1;

    }

    }

    void del_end()

    { if(head==NULL && rear==NULL)

    { printf("underflow!!!!!");

    }

    else

    { rear1=rear;

    rear1=rear1->previous;

  • printf("the value deleted is:%d",rear->data);

    free(rear);

    rear=rear1;

    }

    }

    void display()

    { temp1=head;

    if(temp1==NULL)

    { printf("empty!!!!!");

    }

    while(temp1!=NULL)

    { printf("%d",temp1->data);

    temp1=temp1->next;

    }

    printf("%d",rear->data);

    }

    void main()

    { int choice;

    clrscr();

    do

    { printf("\n\t\tMENU");

    printf("\n1.DISPLAY");

    printf("\n2.insert at beginning");

    printf("\n3.insert at end");

    printf("\n4.delete from beginning");

    printf("\n5.delete from end");

    printf("\n6.exit");

    printf("\nchoice is:");

    scanf("%d",&choice);

    switch(choice)

    { case 1:display();

  • break;

    case 2:insert_beg();

    break;

    case 3:insert_end();

    break;

    case 4:del_beg();

    break;

    case 5:del_end();

    break;

    case 6:exit(0);

    default:printf("wrong choice");

    }

    getch();

    }while(choice!=6);

    }

  • Program to add two big numbers

    #include

    #include

    int i,n1,n2,A1[200],A2[200],sum[200],j,k,A3[200];

    void main()

    { clrscr();

    printf("no. of digits in first number:");

    scanf("%d",&n1);

    printf("\nenter the number:\n");

    for(i=0;i9)

    { if(j>0)

    { sum[j]=sum[j]%10;

    carry=1;

    }

    }

    }

  • printf("\nthe sum is:");

    for(j=0;j=n2-1)

    { j=i-(n1-n2);

    A3[i]=A2[j];

    }

    else

    A3[i]=0;

    }

    for(i=n1-1;i>=0;i--)

    { j=i;

    sum[j]=A1[i]+A3[i]+carry;

    carry=0;

    if(sum[j]>9)

    { if(j>0)

    { sum[j]=sum[j]%10;

    carry=1;

    }

    }

    }

    printf("\nthe sum is:");

    for(j=0;j

  • getch();

    }

    if(n2>n1)

    { int carry=0;

    for(i=n2-1;i>=0;i--)

    { if(i>=n1-1)

    { j=i-(n2-n1);

    A3[i]=A2[j];

    }

    else

    A3[i]=0;

    }

    for(i=n2-1;i>=0;i--)

    { j=i;

    sum[j]=A1[i]+A3[i]+carry;

    carry=0;

    if(sum[j]>9)

    { if(j>0)

    { sum[j]=sum[j]%10;

    carry=1;

    }

    }

    }

    printf("\nthe sum is:");

    for(j=0;j

  • Program to create binary search tree

    #include

    #include

    struct btnode

    { int value;

    struct btnode *l;

    struct btnode *r;

    }*root = NULL, *temp = NULL, *t2, *t1;

    void delete1();

    void insert();

    void delete();

    void inorder(struct btnode *t);

    void create();

    void search(struct btnode *t);

    void search1(struct btnode *t,int data);

    int smallest(struct btnode *t);

    int largest(struct btnode *t);

    int flag = 1;

    void main()

    { int ch;

    printf("\nOPERATIONS ---");

    printf("\n1 - Insert an element into tree\n");

    printf("2 - Delete an element from the tree\n");

    printf("3 - Inorder Traversal\n");

    printf("4 - Exit\n");

    while(1)

    { printf("\nEnter your choice : ");

    scanf("%d", &ch);

    switch (ch)

    {case 1:

    insert();

  • break;

    case 2:

    delete();

    break;

    case 3:

    inorder(root);

    break;

    case 4:

    exit(0);

    default :

    printf("Wrong choice, Please enter correct choice ");

    break;

    }

    }

    }

    void insert()

    { create();

    if (root == NULL)

    root = temp;

    else

    search(root);

    }

    void create()

    { int data;

    printf("Enter data of node to be inserted : ");

    scanf("%d", &data);

    temp = (struct btnode *)malloc(1*sizeof(struct btnode));

    temp->value = data;

    temp->l = temp->r = NULL;

    }

    void search(struct btnode *t)

  • { if ((temp->value > t->value) && (t->r != NULL)) /* value more than root node value insert at right */

    search(t->r);

    else if ((temp->value > t->value) && (t->r == NULL))

    t->r = temp;

    else if ((temp->value < t->value) && (t->l != NULL)) /* value less than root node value insert at left */

    search(t->l);

    else if ((temp->value < t->value) && (t->l == NULL))

    t->l = temp;

    }

    void inorder(struct btnode *t)

    { if (root == NULL)

    { printf("No elements in a tree to display");

    return;

    }

    if (t->l != NULL)

    inorder(t->l);

    printf("%d -> ", t->value);

    if (t->r != NULL)

    inorder(t->r);

    }

    void delete()

    { int data;

    if (root == NULL)

    { printf("No elements in a tree to delete");

    return;

    }

    printf("Enter the data to be deleted : ");

    scanf("%d", &data);

    t1 = root;

    t2 = root;

    search1(root, data);

  • }

    void search1(struct btnode *t, int data)

    { if ((data>t->value))

    { t1 = t;

    search1(t->r, data);

    }

    else if ((data < t->value))

    { t1 = t;

    search1(t->l, data);

    }

    else if ((data==t->value))

    { delete1(t);

    }

    }

    void delete1(struct btnode *t)

    { int k;

    if ((t->l == NULL) && (t->r == NULL))

    { if (t1->l == t)

    { t1->l = NULL;

    }

    else

    { t1->r = NULL;

    }

    t = NULL;

    free(t);

    return;

    }

    else if ((t->r == NULL))

    { if (t1 == t)

    { root = t->l;

    t1 = root;

  • }

    else if (t1->l == t)

    { t1->l = t->l;

    }

    else

    { t1->r = t->l;

    }

    t = NULL;

    free(t);

    return;

    }

    else if (t->l == NULL)

    { if (t1 == t)

    { root = t->r;

    t1 = root;

    }

    else if (t1->r == t)

    t1->r = t->r;

    else

    t1->l = t->r;

    t == NULL;

    free(t);

    return;

    }

    else if ((t->l != NULL) && (t->r != NULL))

    { t2 = root;

    if (t->r != NULL)

    { k = smallest(t->r);

    flag = 1;

    }

    else

  • { k =largest(t->l);

    flag = 2;

    }

    search1(root, k);

    t->value = k;

    }

    }

    int smallest(struct btnode *t)

    { t2 = t;

    if (t->l != NULL)

    { t2 = t;

    return(smallest(t->l));

    }

    else

    return (t->value);

    }

    int largest(struct btnode *t)

    { if (t->r != NULL)

    { t2 = t;

    return(largest(t->r));

    }

    else return(t->value);

    }

  • Program to implement Mini-heap

    #include

    #include

    using namespace std;

    void min_heapify(int *a,int i,int n)

    { int j, temp;

    temp = a[i];

    j = 2 * i;

    while (j = a[j])

    { a[j/2] = a[j];

    j = 2 * j;

    }

    }

    a[j/2] = temp;

    return;

    }

    void build_minheap(int *a, int n)

    { int i;

    for(i = n/2; i >= 1; i--)

    { min_heapify(a,i,n);

    }

    }

    int main()

    { int n, i, x;

    coutn;

  • int a[20];

    for (i = 1; i

  • Program to implement Merge Sort

    #include

    void mergeSort(int [], int, int, int);

    void partition(int [],int, int);

    int main()

    { int list[50];

    int i, size;

    printf("Enter total number of elements:");

    scanf("%d", &size);

    printf("Enter the elements:\n");

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

    { scanf("%d", &list[i]);

    }

    partition(list, 0, size - 1);

    printf("After merge sort:\n");

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

    { printf("%d ",list[i]);

    }

    return 0;

    }

    void partition(int list[],int low,int high)

    { int mid;

    if(low < high)

    { mid = (low + high) / 2;

    partition(list, low, mid);

    partition(list, mid + 1, high);

    mergeSort(list, low, mid, high);

    }

    }

    void mergeSort(int list[],int low,int mid,int high)

  • { int i, mi, k, lo, temp[50];

    lo = low;

    i = low;

    mi = mid + 1;

    while ((lo

  • Program to implement Insertion Sort

    #include

    #define MAX 7

    void insertion_sort(int *);

    void main()

    { int a[MAX], i;

    printf("enter elements to be sorted:");

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

    { scanf("%d", &a[i]);

    }

    insertion_sort(a);

    printf("sorted elements:\n");

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

    { printf(" %d", a[i]);

    }

    }

    void insertion_sort(int * x)

    { int temp, i, j;

    for (i = 1;i < MAX;i++)

    { temp = x[i];

    j = i - 1;

    while (temp < x[j] && j >= 0)

    { x[j + 1] = x[j];

    j = j - 1;

    }

    x[j + 1] = temp;

    }

    }

  • Program to implement Quick Sort

    #include

    #include

    #define MAX 100

    void random_shuffle(int arr[])

    { srand(time(NULL));

    int i, j, temp;

    for (i = MAX - 1; i > 0; i--)

    { j = rand()%(i + 1);

    temp = arr[i];

    arr[i] = arr[j];

    arr[j] = temp;

    }

    }

    void swap(int *a, int *b)

    { int temp;

    temp = *a;

    *a = *b;

    *b = temp;

    }

    int partion(int arr[], int p, int r)

    { int pivotIndex = p + rand()%(r - p + 1); //generates a random number as a pivot

    int pivot;

    int i = p - 1;

    int j;

    pivot = arr[pivotIndex];

    swap(&arr[pivotIndex], &arr[r]);

    for (j = p; j < r; j++)

    { if (arr[j] < pivot)

    { i++;

    swap(&arr[i], &arr[j]);

  • }

    }

    swap(&arr[i+1], &arr[r]);

    return i + 1;

    }

    void quick_sort(int arr[], int p, int q)

    { int j;

    if (p < q)

    { j = partion(arr, p, q);

    quick_sort(arr, p, j-1);

    quick_sort(arr, j+1, q);

    }

    }

    int main()

    { int i;

    int arr[MAX];

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

    arr[i] = i;

    random_shuffle(arr); //To randomize the array

    quick_sort(arr, 0, MAX-1); //function to sort the elements of array

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

    printf("%d \n", arr[i]);

    return 0;

    }

  • Program to implement Breadth First search for

    Graphs

    #include

    #include

    using namespace std;

    int c = 0, t = 0;

    struct node_info

    { int no;

    int st_time;

    }*q = NULL, *r = NULL, *x = NULL;

    struct node

    { node_info *pt;

    node *next;

    }*front = NULL, *rear = NULL, *p = NULL, *np = NULL;

    void push(node_info *ptr)

    { np = new node;

    np->pt = ptr;

    np->next = NULL;

    if (front == NULL)

    { front = rear = np;

    rear->next = NULL;

    }

    else

    { rear->next = np;

    rear = np;

    rear->next = NULL;

    }

    }

    node_info *remove()

    { if (front == NULL)

  • { printf("empty queue\n");

    }

    else

    { p = front;

    x = p->pt;

    front = front->next;

    delete(p);

    return(x);

    }

    }

    void bfs(int *v,int am[][7],int i)

    { if (c == 0)

    { q = new node_info;

    q->no = i;

    q->st_time = t++;

    printf("time of visitation for node "st_time = t++;

    printf("time of visitation for node %d",r->no,":%d",r->st_time");

    v[j] = 1;

    push(r);

    }

  • }

    remove();

    if (c no);

    }

    int main()

    { int v[7], am[7][7];

    for (int i = 0; i < 7; i++)

    v[i] = 0;

    for (int i = 0; i < 7; i++)

    { printf("enter the values for adjacency matrix row:%d",i+1);

    for (int j = 0; j < 7; j++)

    { scanf("%d",&am[i][j];

    }

    }

    bfs(v, am, 0);

    getch();

    }

  • Program to implement Depth First Search for

    Graphs

    #include

    #include

    using namespace std;

    int c = 0;

    struct node

    { char data;

    int st_time, lv_time;

    }*p = NULL, *r = NULL;

    struct stack

    { node *pt;

    stack *next;

    }*top = NULL, *q = NULL, *np= NULL;

    void push(node *ptr)

    { np = new stack;

    np->pt = ptr;

    np->next = NULL;

    if (top == NULL)

    { top = np;

    }

    else

    { np->next = top;

    top = np;

    }

    }

    node *pop()

    { if (top == NULL)

    { printf("underflow\n");

    }

  • else

    { q = top;

    top = top->next;

    return(q->pt);

    delete(q);

    }

    }

    void create(int a[], int b[][7], int i, int j)

    { c++;

    p = new node;

    printf("enter data for new node\n");

    scanf("%d",&p->data;

    p->st_time = c;

    printf("start time for %d",p->data," is %d",c);

    a[i] = 1;

    push(p);

    while (j < 7)

    { if ((b[i][j] == 0) || (b[i][j] == 1 && a[j] == 1))

    { j++;

    }

    else if (b[i][j] == 1 && a[j] == 0)

    { create(a,b,j,0);

    }

    }

    r = pop();

    printf("node popped\n");

    c++;

    printf("leave time for %d",r->data," is %d",c);

    return;

    }

    int main()

  • { int a[7];

    for (int i = 0; i < 7; i++)

    { a[i] = 0;

    }

    int b[7][7];

    printf("enter values for adjacency matrix");

    for (int i = 0 ; i < 7 ; i++ )

    { printf("enter values for %d",(i+1)," row");

    for (int j = 0; j < 7; j++)

    { scanf("%d",&b[i][j]);

    }

    }

    create(a,b,0,0);

    getch();

    }