Kategori arşivi: c

Linked List Addition In Order, C Language (automatically sorted )

Hi guys ,

Today ı will explain the AddInOrder function which is used for inserting item to linkedlist which will be  automatically sorted.

First of all , my function has two parameters.One of them is the list ı want to add new item and the other one is the data I desire to add. Function should return the new list .

linkedlist* AddinOrder(linkedlist* list, int data);

I am creating new item.İt will store the new data.

 linkedlist* newitem = malloc(sizeof(linkedlist));

    newitem->idata = data;
    newitem->pNext = NULL;

Then ı am checking if the list is NULL or the new data is smaller than the old one , Newitem should be the first element and second one will be the list . Because list would be sorted.

Here is the first If Visually.

if (temp == NULL || newitem->idata <= temp->idata) {
        newitem->pNext = temp;
        return newitem;
 }

first if

The other important part of code is While loop.In this loop we are defining ,where are we gonna add newitem on linkedlist.To do that, I have two while conditions. First temp->pnext != NULL ,  becouse if it will be equal the NULL , that’s mean we are the end of the List.There is no other adress to go on list. Secondly we are trying to find first bigger item than our new data.When ı see that data ,I have to stop going next linkedlist adress.Until these termination conditions are not supported , ı will keep going to the next adress.

 while (temp->pNext != NULL && newitem->idata >= temp->pNext->idata) {
        temp = temp->pNext;
    }

After while loop Finally ı found where ı will add item,Then  I have to make connections between the list and new item.

  newitem->pNext = temp->pNext;
    temp->pNext = newitem;

second

 

       Right now our function is ready.You can easly use it to add new element to linkedlist.

İf you want to compile this function , I am writing the function with all parts of it.

linkedlist* AddinOrder(linkedlist* list, int data) {

    linkedlist* temp = list
    linkedlist* newitem = malloc(sizeof(linkedlist));

    newitem->idata = data;
    newitem->pNext = NULL;

    if (temp == NULL || newitem->idata <= temp->idata) {
        newitem->pNext = temp;
        return newitem;
    }

    while (temp->pNext != NULL && newitem->idata >= temp->pNext->idata) {
        temp = temp->pNext;
    }

    newitem->pNext = temp->pNext;
    temp->pNext = newitem;

    return list;

}

You can see the result of function. I added random items to Linkedlist but when ı printed them , ı saw they are already sorted.

İf you want to calculate Big(0) of this function it will be “n” becouse there is only one loop which depends on item count.The other calculations has constant execution time. So BigO = T(n) = O(n)

Thanks for your time.

result

Decimal to Binary Conversion in C using Stack

Hi guys , today I will try to explain how we can convert decimal to binary in C programing language with using Stack.In addition I encoded some functions which are useful according to me.

NOTE : I am very thankful to my professor who is ‘Mehmet Ali Aksoy Tüysüz’ in order to he gave me Data Structures lecture.

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

typedef struct tagStack{
int* pData;               // Stores actual data
int sp;                      // Points to add/remove point
int capacity;             // Capacity of the array(Stack Data)
int item_count;         // Actually stored item count
}Stack;

#define SIZE 16 // I am gonna use this size to create stack for convert operation .

typedef int BOOL;  // Just to be exactly sure about Compiler includes BOOl type

#define TRUE 1
#define FALSE 0

/*

Right Now I will create stack.That’s why ı need to know how much space we need.So ı will get size like parameter of CreateStack Function.Then Function return the stack itself.

*/

Stack* CreateStack(int size)
{
Stack* pStack;

pStack = malloc(sizeof(Stack));

// You should check  the memory be taken or not ? 

if (NULL == pStack) {
fprintf(stderr, “Cannot allocate memory for Stack!!!\n”);
exit(EXIT_FAILURE);                                                                                           }
//After checking , you can take memory and create stack

pStack->pData = malloc(sizeof(int)* size);
if (NULL == pStack->pData) {
fprintf(stderr, “Cannot allocate memory for Stack array!!!\n”);
exit(EXIT_FAILURE);
}

pStack->capacity = size;
pStack->item_count = 0;
pStack->sp = 0;

return pStack;
}

/*

İf I take the space from memory , I should give it back.İf I would not do that,The segmentation fault might be created.

*/

void DestroyStack(Stack* pStack)
{
free(pStack->pData);
free(pStack);
}

/*

I encoded Full and Empty functions.They will allow me to know is there a space in stack or not.

*/

BOOL IsFull(Stack* pStack)
{
if (pStack->capacity == pStack->item_count)
return TRUE;
else
return FALSE;
}

BOOL IsEmpty(Stack* pStack)
{
if (0 == pStack->item_count)
return TRUE;
else
return FALSE;

}

/*

Push function has simple logic.I need a list to add data.So ı am taking list and data as a parameter of function . Return type is Bool.İf ı can add data to list, program will return true, other condition false will be the return value.

*/

BOOL Push(Stack* pStack, int toAdd)
{
if (IsFull(pStack))
return FALSE;
else {
pStack->pData[pStack->sp] = toAdd;
pStack->item_count++;
pStack->sp++;
return TRUE;
}
}

/*

Pop function simply extract the last value of stack.Thats why ı need list to remove data.I am taking it as parameter of function.After that if mission is completed , function return true , other case it will return false.

*/

int Pop(Stack* pStack)
{
int data, place;

place = pStack->sp – 1;
data = pStack->pData[place];
pStack->item_count -= 1;
pStack->sp–;
return data;
}

/*

Right now we get the major function ı wrote.I will summarize program quickly.İf we want to convert decimal to binary, we simply divide the decimal number in two and pushes remainder to stack  until division become zero.After that pop every value one by one and wrote them to int array.Last number is not being calculated so I pushed it the array firstly. 

*/

int* DecimaltoBinary(int number)
{

Stack* stack;
int *counter = malloc(sizeof(int)*SIZE ) ;
int i = 0;
int value = 0 ;
stack = CreateStack(SIZE);

while (number > 1) {
value = (number) % 2;
Push(stack, value);
number = number / 2;
}

counter[0] = number;
for (i = 1; IsEmpty(stack) == FALSE; i++){
counter[i] = Pop(stack);
}

return counter;

}

/*

This is print functions. Binary numbers basicly just includes “0” and “1”. So if the value in the string are not like them , it means we are end of the operation.Thats’s why ı am displaying numbers until values are different than “0” and “1”.

*/

void printBinary(int* binarynum){

int i = 0;
printf(“The binary number : \n”);
while (binarynum[i] == 0 || binarynum[i] == 1){
printf(“%d”, binarynum[i]);
i++;
}
printf(“\n\n”);
}

/*

To display all elemets of stack , ı am using IsEmpty function . As long As stack includes element, This function will display them one by one from Last to first direction .

*/

void PrintStack(Stack* stack) {

int value;
while (IsEmpty(stack) == FALSE)
{
value = Pop(stack);
printf(“%d”,value);
}
printf(“\n”);
}

/*

At first part, Program will convert decimal to binary until user press button which is different than “1”.At second part , Program creates stack with 10 size and initialize it 0 to 9 .After that it displays the elements.

*/

int main(int argc, char** argv)

{
Stack* stack;
int *binaryArray;
int decimalnumber;
int i = 1;

while (i == 1)
{

printf(“Please enter decimal number to convert binary : \n”);
scanf(“%d”, &decimalnumber);
binaryArray = DecimaltoBinary(decimalnumber);
printBinary(binaryArray);
printf(“———————————-\n”);
printf(“Press 1 for another convertion  or for not press 2..\n\n”);
scanf(“%d”,&i);
printf(“\n”);
}
printf(“———————————-\n”);
printf(“\nProgram creating 10 size Stack… \n”);
printf(“\nProgram initializing Stack 0 to 9 …\n”);
stack = CreateStack(10);

for (i = 0; i < 10; i++) {
Push(stack, i);
}

PrintStack(stack);

return EXIT_SUCCESS;

}

stackblog

GDG ISTANBUL 2014

Tarih : 18 Ocak Cumartesi Saat : 13.00 - 17.00

Tarih : 18 Ocak Cumartesi
Saat : 13.00 – 17.00

Merhaba  arkadaşlar , Google Glass’ın da anlatılacağı Gcd 2014 etkinliğini sizinle paylaşmak istiyorum. Original mail ve de katılım linkleri aşağıdadır. Şimdiden iyi eğlenceler iyi çalışmalar 🙂
Dolu dolu geçen 2013’te sizlerle her ay yaptığımız etkinliklerde çok şey paylaştık. Bizleri yalnız bırakmadığınız için teşekkürler. Ve yine söz verdiğimiz gibi 2014’te de her ayın 3. cumartesi günü buluşuyoruz. Bu ay ki etkinlikte neler mi var?
– Coding PHP with Lavarel, Halil Kaya
– Webcar, Muharrem Taç
– Google Glass, Murat Yener, Tahsin Dane
Her ay ki etkinliklerimizden haberdar olmak için GDG Istanbul G+ sayfasını ve Twitter hesabını takip etmeyi unutmayın. Etkinliklerde sunum yapmak, konu önermek, etkinlik organizasyonu ve her türlü fikir için etkinlik günü salonda kosturan tiplerle iletisim kurabilirsiniz.Etkinliğe katılım durumunuzu
https://plus.google.com/u/0/events/c6pdv0sqf43blj7q3akc2d2p52g?cfem=1
sayfasından belirtiniz

Twitter: @GDGIstanbul

 

BEST PROGRAMMING LANGUAGES

With thousands of programming languages out there, it can be daunting to find a language to start with and a good course that assumes no prior knowledge. This post highlights programming languages that are good for beginning programmers and some resources to get started.

progLanguages

With thousands of programming languages out there, it can be daunting to find a language to start with and a good course that assumes no prior knowledge. Especially if you are someone who is busy and wants to learn on their own time and don’t have the flexibility to take an in-person class, getting started with programming can be difficult. This post highlights programming languages that are good for beginning programmers and some resources to get started.

For those with no experience

These courses have been designed for people who have little or no programming experience.

C

C is one of the most widely used programming languages and often used as an introduction to programming. It has influenced many languages that came after it, and knowledge of C will make learning later languages, such as Objective-C (used by Apple), easier. It influences many later languages you could want to learn, so starting with C will give you a deeper understanding of how computers work.

Java

Java is a higher level language which is designed to be compatible with any operating system. It has similar syntax to C and C++. It’s a great programming language to start with because it is widely used and practical, however it won’t give you as deep of an understanding of computer operation as a lower level language like C will.

C++

C++ bridges the gap between a language like C and Java as it has features of both low-level and high-level languages. It’s another commonly used language that has a wide range of uses and compatibility. It’s based off of C and adds object-oriented features. It has also influenced many other languages such as C# and Java.

Python

Python is a language that was designed with human readability in mind. Because of this, it doesn’t take as much code to execute programs as other languages. It’s a great, easy way to learn recurring concepts in computer science and has real world use in the creation of scripts.

Ruby

Ruby has similar function to Python but is less readable. It’s more object-oriented than Python and is similarly designed with simplicity in mind. It has many applications, but is most often used for web applications.

HTML and CSS

HTML and CSS are used for webpage design. While these languages won’t really help pave the way for learning more traditional programming languages, they are essential for webpage design. HTML (HyperText Markup Language) is a “markup language” which allows you to put content into a webpage whereas CSS (Cascading Style Sheets), is used to format and define the layout of a page.

MIT App Inventor for Android

If you aren’t interested in programming as a profession (at least at the moment) it may be worth looking at using the MIT App Inventor for Android. It requires no coding, but will teach you how programmers think and provide knowledge on some concepts in computing. Plus, you’ll end up being able to make Android apps once you’ve mastered it!

What’s next?

If you already have knowledge of another programming language then these are great follow-up languages.

C#

C# is primarily used for Windows applications in the .NET Framework. Learning C# is easy if you have experience in C, C++, or Java. The syntax is similar. It’s popularity has been increasing as C# is used for third-party apps on Windows 8 or Windows Phone.

Objective-C

Objective-C is primarily used for Apple’s operating systems, OSX (for Macs) and iOS (for iPhone and iPad). If you are looking to develop for Mac, Objective-C is the way to go. Apple provides lots of support for learning Objective-C through their developer program.

Javascript

Javascript (little relation to Java) is a common language used to make webpages more dynamic. With a syntax similar to C, it doesn’t require a lot of effort to set up as it’s built into web browsers. It’s also used in other applications such as PDFs.

PHP

PHP is another language often used for web development, although it works well as a general-purpose language as well. PHP can be implemented directly into HTML. Those looking to learn PHP should already know HTML, CSS, and Javascript.

Where to learn online?

If you are just beginning to learn, we recommend that you stick to one language until you are extremely comfortable with it. Once you’ve picked a language, check out these resources to find courses:

OpenSesame

OpenSesame is a corporate elearning course seller that hosts content from a variety of sellers. It’s the best option if you would like to have your employees learn and track them through your own learning management system. If you are an individual however, they allow for the purchase of single licenses and a learning management system is not required. They offer courses in all the languages listed on this page and more from InfiniteSkills, Learntoprogram.tv, Webucator, Stone River, Compuworks, Pearson and more.

CodeAcademy

CodeAcademy offers free in-browser courses that require little set-up and is very user-friendly. The courses are very interactive and offer courses in Javascript, jQuery, PHP, Python and Ruby.

TreeHouse

TreeHouse is a paid service ($25-$49/month) that allows you to take courses in HTML, CSS, jQuery, JavaScript, Ruby, Ruby on Rails, WordPress, PHP, iOS and Android. Similar to CodeAcademy, it focuses on interactivity and allows you to learn in the browser.

C de Binary Search

Arkadaşlar Binary Search algoritmasını  kullandığım kod parçasını main ile birlikte paylaşıyorum.İyi çalışmalar

Binary Search

Binary Search

#include <stdio.h>

#define SIZE 15

int binarySearch( const int b[], int searchKey, int low, int high);

int main(void)
{
int a[SIZE];
int i, key, result;

for(i=0; i<SIZE; i++){
a[i] = 2 * i;
}           //a arrayini çift sayılar ile initialize ediyoruz.Bu bizim içinde arama yapacağımız array olacak.

printf(“Enter number btw 0 and 28: “);
scanf(“%d”, &key);                                               //Hangi integerı aramak istiyorsak onu key değişkenine atıyoruz.

printf (“The array is initialized with this numbers : n”);
for (i = 0 ; i < SIZE ; i++ )
printf (“a[%d] = %d n”,i,a[i]);

result= binarySearch(a, key, 0, SIZE-1);

if(result != -1){                                                      //Eğer key a dizininde var ise ‘ İF’  functionu kaçıncı eleman olduğunu gösterecektir.
printf(“n%d found in array elemet %dn”, key, result);
}else{
printf(“nKey not found!n”);                  //  Şayet aranılan key dizinde yok ise ekranda Key not found görülecektir.
}
return 0;

}

int binarySearch( const int b[], int searchKey, int low, int high)
{
int middle;

while(low<= high){
middle=(low+high)/2;

if(searchKey == b[middle]){
return middle;
}else if(searchKey < b[middle]){
high= middle – 1;
}else{
low = middle + 1;
}
}

return -1;
}