LIST PROCESSING.

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

Advanced Piloting Cruise Plot.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Chapter 3: Linked List Tutor: Angie Hui
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Chapter 7: Arrays In this chapter, you will learn about
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Singly Linked Lists What is a singly-linked list? Why linked lists?
Stacks, Queues, and Linked Lists
Lists Chapter 6 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved
Linked Lists.
Linked Lists: deleting...
Linked Lists Chapter 4.
1/27 COP 3540 Data Structures with OOP Chapter 5 Linked Lists.
1111 Abstract Data Types Cpt S 223. School of EECS, WSU.
Linear Lists – Linked List Representation
Data Structures: A Pseudocode Approach with C
Data Structures ADT List
Chapter 24 Lists, Stacks, and Queues
Main Index Contents 11 Main Index Contents Shifting blocks of elements… Shifting blocks of elements… Model of a list object… Model of a list object… Sample.
11 Data Structures Foundations of Computer Science ã Cengage Learning.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 CSE1301 Computer Programming: Lecture 27 List Manipulation.
Data Structures Using C++
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 11: Linked Lists Lecturer: Santokh Singh Assignment 2 due tomorrow, i.e. Friday 21 Jan 2005.
Double-Linked Lists and Circular Lists
FIFO Queues CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
1 Linked Lists Math 130 Lecture # 22 B Smith: Must add malloc and free to the discussion, as well as walking a list. B Smith: Must add malloc and free.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
ABC Technology Project
1 DATA STRUCTURES. 2 LINKED LIST 3 PROS Dynamic in nature, so grow and shrink in size during execution Efficient memory utilization Insertion can be.
CSCI2100B Linked List Jeffrey
1 Symbol Tables Chapter Sedgewick. 2 Symbol Tables Searching Searching is a fundamental element of many computational tasks looking up a name.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
VOORBLAD.
Review Pseudo Code Basic elements of Pseudo code
1 Breadth First Search s s Undiscovered Discovered Finished Queue: s Top of queue 2 1 Shortest path from s.
1 Chapter Eleven Arrays. 2 A Motivating Example main( ) { int n0, n1, n2, n3, n4; scanf(“%d”, &n0); scanf(“%d”, &n1); scanf(“%d”, &n2); scanf(“%d”, &n3);
© 2012 National Heart Foundation of Australia. Slide 2.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
25 seconds left…...
Binary Search Trees Ravi Chugh March 28, Review: Linked Lists Goal: Program that keeps track of friends Problem: Arrays have fixed length Solution:
We will resume in: 25 Minutes.
PSSA Preparation.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Data Structures Using C++ 2E
Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
By Senem Kumova Metin 1 DATA TYPES. by Senem Kumova Metin 2 DATA TYPE? …… x; // DECLARATION OF VARIABLE X printf(“Do you want to go on? \n”) printf(“Please.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
Introduction to C Programming CE Lecture 19 Linear Linked Lists.
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.
Sudeshna Sarkar, CSE, IIT Kharagpur1 Structure and list processing Lecture
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Chapter 12 – Data Structures
Review & Lab assignments
Presentation transcript:

LIST PROCESSING

Self Referential Structures 1/4 struct node { int data; struct node *next; }; struct node a,b; a b a.data=1; b.data=2; a.next= b.next=NULL; // a and b do not point to some other node A structure of type struct node data next data next data next 1 NULL 2 NULL Senem Kumova Metin

Self Referential Structures 2/4 // next is a pointer to a “struct node” object a.next= &b; // next node for a is b // as a result b = *(a.next) a b *(a.next).data =? a.next->data =? 1 &b 2 NULL a.next=&b

Self Referential Structures 3/4 struct node { int data; struct node *next; }; struct node * p1 ; struct node * p2; // Create objects using pointers, pointers store the address of each object p1= (struct node *)malloc(sizeof(struct node)); p2= (struct node *)malloc(sizeof(struct node)); p1->data = 4; p2->data= 5; p1->next= p2; // p2 indicates adress of object P2->next =NULL; data=5 // next=NULL data=4 // next=p2 p1 p2

Self Referential Structures 4/4 struct node * p3= malloc(sizeof(struct node)); p3->data=1; p3->next=NULL; p2->next= p3; p1->next == p2 p1->next->next == p3 p2->data == p1->next->data p3->data == p1->next->next->data data=5 // next=p3 data=4 // next=p2 p1 p2 p3 data=1 // next=NULL

Linear Linked Lists head head->next NULL data head head->next Linear linked list is a data structure of explicit ordering of items (nodes) Each item(node) contains two portions: information(data) portion next address portion Generally the variable head contains an address or pointer that gives the location of the first node of the linked list

Linear Linked Lists : Definition next NULL data head head->next struct node { int data; struct node *next; }; // type name for new type is “struct node” struct node * head; // declares the pointer for first node (head)

Linear Linked Lists : 2 nodes in main() struct node { int data; struct node *next; }; main() { struct node * head; /* Create List */ head = (struct node *)malloc(sizeof(struct node)); head->data=1; head->next=NULL; /* Add 1st element */ head->next= (struct node *) malloc(sizeof(struct node)); head->next->data =2; head->next->next=NULL; } data=1 next=NULL head data=2 next=NULL data=1 next=& head head->next

Linear Linked Lists : Create and fill 3 nodes in main() typedef struct node { int data; struct node *next; } NODE; main() { NODE * head; head = malloc(sizeof(NODE)); head->data=1; head->next=NULL; /* Add 1st element */ head->next= malloc(sizeof(NODE)); head->next->data =2; head->next->next=NULL; /* Add 2nd element */ head->next->next = malloc(sizeof(NODE)); head->next->next->data =3; head->next->next->next=NULL; } data=1 next=NULL head data=2 next=0 data=1 next=& head head->next head data=1 next=& data=2 next=& data=2 next=0

Linked List Types Single linked lists (next) Head Tail next NULL next Double linked lists (next + previous) Head next previous NULL Circle linked lists (next) Head next Tail

Basic Linear Linked List Operations Creating a list Counting elements of list Printing data in list Inserting elements(nodes) to lists Deleting elements(nodes) of list Finding/searching elements in the list Sorting elements etc..

CREATING LINEAR LINKED LIST An integer array (x[4]) will be used to create a list. Example 1 : In main() add from head Example 2 : In main() add from tail Example 3 : In function ( Using Iteration) Example 4 : In function ( Using Recursion) How to call functions in Example 3 and 4

Example 1: In main() add from head typedef struct node {int data; struct node *next; } NODE; main(){ int x[4]={1,2,3,4}; NODE * head=NULL; NODE* tmp=NULL; for (i=0; i<4; i++) { if(head==NULL) // FIRST NODE IN LIST { head=malloc(sizeof(NODE)); head->data=x[i]; head->next =NULL; } else { tmp=malloc(sizeof(NODE)); tmp->data=x[i]; tmp->next=head; head=tmp; } } Senem Kumova Metin

Example 2 : In main() add from tail typedef struct node {int data; struct node *next; } NODE; main() { int x[4]={1,2,3,4}; NODE * head=NULL; NODE * tail=NULL; for (i=0; i<4; i++) { if(head==NULL) // FIRST NODE IN LIST { head=malloc(sizeof(NODE)); head->data=x[i]; head->next =NULL; tail=head; } else { tail->next=malloc(sizeof(NODE)); tail=tail->next; tail->data=x[i]; tail->next=NULL; } } Senem Kumova Metin

Example 3 : In function ( Using Iteration) NODE * create_ite (int x[] , int size) { NODE * head = NULL; NODE * tail =NULL; int i; if(size!=0) { head = malloc(sizeof(NODE)); head -> data = x[0]; tail = head; for (i = 1; i<size; ++i) { /* add to tail */ tail -> next = malloc(sizeof(NODE)); tail = tail -> next; tail -> data = x[i]; } tail -> next = NULL; /* end of list */ } return head; } Senem Kumova Metin

Example 4 : In function (Using Recursion) NODE * create _rec(int x[], int size) { NODE * head; if (size==0 ) /* base case */ return NULL; else { /* method */ head = malloc(sizeof(NODE)); head -> data = x[0]; head -> next = create_rec(x + 1 , size-1); return head; } }

How to call create functions in Example 3 and 4 typedef struct node { int data; struct node *next; } NODE; NODE * create_ite (int x[] , int size) ; // prototype for iterative function NODE * create _rec(int x[], int size) ; // prototype for recursive function main() { int x[4]={1,2,3,4}; NODE * head1; NODE * head2; head1=create_ite(x,4); head2=create_rec(x,4); }

Count Elements of a List Example 1 : Using Iteration Example 2: Using Recursion How to call them in main()

Example 1 : Iteration Example 2 : Recursion int count_list_ite (NODE * head) { int count=0; for (; head != NULL; head = head -> next) ++count; return count; } Example 2 : Recursion int count_list_rec (NODE * head) { if (head == NULL) return 0; else return(1 + count_list_rec(head -> next)); }

How to call count functions in Example 1 and 2 typedef struct node { int data; struct node *next; } NODE; int count_list_ite (NODE * head); // prototype for iterative function int count_list_rec (NODE * head); // prototype for recursive function main() { int x[4]={1,2,3,4}; int size1, size2 ; NODE * head; head=create(x,4); size1= count_list_ite(head); size2= count_list_rec(head); }

Print Elements of a List Example 1 : Using Iteration Example 2: Using Recursion

Example 1 : Using Iteration void print_ite (NODE * head) { NODE * p; if (head == NULL) printf(“NULL list”); else { for (p = head; p != NULL; p = p -> next) printf(“%d\n ”, p -> data); } }

Example 2 : Using Recursion void print_rec (NODE * head) { if (head == NULL) printf(“NULL list”); else { printf(“%d\n”, head -> data); print_rec(head ->next); }

Insertion of Elements in a List void insert(NODE * p1, NODE * p2, NODE * q) { assert (p1-> next == p2); /* if the expression inside assert is false, the system will print a message and the program will be aborted */ p1->next = q; q->next = p2; } initially p2 p1 A next C next NULL q B next

How to call insert function typedef struct node { int data; struct node *next; } NODE; /* Function prototypes */ NODE * create_rec( int x[], int size); void insert(NODE * p1, NODE * p2, NODE * q) void print_ite (NODE * head) main() { int x[4]={1,2,3,4}; NODE * head; NODE n; n.data=7; n.next=NULL; head=create(x,4); insert(head->next,head->next->next, &n); print_ite(head); }

Delete Elements of a List Example 1 : Using Iteration Example 2: Using Recursion

Example 1 : Using Iteration void delete (NODE * head) { NODE * p; NODE * q; if (head == NULL) printf(“NULL list”); else { p=head; while (p != NULL;) { q=p; p = p -> next ; free(q); } } }

Example 2 : Using Recursion void delete (NODE * head) { if (head != NULL) { delete(head ->next); free(head); }