Knowledge in Data Structures & algorithms

INTRODUCTION TO DATA STRUCTURE (HASHING)

INTRODUCTION TO DATA STRUCTURE (HASHING)

INTRODUCTION TO DATA STRUCTURE (GRAPHS)

INTRODUCTION TO DATA STRUCTURE (GRAPHS)

INTRODUCTION TO DATA STRUCTURE (LINKED LIST)

INTRODUCTION TO DATA STRUCTURE (LINKED LIST)

INTRODUCTION TO DATA STRUCTURE (STRUCTURES)

INTRODUCTION TO DATA STRUCTURE (STRUCTURES)

Solutions of Algorithms book by CORMEN

Instructor’s Manual by Thomas H. Cormen Clara Lee Erica Lin to Accompany Introduction to Algorithms Second Edition

Linked List

#include<stdio.h> #include<stdlib.h> struct node{     int data;     struct node* next; }; //struct node* head; void insert(struct node** head,int x) {     struct node* temp=(struct node*)malloc(sizeof(struct node));     temp->data=x;     temp->next=*head;     *head=temp;     //return head; } void deleteList(struct node** head,int pos) {     struct node* temp,*prev;     int count=0;     temp=(*head);     while(temp!=NULL)     {         prev=temp;         temp=temp->next;                  count++;         if(count==pos)         {             if(temp->next==NULL)         {             prev->next=NULL;             break;         }             prev->next=temp->next;             break;         }     } } void reverse(struct node** head) {     struct node* current,*ptr=NULL,*next=NULL;     current=(*head);     while(current!=NULL)     {         next=current->next;         current->next=ptr;         ptr=current;         current=next;              }     (*head)=ptr; } void append(struct node** head_ref,int data) {     struct node* temp=(struct node*)malloc(sizeof(struct node));     struct node *last= *head_ref;     temp->data=data;     temp->next=NULL;          if((*head_ref) == NULL)     {         (*head_ref)= temp;         return;     }     while(last->next!= NULL)    {     last=last->next;   }     last->next=temp;   //temp->next=NULL;         } bool search(struct node* head, int x)  {      struct node* current = head;       while (current != NULL)      {          if (current->data == x)              return true;          current = current->next;      }      return false;  }  void print(node* head) {     while(head!=NULL)     {         printf("%d  ",head->data);         head=head->next;     }     printf("\n"); } int main() {   struct node* head=NULL;     int n,i,x;     scanf("%d",&n);     for(i=0;i<n;i++)     {         scanf("%d",&x);         append(&head,x);     }      //    deleteList(&head,3);     print(head);     reverse(&head);     print(head);     search(head, 21)? printf("Yes") : printf("No"); }

Stack Balancing

#include<bits/stdc++.h> using namespace std; bool balancing(string str) {     stack<char> s;     char x;          for(int i=0;i<str.length();i++)     {             if(str[i] == '(' || str[i]=='{' || str[i]=='[')             {                 s.push(str[i]);                              }             if(s.empty())             {                 return false;             }                          switch(str[i])             {                 case ')':                     x=s.top();                     s.pop();                     if (x=='{' || x=='[')                  return false;              break;             case '}':                 // Store the top element in b              x = s.top();              s.pop();              if (x=='(' || x=='[')                  return false;              break;             case ']':                 // Store the top element in c              x = s.top();              s.pop();              if (x =='(' || x == '{')                  return false;              break;          }      }         // Check Empty Stack      return (s.empty());  }            int main()  {      string expr = "{()})";         if (balancing(expr))          cout << "Balanced";      else         cout << "Not Balanced";      return 0;  }                                             

Selection SOrt

#include<iostream> using namespace std; int main() {     int a[100],i,j,temp,n;     cin>>n;     for(i=0;i<n;i++)     {         cin>>a[i];     }     for(i=0;i<n;i++)     {         int minvalue = a[i];         j = i;         for(i=j+1;i<n;i++)         {             if(a[i]>minvalue)             {                 temp=minvalue;                 minvalue=a[i];                 a[i]=temp;             }         }     }     for(i=0;i<n;i++)     {         cout<<a[i];     }      }

Infix To Postfix Using Stack - C Program

#include<stdlib.h> int stack[100]; int top=-1; void push(int x); int pop(); void push(int x) {     top++;     stack[top]=x; } int pop() {     return stack[top--]; } int main() {     char km[100];     char *e;     int n1,n2,n3,count=0;     printf("Enter The Expression");     scanf("%s",&km);     e=km;     while(*e!='\0')     {         if(isdigit(*e))         {             int num = *e - 48;             push(num);         }         else{         n1=pop();         n2=pop();         switch(*e)     {         case '+':         {             n3=n1+n2;             break;         }         case '-':         {             n3=n2-n1;             break;         }         case '*':         {             n3=n2*n1;                         break;         }         case '/':         {             n3=n2/n1;                         break;         }         }         push(n3);         }         e++;         }                         printf("\nThe result of expression %s  =  %d\n\n",km,pop());         }

Check For A Loop - LInked List

#include<stdio.h> struct node {     int data;     struct node* next; }; void checkloop(struct node **head_ref) {     struct node* fast=head_ref;     struct node* slow=head_ref;     while(fast!=NULL)     {         fast=fast->next;         if(fast->next!=NULL)         {             fast=fast->next;             slow=slow->next;         }         if(fast==slow)         {             printf("loop found");             return;         }     }         printf("no loop"); } int main() {     struct node* head=NULL;     struct node* first=NULL;     struct node* a=NULL;     struct node* b=NULL;     struct node* c=NULL;     head=(struct node*)malloc(sizeof(struct node));     first=(struct node*)malloc(sizeof(struct node));     a=(struct node*)malloc(sizeof(struct node));     b=(struct node*)malloc(sizeof(struct node));     c=(struct node*)malloc(sizeof(struct node));     head->data=7;     head->next=a;     a->data=90;     a->next=first;     first->data=1;     first->next=b;     b->data=34;     b->next=c;     c->data=78;     c->next=first;     checkloop(&head); }