Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Tuesday, April 7, 2015

[Data Structures - C] Circular Queue with Array in C Language

It is "Queue" with Array in C Programming language.
There are some functions get, put, printQue. They are important things.


#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int *Queue;
int max = 0;
int front,rear;
int command=0;
int data;
bool isFull = false;

void initQue(void);
void put(int);
bool get(int *data);
void printQue();

int main(int argc, const char * argv[]) {
    initQue();
    while(true) {
        printf("Que Program\n");
        printf("1. Put data\n");
        printf("2. Get data\n");
        printf("3. Print Que\n");
        printf("4. Exit Que\n");
        printf("Select your command : ");
        scanf("%d",&command);
        getchar();
        
        switch (command) {
            case 1:
                if (!isFull) {
                    printf("Enter data what you want to put :");
                    scanf("%d",&command);
                    getchar();
                    put(command);
                } else {
                    printf("Que is full. Please do get at least once");
                }
                break;
            case 2:
                if(get(&data)) {
                    printf("Getting data is %d\n",data);
                } else {
                    printf("Que is empty\n");
                }
                break;
            case 3:
                printQue();
                break;
            case 4:
                exit(0);
            default:
                break;
        }
        
    }
    return 0;
}

void initQue(void) {
    printf("Please enter volume of Que : ");
    scanf("%d",&max);
    getchar();
    Queue = (int *)malloc(sizeof(int)*max);
    front = 0;
    rear = 0;
    printf("Que is created with %d volume\n",max);
}

void put(int data) {
    if(!isFull) {
        Queue[rear++] = data;
    }
    
    if(rear>=max) {
        rear = 0;
    }
    
    if(front==rear) {
        isFull = true;
    }
    
}

bool get(int *data) {
    if(front==rear&&!isFull) {
        return false;
    }
    *data = Queue[front++];
    if(front>=max) {
        front=0;
    }
    isFull = false;
    return true;
}

void printQue() {
    int tempFront = front;
    
    printf("Front -> ");
    
    if (front==rear&&!isFull) {
        printf("Que is Empty.");
        return;
    }
    
    do {
        printf("%d -> ",Queue[tempFront++]);
        if(tempFront>=max) {
            tempFront = 0;
        }
    } while(tempFront!=rear);
        
    printf("Rear\n");
}


Wednesday, October 1, 2014

[Javascript Data Structures & Algorithms] List


Here is List data structure with js.
reference is Data Structures & Algorithms with JavaScript.
In the book, there is some error and needless codes. I fix and cut off that.

function List() {
  this.listSize = 0;
  this.pos = 0;
  this.dataStore = [];
  this.clear = clear;
  this.find = find;
  this.toString = toString;
  this.insert = insert;
  this.append = append;
  this.remove = remove;
  this.contains = contains;
  this.front = front;
  this.end = end;
  this.prev = prev;
  this.next = next;
  this.length = length;
  this.currPos = currPos;
  this.moveTo = moveTo;
  this.getElement = getElement;
}

function append(element) {
  this.dataStore[this.listSize++] = element;
}

function remove(element) {
  var foundAt = this.find(element);
  if (foundAt > -1) {
    this.dataStore.splice(foundAt,1);
    --this.listSize;
    return true;
  }
  return false;
}

function find(element) {
  for(var i=0; i<this.dataStore.length; ++i) {
    if(this.dataStore[i]==element) {
      return i;
    }
  }
  return -1;
}

function length() {
  return this.listSize;
}

function toString() {
  return this.dataStore;
}

function insert(element, after) {
  var insertPost = this.find(after);
  if(insertPost > -1 ) {
    this.dataStore.splice(insertPos+1,0, element);
    ++this.listSize;
    return true;
  }
  return false;
}

function clear() {
  delete this.dataStore;
  this.dataStore.length = 0;
  this.listSize = this.pos = 0;
}

function contains(element) {
  if(this.find(element)>-1) {
    return true;
  }
  return false;
}

function front() {
  this.pos = 0;
}

function end() {
  this.pos = this.listSize-1;
}

function prev() {
  if(this.pos>=0) {
    --this.pos;
  }
}

function next() {
  if(this.pos < this.listSize) {
    ++this.pos;
  }
}

function currPos() {
  return this.pos;
}

function moveTo(position) {
  this.pos = position;
}

function getElement() {
  return this.dataStore[this.pos];
}


var names = new List();

names.append("Carrot Carrot");
names.append("Flash Maestro");
names.append("Jake Song");

for(names.front(); names.currPos() < names.length(); names.next()) {
  print(names.getElement());
}

for(names.end(); names.currPos()>=0; names.prev()) {
  print(names.getElement());
}

It is realized with Array. But in js, array and list are very similar. It's just example. If you want to realize List, Make it with C, C++, Java something like that.

My next post is List with C.