Chapter 3
Data Structures
Assoc. Prof. Dr. Oğuz FINDIK
2016-2017
KBUZEM
KARABUK UNIVERSITY
1
 Strings are one-dimesional arrays of char and
terminated by the end-of-string sentinel 0 or null
character .
 String constant are written between double quotes.
For example "abc" is a character array of size 4, with
the last element being the null character 0.
 char *p ="abc"
 Printf ("%s%sn",p,p+1);
2
Strings
 char *p ="abcde";
 char s[]={'a', 'b', 'c', 'd', 'e', '0'};
 #include <stdio.h>
 #include <stdlib.h>
 void f(int a[]);
 int main(void) {
 char s[] = "Pointer is heart of the C programming";
 char *p = " Pointer is heart of the C programming";
 int a[3];
 double d[5];
 printf("%s%dn%s%dn%s%dn%s%dn", "sizeof(s) = ", sizeof(s),
 "sizeof(p) = ", sizeof(p), "sizeof(a) = ", sizeof(a),
 "sizeof(d) = ", sizeof(d));
 f(a);
 return EXIT_SUCCESS;
 }
 void f(int a[]) {
 printf("In f(): sizeof(a) = %dn", sizeof(a));
 } 3
Strings
 #include <stdio.h>
 #include <stdlib.h>
 int find_size(char *);
 int main(void) {
 char *s = "Pointer is heart of the C programming";
 printf("Character of String : %d", find_size(s));
 return EXIT_SUCCESS;
 }
 int find_size(char *s) {
 int sum = 0;
 while (*s != '0') {
 while(isspace(*s)) s++;
 if (*s!='0'){
 sum++;
 s++;
 }
 }
 return sum;
 } 4
Characters of Strings
#include <stdio.h>
#include <stdlib.h>
int find_size(char *);
int main(void) {
char *s = "Pointer is heart of the C programming ";
printf("Words of String : %d", find_size(s));
return EXIT_SUCCESS;
}
int find_size(char *s) {
int sum = 0;
while (*s != '0') {
while(isspace(*s)) s++;
if (*s!='0'){
sum++;
while (!isspace(*s) && *s!='0') s++;
}
}
return sum;
}
5
Words of Strings
#include <stdio.h>
#include <stdlib.h>
void *str_cpy(char *, register const
char *);
int str_len(const char *);
int main(void) {
char c[] = "Pointers is important for C
programmer";
char *s1 = malloc((str_len(c) + 1) *
sizeof(char));
printf("%s", str_cpy(s1, c));
return EXIT_SUCCESS;
}
6
String copy Function
int str_len(const char *s1) {
int length = 0;
while (*s1++ != '0')
length++;
return length;
}
void *str_cpy(char *s1, register
const char *s2) {
register char *p = s1;
while (*p++ = *s2++)
;
return s1;
}
 int a[3][5];
 Starting at the base adress of the array, all the array
elements are stored contiguously in memory. The array
name a by itself is equivalent to &a[0]. It is pointer to an
array of 5 ints.
7
Multidimesional Array
 In C, array elements can be of any type, including
pointer type.
8
Array of pointers
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXWORD 50
#define N 10
void wrt_words(char **);
void swap(char **, char **);
void sort_words(char **);
int main(void) {
char word[MAXWORD];
char *w[N];
int n, i;
for (i = 0; i < N; i++) {
scanf("%s", word);
fflush(stdout);
w[i] = calloc(strlen(word) + 1,
sizeof(char));
strcpy(w[i], word);
}
sort_words(w);
wrt_words(w);
return EXIT_SUCCESS;
}
9
Example
void sort_words(char **a) {
int i, j;
for (i = 0; i < N; i++) {
for (j = i + 1; j < N; j++) {
if
(strcmp(a[i], a[j]) > 0)
swap(&a[i], &a[j]);
}
}}
void swap(char **p, char **q) {
char *temp;
temp = *p;
*p = *q;
*q = temp;
}
void wrt_words(char **a) {
int i;
for (i = 0; i < N; i++) {
printf("%sn", a[i]);
}
}
 typedef is a keyword used in C language to assign alternative
names to existing types. Its mostly used with user defined
data types, when names of data types get slightly
complicated. Also typedef improves portability of your
product. Following is the general syntax for using typedef,
 typedef existing_name alias_name
 typedef unsigned long ulong;
 ulong i, j ;
10
typedef
 Enumerated Types are a special way of creating your own
Type in C
 enum boolean {true,false};
 enum day
{Pazar,Pazartesi,Sali,carsamba,persembe,Cuma,Cumartesi}
 enum day =Pazar;
11
Enumerated Types
 The structure mechanism provides a means to aggregate
variables of different types. Let us define a structure that
describes student that has name, surname and number.
struct student{
int sequence;
char *name;
char *surname;
char *number;
};
struct student s1,s2; 12
Structure and Unions
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int sequence;
char *name;
char *surname;
char *number;
} student;
int main(void) {
int sira =0;
student s1;
s1.name = calloc(50,sizeof(char));
strcpy(s1.name,"Oguz");
s1.surname ="FINDIK";
s1.number="0102124525";
s1.sequence =++sira;
printf("Sequence :%dn",s1.sequence);
printf("Name :%sn",s1.name);
printf("Surname :%sn",s1.surname);
printf("Number :%sn",s1.number);
return EXIT_SUCCESS;
}
13
Example
A union, like a structure, is a derived type. Unions follow the same
syntax as structures but have members that share storage. A union
type defines a set of alternative values that may be stored in a
shared portion of memory. The programmer is responsible for
interpreting the stored values correctly. Consider the declaration
typedef union int_or_float {
int i;
float f;
} number;
14
Unions
#include <stdio.h>
#include <stdlib.h>
typedef union int_or_float {
int i;
float f;
} number;
int main(void) {
number n;
n.i = 10;
printf("d: %dn", n.i);
n.f = 20.0;
printf("f: %fn", n.f);
printf("d: %dn", n.i);
return EXIT_SUCCESS;
}
15
Example

Data structure week 3

  • 1.
    Chapter 3 Data Structures Assoc.Prof. Dr. Oğuz FINDIK 2016-2017 KBUZEM KARABUK UNIVERSITY 1
  • 2.
     Strings areone-dimesional arrays of char and terminated by the end-of-string sentinel 0 or null character .  String constant are written between double quotes. For example "abc" is a character array of size 4, with the last element being the null character 0.  char *p ="abc"  Printf ("%s%sn",p,p+1); 2 Strings
  • 3.
     char *p="abcde";  char s[]={'a', 'b', 'c', 'd', 'e', '0'};  #include <stdio.h>  #include <stdlib.h>  void f(int a[]);  int main(void) {  char s[] = "Pointer is heart of the C programming";  char *p = " Pointer is heart of the C programming";  int a[3];  double d[5];  printf("%s%dn%s%dn%s%dn%s%dn", "sizeof(s) = ", sizeof(s),  "sizeof(p) = ", sizeof(p), "sizeof(a) = ", sizeof(a),  "sizeof(d) = ", sizeof(d));  f(a);  return EXIT_SUCCESS;  }  void f(int a[]) {  printf("In f(): sizeof(a) = %dn", sizeof(a));  } 3 Strings
  • 4.
     #include <stdio.h> #include <stdlib.h>  int find_size(char *);  int main(void) {  char *s = "Pointer is heart of the C programming";  printf("Character of String : %d", find_size(s));  return EXIT_SUCCESS;  }  int find_size(char *s) {  int sum = 0;  while (*s != '0') {  while(isspace(*s)) s++;  if (*s!='0'){  sum++;  s++;  }  }  return sum;  } 4 Characters of Strings
  • 5.
    #include <stdio.h> #include <stdlib.h> intfind_size(char *); int main(void) { char *s = "Pointer is heart of the C programming "; printf("Words of String : %d", find_size(s)); return EXIT_SUCCESS; } int find_size(char *s) { int sum = 0; while (*s != '0') { while(isspace(*s)) s++; if (*s!='0'){ sum++; while (!isspace(*s) && *s!='0') s++; } } return sum; } 5 Words of Strings
  • 6.
    #include <stdio.h> #include <stdlib.h> void*str_cpy(char *, register const char *); int str_len(const char *); int main(void) { char c[] = "Pointers is important for C programmer"; char *s1 = malloc((str_len(c) + 1) * sizeof(char)); printf("%s", str_cpy(s1, c)); return EXIT_SUCCESS; } 6 String copy Function int str_len(const char *s1) { int length = 0; while (*s1++ != '0') length++; return length; } void *str_cpy(char *s1, register const char *s2) { register char *p = s1; while (*p++ = *s2++) ; return s1; }
  • 7.
     int a[3][5]; Starting at the base adress of the array, all the array elements are stored contiguously in memory. The array name a by itself is equivalent to &a[0]. It is pointer to an array of 5 ints. 7 Multidimesional Array
  • 8.
     In C,array elements can be of any type, including pointer type. 8 Array of pointers
  • 9.
    #include <stdio.h> #include <stdlib.h> #include<string.h> #define MAXWORD 50 #define N 10 void wrt_words(char **); void swap(char **, char **); void sort_words(char **); int main(void) { char word[MAXWORD]; char *w[N]; int n, i; for (i = 0; i < N; i++) { scanf("%s", word); fflush(stdout); w[i] = calloc(strlen(word) + 1, sizeof(char)); strcpy(w[i], word); } sort_words(w); wrt_words(w); return EXIT_SUCCESS; } 9 Example void sort_words(char **a) { int i, j; for (i = 0; i < N; i++) { for (j = i + 1; j < N; j++) { if (strcmp(a[i], a[j]) > 0) swap(&a[i], &a[j]); } }} void swap(char **p, char **q) { char *temp; temp = *p; *p = *q; *q = temp; } void wrt_words(char **a) { int i; for (i = 0; i < N; i++) { printf("%sn", a[i]); } }
  • 10.
     typedef isa keyword used in C language to assign alternative names to existing types. Its mostly used with user defined data types, when names of data types get slightly complicated. Also typedef improves portability of your product. Following is the general syntax for using typedef,  typedef existing_name alias_name  typedef unsigned long ulong;  ulong i, j ; 10 typedef
  • 11.
     Enumerated Typesare a special way of creating your own Type in C  enum boolean {true,false};  enum day {Pazar,Pazartesi,Sali,carsamba,persembe,Cuma,Cumartesi}  enum day =Pazar; 11 Enumerated Types
  • 12.
     The structuremechanism provides a means to aggregate variables of different types. Let us define a structure that describes student that has name, surname and number. struct student{ int sequence; char *name; char *surname; char *number; }; struct student s1,s2; 12 Structure and Unions
  • 13.
    #include <stdio.h> #include <stdlib.h> typedefstruct { int sequence; char *name; char *surname; char *number; } student; int main(void) { int sira =0; student s1; s1.name = calloc(50,sizeof(char)); strcpy(s1.name,"Oguz"); s1.surname ="FINDIK"; s1.number="0102124525"; s1.sequence =++sira; printf("Sequence :%dn",s1.sequence); printf("Name :%sn",s1.name); printf("Surname :%sn",s1.surname); printf("Number :%sn",s1.number); return EXIT_SUCCESS; } 13 Example
  • 14.
    A union, likea structure, is a derived type. Unions follow the same syntax as structures but have members that share storage. A union type defines a set of alternative values that may be stored in a shared portion of memory. The programmer is responsible for interpreting the stored values correctly. Consider the declaration typedef union int_or_float { int i; float f; } number; 14 Unions
  • 15.
    #include <stdio.h> #include <stdlib.h> typedefunion int_or_float { int i; float f; } number; int main(void) { number n; n.i = 10; printf("d: %dn", n.i); n.f = 20.0; printf("f: %fn", n.f); printf("d: %dn", n.i); return EXIT_SUCCESS; } 15 Example