Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPS 393 Introduction to Unix and C

Similar presentations


Presentation on theme: "CPS 393 Introduction to Unix and C"— Presentation transcript:

1 CPS 393 Introduction to Unix and C
START OF WEEK 12 (C-6) 7/4/2018 Course material created by D. Woit

2 Course material created by D. Woit
Linked Lists Generally, a list could be described as: __ ____ ____ ____ ____ | -|-->|e1|-|-->|e2|-|--->|e3|-|--> >|en|-| List where ei is the ith element on the list. Therefore, the list a b c can be: __ ____ ____ ____ | -|-->|a |-|-->|b |-|--->|c |-| A "null" pointer signifies end of list (NULL from stdio.h) 7/4/2018 Course material created by D. Woit 2

3 Course material created by D. Woit
example /*Source list.c Making the list is done "by hand" here for illustration, but you would normally write a function, such as Add, so that a call such as Add(List,'a') would add an 'a' to the List New element is added to the head of the list */ #include <stdio.h> #include <stdlib.h> typedef struct node NodeType; struct node { char ch; NodeType *next; }; 7/4/2018 Course material created by D. Woit 3

4 Course material created by D. Woit
int main(void) { NodeType *Node, //a list node *List, //a list *ptr; //a temp var to move along a list, node by node List=NULL; // initially empty list Node=(NodeType *) malloc(sizeof(NodeType)); //we add first node Node->ch='a'; Node->next=NULL; // current end of list printf("Node.ch: %c\n", Node->ch); //or (*Node).ch printf("Node.next: %p\n", Node->next); List=Node ; // pointer to list is updated printf("List.ch: %c\n", List->ch); printf("List.next: %p\n", List->next); // now |.|->|a|-| 7/4/2018 Course material created by D. Woit 4

5 Course material created by D. Woit
Node=(NodeType *) malloc(sizeof(NodeType)); //we add second node Node->ch='b'; // data field is set to ‘b’ Node->next=NULL; // for the time being Node->next=List; // now this node is set to point to the old head of the list List=Node; // and head of the list is set to point to the new node // now |.|->|b|-|->|a|-| /*print out list*/ printf(" \nList is:\n \n "); for (ptr=List; ptr!=NULL; ) { printf(" %c ", ptr->ch); ptr=ptr->next; } printf("\n"); 7/4/2018 Course material created by D. Woit 5

6 Course material created by D. Woit
printf(" \nDetail :\n \n"); for (ptr=List; ptr!=NULL; ) { printf("List.ch: %c\n", ptr->ch); printf("List.next: %p\n", ptr->next); ptr=ptr->next; } return 0; 7/4/2018 Course material created by D. Woit 6

7 Course material created by D. Woit
Add Can make a function Add(list, char) to put char on front of list /*Source add.h header file for add.c */ typedef struct node NodeType; struct node { char ch; NodeType *next; }; void Add(NodeType **L, char c); 7/4/2018 Course material created by D. Woit 7

8 Course material created by D. Woit
Source add.c /*Source add.c function to add a char to the front of a list */ #include <stdio.h> #include <stdlib.h> #include "add.h“ /*Note: need to pass address of L so that it is call by value if just passed *L, any changes to L inside function would be lost upon function return. e.g., calling add(List,ch) would not cause List to be changed after add return 7/4/2018 Course material created by D. Woit 8

9 Course material created by D. Woit
void Add(NodeType **L, char c) { NodeType *new; new=(NodeType *) malloc(sizeof(NodeType)); //needs error checking new->ch=c; new->next=*L; *L=new; } 7/4/2018 Course material created by D. Woit 9

10 Course material created by D. Woit
addmain.c /*Source addmain.c program to use function Add (in file add.c) to add to front of a list */ #include <stdio.h> #include "add.h" int main(void) { NodeType *Node, //a pair *List, //a list *ptr; //a temp var to move along a list, pair by pair char ch; //char to add to front of list List=NULL; 7/4/2018 Course material created by D. Woit 10

11 Course material created by D. Woit
for (ch='a'; ch<'f';ch++) //add a b c d e, each to front of list Add(&List,ch); //call by value, so List changed printf("List is: "); /*print list*/ for (ptr=List; ptr!=NULL; ) { printf(" %c ", ptr->ch); ptr=ptr->next; } printf("\n"); return 0; 7/4/2018 Course material created by D. Woit 11

12 Course material created by D. Woit
Searching /*source: search.c Search a list to determine if given character is in list*/ int Srch(NodeType *L, char item) { NodeType *p; for (p=L; p != NULL; p = p->next) if (p->ch == item) return 1; return 0; } 7/4/2018 Course material created by D. Woit 12

13 We can alter addmain.c to do a search:
//Source addmainS.c #include <stdio.h> #include "add.h“ #include “srch.h” void main(void) { NodeType *Node, *List, *ptr; char ch, item; List=NULL; for (ch='a'; ch<'f';ch++) Add(&List,ch); printf("List is: "); for (ptr=List; ptr!=NULL; ) { printf(" %c ", ptr->ch); ptr=ptr->next; } printf("\n"); /*search for a given item in list*/ printf("Enter char to search for: "); scanf("%c",&item); if ((Srch(List,item))==1) printf("found\n"); else printf("not found\n"); 7/4/2018 Course material created by D. Woit 13

14 Course material created by D. Woit
HMWK: Write a version of Add that does not mutate (change) the list; instead, it should return a new list. The function prototype could be NodeType *Add(NodeType *L, char c); Write functions for first and rest as follows: first returns a copy of the first item on the list (error if empty list) rest returns a list that is the given list with the first item removed. (error if empty list). first should return a character and rest should return a list. Write fucntion InsertO which inserts a given item into a list, keeping the list in order. Write function SrchO which searches an ordered list for the given item. Note that if the item is not in the list, search can terminate as soon as it finds an item larger than the searched-for item. Course material created by D. Woit 14 7/4/2018

15 Course material created by D. Woit
ADTs -Normally, programmer supplies object code and definitions (.o and .h file) for an ADT. User writes a main which includes the .h and compiles their main with the given .o -So user does not see source code. -e.g., C sqrt function. WE use it by including math.h and then compiling our main with the math object code using -lm (other object code included automatically, e.g., for printf, etc, but we still need definitions, eg., stdio.h) 7/4/2018 Course material created by D. Woit 15

16 Course material created by D. Woit
USER PROGRAM: /*Source userlistADT.c compile: gcc userlistADT.c listADT.o */ #include <stdio.h> #include <stdlib.h> #include "listADT.h" int main(void) { List L; init(&L); add(&L,'a'); add(&L,'b'); add(&L,'c'); print(L); printf("Length is %d\n",length(L)); return 0; } 7/4/2018 Course material created by D. Woit 16

17 Course material created by D. Woit
PROGRAMMER CODE: /*Source listADT.h */ typedef struct node NodeType; struct node { char ch; NodeType *next; }; typedef struct { int length; NodeType *head; } List; void init(List *L) ; void add(List *L, char c) ; void print(List L) ; int length(List L) ; 7/4/2018 Course material created by D. Woit 17

18 Course material created by D. Woit
listADT.c void add(List *L, char c) { NodeType *new; new=(NodeType *) malloc(sizeof(NodeType)); new->ch=c; new->next=L->head; L->head=new; L->length++; } void print(List L) { int i; NodeType *p; printf("List is: "); for (i=0,p=L.head; i<L.length; i++,p=p->next) { printf(" %c ", p->ch); putchar('\n'); /*Source listADT.c a user compiles listADT.o with their main to use a list ADT */ #include <stdio.h> #include <stdlib.h> #include "listADT.h" void init(List *L) { L->length=0; L->head=NULL; } int length(List L) { return L.length; 7/4/2018 Course material created by D. Woit 18

19 Hiding all Implementation Details
-If selling Off-The-Shelf code to a 3rd party, want to completely hide the implementation details from user (3rd party). -How? User only gets ADT.h and ADT.o. User includes ADT.h in main and compiles main with ADT.o (has no source code ADT.c) ADT.h has no ADT implementation details, only (void *) ADT.o has ADT implementation details (structs etc), and casts the (void *) to (ADT *) as necessary within ADT functions -example: 7/4/2018 Course material created by D. Woit 19

20 Example of how to hide details of ADT from user
/*Source: main.c example of how to hide details of ADT from user by keeping struct details out of complex.h, and only in complex.c */ #include <stdio.h> #include <stdlib.h> #include "complex.h" int main (void) { complex *m; set(&m,7,2); print(m); return 0; } 7/4/2018 Course material created by D. Woit 20

21 Course material created by D. Woit
void set(complex **a, int v, int w) { priv_complex *new; new=(priv_complex *)malloc(sizeof(priv_complex)); (*new).i=v; (*new).j=w; (*a)=(void *)new; } void print(complex *a) { printf("%d+%di\n", (*(priv_complex *)a).i, (*(priv_complex *)a).j); //Source complex.h typedef void *complex; void set(complex **a,int i, int j) ; void print(complex *a) ; //Source complex.c #include <stdio.h> #include <stdlib.h> #include "complex.h" typedef struct { int i; int j; } priv_complex; 7/4/2018 Course material created by D. Woit 21


Download ppt "CPS 393 Introduction to Unix and C"

Similar presentations


Ads by Google