Download presentation
Presentation is loading. Please wait.
Published byMeredith Holt Modified over 8 years ago
2
Last Lecture Reminder Array Sorts Exam Questions Examples Open Questions ◦ Search Engines ◦ Operating Systems ◦ Multiple Files in Projects Additional Lesson – before MOED ALEF
3
Linked Lists Files
4
2345 Head Next Next = NULL
5
typedef struct link{ int data; struct link *next; }link;
6
void main(){ link *head=NULL, *temp=NULL; int d; printf("Enter the list values:\n"); scanf ("%d",&d); while (d!=-1){ temp=(link *)malloc(sizeof(link)); if (!temp) break; temp->next=head; temp->data=d; head=temp; scanf ("%d",&d); } A list is presented by its head
7
while(temp){ printf("%d ",temp->data); temp=temp->next; } while (head){ temp=head->next; free(head); head=temp; }
8
void free_list(link *head) { if (head== NULL) /* Finished freeing. Empty list */ return; free_list(head->next); /* Recursively free what’s ahead */ free(head); }
10
C Program STDIN STDOUT STDERR FILE1 FILE2
11
stdin stdout stderr
12
#include void main(){ int n; printf("hello world"); fprintf(stdout,"hello world"); fprintf(stderr,"hello world"); fscanf(stdin,"%d", &n); scanf("%d", &n); }
13
#include void main(){ FILE *f1; f1 = fopen(“test1.txt","w"); if(!f1){ printf("Error opening file!\n"); return; } fprintf(f1,"hello world"); fclose(f1); }
14
#include void main(){ char ch; FILE *f1; f1 = fopen(“test2.txt","r"); if(!f1){ printf("Error opening file!\n"); return; } fscanf(f1,"%c",&ch); fclose(f1); }
15
Bubble Sort Selection Sort Insertion Sort Merge Sort Quick Sort
16
#include void main() { int i, j, min, temp, nums[10]; for (i=0; i<SIZE; i++){ /*input*/ printf ("enter number\n"); scanf ("%d",&nums[i]); } for (i=0; i<SIZE; i++){ /*main loop*/ for (j=0; j<SIZE-i-1; j++) /*finding smaller*/ if (nums[j]>nums[j+1]) { temp = nums[j]; /*swapping*/ nums[j] = nums[j+1]; nums[j+1] = temp; } for (i=0; i<SIZE; i++) /*printing*/ printf ("%d\n",nums[i]); }
17
Unsorted array: Effective array of size 6: Effective array of size 5: Effective array of size 4: Effective array of size 3: Effective array of size 2: Effective array of size 1: Sorted array: 45 67 12 34 25 39 45 12 34 25 39 67 12 34 25 39 45 12 25 34 39 12 25 34 12 25 12 12 25 34 39 45 67
18
37591211527235779
19
#include void main() { int i, j, min, temp, nums[10]; for (i=0; i<10; i++){ /*input*/ printf ("enter number\n"); scanf ("%d",&nums[i]); } for (i=0; i<9; i++){ /*main loop*/ min = i; for (j=i+1; j<10; j++) /*finding minimum index*/ if (nums[j]<nums[min]) min = j; temp = nums[i]; /*swaping*/ nums[i] = nums[min]; nums[min] = temp; } for (i=0; i<10; i++) /*printing*/ printf ("%d\n",nums[i]); }
20
375912
21
#include void main() { int i, j, temp, nums[10]; for (i=0; i<10; i++){ /*input*/ printf ("enter number\n"); scanf ("%d",&nums[i]); } for (i=0; i<10; i++){ /*main loop*/ /*finding location of nums[j]*/ for (j=i; j>0 && nums[j]<nums[j-1]; j--){ temp = nums[j]; /*swaping*/ nums[j] = nums[j-1]; nums[j-1] = temp; } for (i=0; i<10; i++) /*printing*/ printf ("%d\n",nums[i]); }
22
void mergeSort(int ar[],int lo,int hi){ if (lo<hi) { int m=(lo+hi)/2; mergeSort(ar,lo, m); mergeSort(ar, m+1, hi); merge(ar, lo, m, hi); }
23
#define SIZE 100 void merge(int a[],int lo,int m, int hi){ int i, j, k, b[SIZE]; for (i=lo; i<=hi; i++) b[i]=a[i]; i=lo; j=m+1; k=lo; while (i<=m && j<=hi) if (b[i]<=b[j]) a[k++]=b[i++]; else a[k++]=b[j++]; while (i<=m) a[k++]=b[i++]; }
25
What’s printed on the screen when the following programs are run? ◦ printing.c ◦ change_val.c What does this function do? ◦ secret.c
26
Implement a function such that – ◦ Input: string s that contains (among other things) parentheses ◦ Output: 1 if the parentheses’ nesting is legal, 0 otherwise ◦ For example – (()())() is legal, while ())() and (()(()) are not. Write a program that accepts a string from the user and checks the parentheses.
27
Write a structure of a point in 2D space and a distance function between two points Write a program that gets an array of points from the user and calculates the largest distance between a pair of points in the array The size of the array is given by the user before the points are read in Solution – max_distance.c
28
Implement a linked list where each item simply contains an integer Input a number n from the user, and split the original list into two lists such that the first contains all the elements smaller than n, and the other contains all the others Display both linked lists on the screen
29
Define two structures – a point and a circle Implement a function is_in_circle that accepts a point and a circle, and returns 1 if the point is in the circle, 0 otherwise Write a program that accepts input from user in the following order: ◦ n = the number of circles ◦ A point And outputs the number of circles containing that point n is user input!`
30
Search Engines Operating Systems Multiple Files in Projects
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.