1 240-222 CPT: Lists/15 240-222 Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to describe linked lists in C 15. Lists.

Slides:



Advertisements
Similar presentations
Linked List 1. Introduction to Linked List 2. Node Class 3. Linked List 4. The Bag Class with Linked List.
Advertisements

Linked List Variations
Data Structure Lecture-5
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
Abstract Data Type Example l One more example of ADT: l integer linked list using class l A class with dynamic objects: l Copy constructor l Destructor.
Structures and Lists (and maybe stacks) Chapters 9-10.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
 2006 Pearson Education, Inc. All rights reserved Data Structures.
CS Data Structures Chapter 4 Lists.
Linked List (I) Ying Wu Electrical & Computer Engineering Northwestern University ECE230 Lectures Series.
Lecture 25 Self-Referential Structures Linked Lists
 2003 Prentice Hall, Inc. All rights reserved Linked Lists Upcoming program has two class templates –Create two class templates –ListNode data.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Introduction to Data Structures Systems Programming.
CPT: Ptr+Str/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to discuss how pointers are used with structs.
CSC 211 Data Structures Lecture 23
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
CS 1031 Linked Lists Definition of Linked Lists Examples of Linked Lists Operations on Linked Lists Linked List as a Class Linked Lists as Implementations.
 2008 Pearson Education, Inc. All rights reserved. 1 Member data stores a value of type parameter NODETYPE Member nextPtr stores a pointer to the next.
Introduction to Data Structures Systems Programming Concepts.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Queues.
Programming Practice 3 - Dynamic Data Structure
APS105 Lists. Structures Arrays allow a collection of elements –All of the same type How to collect elements of different types? –Structures; in C: struct.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Data Structures Dale Roberts, Lecturer Computer Science,
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
1 Objectives of these slides: to describe linked lists in C 6 – Lists.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Dale Roberts, Lecturer Data Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Data Structures - Prabir Sarkar. AGENDA Stack Queue Linked List Trees Graphs Searching and Sorting Algorithm.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Pointers and Linked Lists
Chapter 12 – Data Structures
Dynamic Allocation Review Structure and list processing
Pointers and Linked Lists
5.13 Recursion Recursive functions Functions that call themselves
Elementary Data Structures
Data Structure Dr. Mohamed Khafagy.
CPS 393 Introduction to Unix and C
Linked list.
Linked Lists.
Stack and Queue APURBO DATTA.
Introduction to Data Structures
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Recursion.
Chapter 16-2 Linked Structures
Popping Items Off a Stack Lesson xx
Review & Lab assignments
Chapter 16 Linked Structures
Linked Lists.
Andy Wang Object Oriented Programming in C++ COP 3330
Linked Lists.
Linked Lists.
Structures and List Processing
Presentation transcript:

CPT: Lists/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to describe linked lists in C 15. Lists

CPT: Lists/15 Overview 1. List Data Structures and Operations 2. List Implementations 3. Dynamically Created Lists 4. Converting a String to a List 5. List Functions

CPT: Lists/15 1. List Data Structures and Operations l Some possible operations: create/destroy a list test to see if a list is empty return the tail of the list insert/delete elements print a list calculate the length of a list

CPT: Lists/15 2. List implementations Version 1: #define N 1000 /* the size of the list */ typedef char LIST[N]; LIST lt; /* same as char lt[N] */

CPT: Lists/15 Version 2(Sec. 12.2) struct listnode { char data; struct listnode *nextptr; }; typedef struct listnode LISTNODE; LISTNODE elem; elem datanextptr

CPT: Lists/15 Use LISTNODE a, b, c; a.data = 'a'; b.data = 'c'; c.data = 'e'; a.nextptr = b.nextptr = c.nextptr = NULL; continued abc

CPT: Lists/15 a.nextptr = &b; b.nextptr = &c; printf("%c", a.nextptr->data); /* 'c' printed */ printf("%c", a.nextptr->nextptr->data); /* 'e' printed */ abc NULL ‘a’‘c’‘e’

CPT: Lists/15 3. Dynamically Created Lists /* list implementation as before */ typedef LISTNODE *LNP; LNP head = NULL; head = malloc(sizeof(LISTNODE)); head->data = 'n'; head->nextptr = NULL; Function prototype in stdlib.h : void *malloc(size_t size); head ‘n’ NULL

CPT: Lists/15 Add a second element head->nextptr = malloc(sizeof(LISTNODE)); head->nextptr->data = 'e'; head->nextptr->nextptr = NULL; head ‘n’‘e’ NULL

CPT: Lists/15 Add a third element head->nextptr->nextptr = malloc(sizeof(LISTNODE)); head->nextptr->nextptr->data = 'w'; head->nextptr->nextptr->nextptr = NULL; head ‘n’‘e’‘w’ NULL

CPT: Lists/15 4. Converting a String to a List #include #include /* list type implementation */ LNP string_to_list(char []); int main() { LNP h = NULL; h = string_to_list("AB"); return 0; } /* implementation of string_to_list() */

CPT: Lists/15 LNP string_to_list(char s[]) { LNP head = NULL, tail; int i; if (s[0] != '\0') { head = malloc(sizeof(LISTNODE)); head->data = s[0]; tail = head; for (i=1; s[i] != '\0'; i++){ tail->nextptr = malloc(sizeof(LISTNODE)); tail = tail->nextptr; tail->data = s[i]; } tail->nextptr = NULL; /* list end */ } return head; }

CPT: Lists/15 string_to_list("AB") head = malloc(sizeof(LISTNODE)); head->data = s[0]; tail = head; head ‘A’ tail ‘?’

CPT: Lists/15 tail->nextptr = malloc(sizeof(LISTNODE)); head ‘A’ tail ‘?’

CPT: Lists/15 tail = tail->nextptr; tail->data = s[i]; /* i = 1 here */ head ‘A’ tail ‘?’‘B’

CPT: Lists/15 s[2] = '\0' /* so end of list is assigned NULL */ head ‘A’ tail NULL ‘B’

CPT: Lists/15 5. List Functions 5.1. Empty Lists 5.2. Return the First Element of a List 5.3. Produce the Tail of a List 5.4. Put an Element on the Front of a List 5.5. Insertion continued

CPT: Lists/ Deletion 5.7. List Membership 5.8. Print a List 5.9. List Length Concatenate Two Lists

CPT: Lists/ Empty Lists Make an empty list: LNP h1; h1 = NULL; Test for emptiness: int isempty(LNP sptr) { return (sptr == NULL); }

CPT: Lists/ Return the First Element of a List char first(LNP cptr) { if (isempty(cptr)) return '\0' else return cptr->data; }

CPT: Lists/15 Use LNP head; char c; head = string_to_list("new"); c = first(head); /* c is 'n'; head is not altered */

CPT: Lists/ Produce the tail of a list void tail(LNP *cptr) { LNP temp; if (isempty(*cptr)) printf("The list is empty.\n\n"); else { temp = *cptr; *cptr = (*cptr)->nextptr; free(temp); } } l cptr is the address of a pointer, so that the pointer can be modified using "call by reference".

CPT: Lists/15 Use LNP head; head = string_to_list("new"); : tail(&head); /* head is now the list version of “ew” */

CPT: Lists/ Put an Element on the List Front LNP cons(char c, LNP cptr) { LNP temp; temp = malloc(sizeof(LISTNODE)); temp->data = c; temp->nextptr = cptr; return temp; }

CPT: Lists/15 Use LNP h1, h2; h1 = string_to_list("ew"); h2 = cons('n', h1); Before the cons() call: h1 ‘e’‘w’ NULL

CPT: Lists/15 h2 ‘n’‘e’‘w’ NULL After: h1

CPT: Lists/ Insertion head ‘n’‘w’ Before: previousptrcurrentptr NULL newptr ‘o’ NULL

CPT: Lists/15 head ‘n’‘o’‘w’ NULL After:

CPT: Lists/15 Code Fig void insert(LNP *sptr, char value) { LNP newptr, previousptr, currentptr; newptr = malloc(sizeof(LISTNODE)); if (newptr) { newptr->data = value; newptr->nextptr = NULL; previousptr = NULL; currentptr = *sptr; continued

CPT: Lists/15 while ((currentptr != NULL) && (value > currentptr->data)) { previousptr = currentptr; currentptr = currentptr->nextptr; } if (previousptr == NULL) { newptr->nextptr = *sptr; *sptr = newptr; } else { previousptr->nextptr = newptr; newptr->nextptr = currentptr; } } else printf("No memory available.\n"); }

CPT: Lists/15 Note l The use of a pointer address (sptr) as an argument to insert() is to allow the head pointer to the list to be altered if the character is inserted as the first node.

CPT: Lists/15 Use LNP h1; h1 = string_to_list("nw"); insert(&hl, 'o'); Dangers: LNP h1, h2; h1 = string_to_list("nw"); h2 = h1; insert(&hl, 'o');

CPT: Lists/ Deletion LNP head; head = string_to_list("new"); c = delete(&head, 'e'); head ‘n’ previousptrcurrentptr ‘e’‘w’ NULL

CPT: Lists/15 head ‘n’ previousptrcurrentptr ‘e’‘w’ NULL tempptr

CPT: Lists/15 Code Fig char delete(LNP *sptr, char value) { LNP previousptr, currentptr, tempptr; if (value == (*sptr)->data) { tempptr = *sptr; *sptr = (*sptr)->nextptr; free(tempptr); return value; } else { previousptr = *sptr; currentptr = (*sptr)->nextptr; continued

CPT: Lists/15 while ((currentptr != NULL) && (currentptr->data != value)) { previousptr = currentptr; currentptr = currentptr->nextptr; } if (currentptr) { tempptr = currentptr; previousptr->nextptr = currentptr->nextptr; free(tempptr); return value; } } return '\0'; }

CPT: Lists/15 Some Comments l The use of a pointer address (sptr) as an argument to delete() is to allow the head pointer to the list to be altered if the first character in the list is being deleted.

CPT: Lists/15 l delete() can stop when: –1. It has found the character. –2. It has reached the end of the list (the character isn't there). –3. It has reached a character lexically bigger than the one being sought. Not used in this code.

CPT: Lists/15 Dangers LNP h1, h2; char c; h1 = string_to_list("all"); h2 = h1; c = delete(&h1, 'l'); h2 would be pointing at nothing if the first node of h1 was deleted h1 ‘a’‘l’ NULL h2

CPT: Lists/ List Membership int member(char c, LNP cptr) { if (isempty(cptr)) return 0; else { if (c == first(cptr)) return 1; else return member(c, cptr->nextptr); } }

CPT: Lists/ Print a List (iteratively) Fig void printList(LNP cptr) { if (!cptr) printf("List is empty.\n\n"); else { printf("The list is:\n"); while (cptr) { printf("%c --> ", cptr->data); cptr = cptr->nextptr; } printf("NULL\n\n"); } }

CPT: Lists/15 Use LNP head; head = string_to_list("old"); printList(head); The list is: o --> l --> d --> NULL

CPT: Lists/15 Print a List (recursively) void printList(LNP cptr) { if (isempty(cptr)) printf("NULL"); else { printf("%c --> ", first(cptr)); printList(cptr->nextptr); } }

CPT: Lists/ List Length int length(LNP cptr) { if (isempty(cptr)) return 0; else return (1 + length(cptr->nextptr)); }

CPT: Lists/ Concatenate Two Lists void concat(LNP a, LNP b) { if (a->nextptr == NULL) a->nextptr = b; else concat(a->nextptr, b); }

CPT: Lists/15 Use LNP h1, h2; h1 = string_to_list("new"); h2 = string_to_list("ton"); concat(h1, h2); /* h1 altered */ print_list(h1); The list is: n --> e --> w --> t --> o --> n --> NULL

CPT: Lists/15 Dangers LNP h1, h2; h1 = string_to_list("ab"); h2 = string_to_list("cd"); concat(h1, h2); /* h1 is list "abcd" */ h2->data = 'o'; h1 ‘a’‘o’‘d’ NULL ‘b’ h2