Ch4-Lists-C-Implementation

Ch4-Lists-C-Implementation - Chapter4ListsinC SangYongHan

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter4 – Lists in C                  Sang Yong Han            http://ec.cse.cau.ac.kr/ Chung-Ang University Spring 2011 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
int i, *pi; float f, *pf; pi = (int *) malloc(sizeof(int)); pf = (float *) malloc (sizeof(float)); *pi =1024; *pf =3.14; printf(”an integer = %d, a float = %f\n”, *pi, *pf); free(pi); free(pf); request memory return memory Using Dynamically  Allocated Memory (review)
Background image of page 2
List ADT ADT with position-based methods generic methods size(), isEmpty() query methods isFirst(p), isLast(p) accessor methods first(), last() before(p), after(p) update methods swapElements(p,q),  replaceElement(p,e) insertFirst(e), insertLast(e) insertBefore(p,e), insertAfter(p,e) removeAfter(p)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
typedef struct node, *pnode; typedef struct node {              char data [4];              pnode next;              }; Creation pnode ptr =NULL;  Allocation ptr=(pnode) malloc (sizeof(node)); Declaration Implementation
Background image of page 4
b a t \0 NULL address of first node ptr data ptr link ptr e name h (*e).name strcpy(ptr data, “bat”); ptr link = NULL; Create one Node
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
pnode create2( ) { /* create a linked list with two nodes */     pnode first, second;     first = (pnode) malloc(sizeof(node));     second = ( pnode) malloc(sizeof(node));     second -> next= NULL;     second -> data = 20;     first -> data = 10;     first ->next= second;     return first; } 10 20 NULL ptr Example: Create a two-nodes list
Background image of page 6
void insertAfter(pnode node, char* data) {
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 29

Ch4-Lists-C-Implementation - Chapter4ListsinC SangYongHan

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online