+ All Categories
Home > Documents > CPS 393 Introduction to Unix and C

CPS 393 Introduction to Unix and C

Date post: 07-Jan-2016
Category:
Upload: cece
View: 28 times
Download: 4 times
Share this document with a friend
Description:
CPS 393 Introduction to Unix and C. START OF WEEK 12 (C-6). Linked Lists. Generally, a list could be described as: __ ____ ____ ____ ____ | - | --> |e1|-| --> |e2|-| ---> |e3|-| --> .... ---> |en|-| - PowerPoint PPT Presentation
21
06/20/22 Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 12 (C-6)
Transcript
Page 1: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 1

CPS 393Introduction to Unix and C

START OF WEEK 12 (C-6)

Page 2: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 2

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)

Page 3: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 3

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;• };

Page 4: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 4

• 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|-|

Page 5: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 5

• 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");

Page 6: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 6

• 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;• }

Page 7: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 7

AddCan 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);

Page 8: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 8

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• */

Page 9: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 9

• void Add(NodeType **L, char c) {• NodeType *new;• new=(NodeType *) malloc(sizeof(NodeType)); //needs

error checking• new->ch=c;• new->next=*L;• *L=new;• }

Page 10: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 10

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;

Page 11: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 11

• 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;• }

Page 12: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 12

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;• }

Page 13: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 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");• }

Page 14: CPS 393 Introduction to Unix and C

04/20/23Course material created by D. Woit 14

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.

Page 15: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 15

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)

Page 16: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 16

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;• }

Page 17: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 17

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) ;

Page 18: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 18

listADT.c • /*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;• }

• 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');• }

Page 19: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 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:

Page 20: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 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;• }

Page 21: CPS 393 Introduction to Unix and C

04/20/23 Course material created by D. Woit 21

• //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;

• 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);

• }


Recommended