1 / 56

Cs288 Intensive Programming in Linux

Cs288 Intensive Programming in Linux. Instructor: C F Yurkoski Christopher.f.yurko w ski@njit.edu Section web site: https://web.njit.edu/~yurkowsk/cs288.html Class 9. Test revu Example using switch statements. Stacks More on linked lists Errnos getopts Binary trees (maybe next week).

laurencec
Download Presentation

Cs288 Intensive Programming in Linux

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Cs288Intensive Programming in Linux Instructor: C F Yurkoski Christopher.f.yurkowski@njit.edu Section web site: https://web.njit.edu/~yurkowsk/cs288.html Class 9

  2. Test revu • Example using switch statements. • Stacks • More on linked lists • Errnos • getopts • Binary trees (maybe next week).

  3. Programming section • Write a program which accepts one command line argument which is the name of the file to which it should write its output. (The output should be written to the beginning of the file each time, do not append to the file.) • Your program should read a series of lines from standard input until an EOF is encountered. • Each line of input consists of a command and an integer. • The command is either ADD or DEL. • If the command is ADD you should add the integer to a linked list sorted in numeric order. • If the command is DEL you should delete that number from that list.

  4. When the EOF is encountered, write the list to the file whose name is arg1, one integer per line. • Ignore any duplicate adds. • Ignore any deletes of items not in the list. • For example, if the file input contains: ADD 10 ADD 8 ADD 11 ADD 11 DEL 8 DEL 9 ADD 9 • And if your program is called myprog, • After this is executed: • cat input | myprog output • output should contain: 9 10 11

  5. 1. a a

  6. 2. 234

  7. Who invented C?

  8. s

  9. a z e t

  10. 24

  11. A H G F E D

  12. 1

  13. sample solution https://web.njit.edu/~yurkowsk/x/stack1.c

  14. #include <stdlib.h> #include <stdio.h> #include <string.h> #define SIZE 20 main() { int *p, *q, fill; char cmd[10]; q=p=malloc(SIZE*sizeof(int)); if(p==NULL){ fprintf(stderr, "cannot allocate any more memory\n"); exit(-1); }

  15. while(scanf("%s", cmd)!=EOF) if(!strcmp(cmd,"POP")){ fprintf(stderr,"%s\n", "POP"); p--; } else { scanf("%d",&fill); fprintf(stderr,"%s %d\n", p, fill); *p=fill; p++; } while(q<p) printf("%d\n",*q++); }

  16. afsconnect1-56 x >: cat - | ./a.out 2>/dev/null PUSH 1 PUSH 2 POP PUSH 3 1 3 afsconnect1-57 x >:

  17. Some issues • Doesn’t check for overflow • Doesn’t check for stack underflow • Only handles frames of int, not a complex type

  18. improved solution • https://web.njit.edu/~yurkowsk/x/stack2.c

  19. afsconnect1-65 x >: cat /tmp/input PUSH 1 PUSH 2 PUSH 3 PUSH 4 PUSH 5 PUSH 6 PUSH 7 PUSH 8 PUSH 9 PUSH a PUSH b PUSH c PUSH d PUSH e PUSH f PUSH 0 afsconnect1-66 x >: cat /tmp/input | ./a.out overflow afsconnect1-67 x >:

  20. complex frame type • https://web.njit.edu/~yurkowsk/x/stack4b.c

  21. recall homework for next week should handle variable size frames.

  22. hints for doing the variable frame size homework • consider using a subroutine to print the frames. • consider using a union to store items and pointer on the stack. • have stack pointer point to cell containing previous stack pointer.

  23. Singly linked lists

  24. https://web.njit.edu/~yurkowsk/list.h Which contains: typedefstruct list list; struct list { list *ptr; char word[26]; }; list *insert(list *lp, list *newp); list *append(list *lp, list *newp); list *delete(list *lp, list *newp);

  25. Write an append() function with a signature as defined in the header file above. • The append() function will get called from list.o and will be passed a pointer to a linked list of items and a new item to append to the list. • It may be called multiple times. • Assume the list is NULL terminated. • Assume the ptr of the new item is NULL. • The list may have no items in it.

  26. To compile: • cc -c append.c • cc append.o list.o -o append

  27. take 15 minutes todo this assignment

  28. solution: #include <stdio.h> #include <stdlib.h> #include "list.h" list *append(list *lp, list *newp){ while(lp->ptr!=NULL) { lp=lp->ptr; } lp->ptr=newp; }

  29. #include <stdio.h> #include <stdlib.h> #include "list.h" main() { list head, *nextp, *lp, *lastp; head.ptr=NULL; lastp=&head; nextp=malloc(sizeof(list)); nextp->ptr=NULL; while((scanf("%s",nextp->word))>0) { append(&head,nextp); nextp=malloc(sizeof(list)); nextp->ptr=NULL; } lp=&head; while(lp->ptr!=NULL){ lp=lp->ptr; printf("word %s\n",lp->word); } }

  30. Some errnos from <errno.h> E2BIG Argument list too long. EACCES Permission denied. EADDRINUSE Address in use. EADDRNOTAVAIL Address not available. EAFNOSUPPORT Address family not supported. EBADF Bad file descriptor. EBADMSG Bad message. EBUSY Device or resource busy.

  31. #include <errno.h> #include <stdio.h> #include <stdlib.h> main() { FILE *fp; fp=fopen("test","r"); printf("errno=%d\n",errno); switch(errno){ case 0: printf("no error\n"); break; case 2: printf("file does not exist\n"); break; case EACCES: printf("permission denied\n"); } }

  32. In class example #2 • Using previous example as a basis, create a file containing a delete() function whose signature is as defined in the header file. • It will be passed a pointer to a list and the list element whose word field contains the item to delete. • New delete() function should delete all items from the list which match. • The item may occur once, multiple times or not at all. • Be sure to handle the case where the item to delete is the first or last item. • Be sure to make sure the new list is terminated with a NULL pointer. • Put your program in moodle as assignment 2. • You can/should use the previous assignment to build a test list from which you can delete items. • Take another 20 minutes to do this; you can do this in groups if you like.

  33. sample output delete function

  34. sample solution delete()

  35. Doubly linked lists

  36. Class 9 Homework #2. (add pointer to list.o) rewrite insert() using a doubly linked list and this header file: https://web.njit.edu/~yurkowsk/double.h afsconnect1-54 public_html >: cat double.h typedef struct list list; struct list { list *back; list *forward; char word[26]; }; list *insert(list *lp, list *newp); list *append(list *lp, list *newp); list *delete(list *lp, list *newp);

  37. getopt • man 3 getopt GETOPT(3) Linux Programmerâs Manual GETOPT(3) NAME getopt, getopt_long, getopt_long_only, optarg, optind, opterr, optopt - Parse command-line options SYNOPSIS #include <unistd.h> intgetopt(intargc, char * const argv[], const char *optstring); extern char *optarg; extern intoptind, opterr, optopt; #include <getopt.h> intgetopt_long(intargc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex); intgetopt_long_only(intargc, char * const argv[], const char *optstring,

More Related