C Code Examples: Data Structures and Algorithms

Classified in Computers

Written at on English with a size of 5.54 KB.

Recursive Binary Tree Traversals

Inorder Traversal:

void inorder(struct node *root)
{
  if(root != NULL)
  {
    inorder(root->left);
    printf("%d\t", root->data);
    inorder(root->right);
  }
}

Preorder Traversal:

void preorder(struct node *root)
{
  if(root != NULL)
  {
    printf("%d\t", root->data);
    preorder(root->left);
    preorder(root->right);
  }
}

Postorder Traversal:

void postorder(struct node *root)
{
  if(root != NULL)
  {
    postorder(root->left);
    postorder(root->right);
    printf("%d\t", root->data);
  }
}

Linked List Operations

Search

void search(struct node *head,int key)
{
  struct node *temp = head;
  while(temp != NULL)
  {
    if(temp->data == key)
      printf("key found");
    temp = temp->next;
  }
  printf("key not found");
}

Concatenation

void Concat(struct Node *first, struct Node *second)
{
  struct Node *p = first;
  while (p->next != NULL)
  {
    p = p->next;
  }
  p->next = second;
  second = NULL;
}

Doubly Linked List Operations

Insertion at the Front

void insertfront()
{
  ptr=createnode();
  if(start==NULL)
  {
    ptr->left=ptr->right=NULL;
    start=last=ptr;
  }
  else
  {
    ptr->left=NULL;
    ptr->right=start;
    start->left=ptr;
    start=ptr;
  }
}

Deletion at the End

void deleteend()
{
  temp=start;
  while(temp->right!=NULL)
  {
    temp=temp->right;
  }
  last=temp->left;
  last->right=NULL;
  temp->left=NULL;
  printf("element is deleted \n");
  free(ptr);
}

Stack Operations

#include<stdio.h>
int stk[10], ch, n, top=-1, item, i;
void push()
{
  if(top>=n-1)
  {
    printf("STACK is over flow\n");
  }
  else
  {
    printf(" Enter a value to be pushed:");
    scanf("%d", &item);
    top++;
    stk[top]=item;  
    printf(" pushed successfully\n:");
  }
}

void pop()
{
  if(top == -1)
  {
    printf("Stack is under flow\n");
  }
  else
  {
    printf("The popped elements is %d\n", stk[top]);
    top--;
  }
}
void display()
{
  if(top == -1)
  {
    printf(" STACK is empty \n");
  }
  else
  {
    printf("The elements in STACK \n");
    for(i=top; i>=0; i--)
      printf("%d\n" ,stk[i]);
  }
}
void main()
{
  printf("Enter the size of STACK\n");
  scanf("%d", &n);
  for(;;)
  {
    printf("1.PUSH 2.POP 3.DISPLAY \n");
    printf("Enter the Choice:\n");
    scanf("%d",&ch);
    switch(ch)
    {
      case 1: push(); break;
      case 2: pop(); break;
      case 3: display(); break;
    }
  }
}

String Operations

Compare Two Strings

int scomp(char s1[], char s2[])
{
  int i, j;
  if(slen(s1) != slen(s2))
  {
    return 0;
  }
  for(i=0; s1[i] != '\0'; i++)
  {
    if(s1[i] != s2[i])
    {
      return 0;
    }
  }
  return 1;
}

Concatenate Two Strings

int scat(char s1[], char s2[])
{
  int i, j;
  for(i = slen(s1), j=0; s2[j] != '\0'; i++, j++)
  {
    s1[i] = s2[j];
  }
  s1[i] = '\0';
  return 0;
}

Reverse a String

void reverse()
{
  char str[100], temp; int i, j = 0;
  printf("Enter The String: "); gets(str);
  i = 0;  j = strlen(str) - 1;  while (i < j)
  {
    temp = str[i]; str[i] = str[j]; str[j] = temp; i++; j--;
  }
  printf("\nReverse a String Is: %s\n\n", str);
}

Postfix Expression Evaluation

#include<stdio.h>
float compute(char symbol, float op1, float op2)
{
  switch (symbol)
  {
    case '+': return op1 + op2;
    case '-': return op1 - op2;
    case '*': return op1 * op2;
    case '/': return op1 / op2;
    case '$':
    case '^': return pow(op1,op2);
  }
}
void main()
{
  float s[20], res, op1, op2;
  int top, i;
  char postfix[20], symbol;
  printf("\nEnter the postfix expression:\n");
  scanf ("%s", postfix);
  top=-1;
  for (i=0; i<strlen(postfix) ;i++)
  {
    symbol = postfix[i];
    if(isdigit(symbol))
      s[++top]=symbol - '0';
    else
    {
      op2 = s[top--];
      op1 = s[top--];
      res = compute(symbol, op1, op2);
      s[++top] = res;
    }
  }
  res = s[top--];
  printf("\nThe result is : %f\n", res);
}

Circular Queue Operations

#include <stdio.h>
int cq[10], front = -1, rear = -1,item,ch,i,size;
void enQueue()
{
  if(front ==(rear+1)%SIZE)
  {
    printf("C Q IS OVERFLOW\n");
  }
  else
  {
    printf("enter element\n");
    scanf("%d",&item);
    if (front == -1)
      front = 0;
    rear = (rear + 1) % SIZE;
    cq[rear] = item;
    printf("Inserted\n");
  }
}
void deQueue()
{
  if(front == -1)
  {
    printf("CIRCULAR QUEUE IS UNDERFLOW\n");
  }
  else
  {
    printf("\n Deleted element -> %d \n", cq[front]);
    if (front == rear)
      front = rear = -1;
    else
      front = (front + 1) % SIZE;
  }
}
void display()
{
  if(front == -1)
  {
    printf("CIRCULAR QUEUE IS empty\n");
  }
  else
  {
    printf("Items are \n");
    for (i = front; i != rear; i = (i + 1) % SIZE)
    {
      printf("%d\n", cq[i]);
    }
    printf("%d\n", cq[i]);
  }
}
void main()
{
  for(;;)
  {
    printf("enter cq size !!!\n");
    scanf("%d",&size);
    printf("1.Insert 2.delete 3.display 4.Exit\n");
    printf("Enter your choice:\n");
    scanf("%d", &ch);
    switch(ch)
    {
      case 1:enQueue();break;
      case 2:deQueue();break;
      case 3:display();break;
    }
  }
}

...continue

Entradas relacionadas: