560 likes | 627 Views
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).
E N D
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
Test revu • Example using switch statements. • Stacks • More on linked lists • Errnos • getopts • Binary trees (maybe next week).
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.
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
sample solution https://web.njit.edu/~yurkowsk/x/stack1.c
#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); }
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++); }
afsconnect1-56 x >: cat - | ./a.out 2>/dev/null PUSH 1 PUSH 2 POP PUSH 3 1 3 afsconnect1-57 x >:
Some issues • Doesn’t check for overflow • Doesn’t check for stack underflow • Only handles frames of int, not a complex type
improved solution • https://web.njit.edu/~yurkowsk/x/stack2.c
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 >:
complex frame type • https://web.njit.edu/~yurkowsk/x/stack4b.c
recall homework for next week should handle variable size frames.
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.
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);
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.
To compile: • cc -c append.c • cc append.o list.o -o append
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; }
#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); } }
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.
#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"); } }
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.
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);
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,