Linked Lists Chris Wright Winter 2006.

Slides:



Advertisements
Similar presentations
Chapter 3: Linked List Tutor: Angie Hui
Advertisements

Linked List Alternate approach to maintaining an array of elements Rather than allocating one large group of elements, allocate elements as needed Q: how.
Page 11 Solutions to Practice Problems Using a Linked List From Previous Days Notes Create C functions to solve the following problems with linked lists.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Stacks, Queues, and Linked Lists
Linked Lists.
CSCE 3110 Data Structures & Algorithm Analysis
CSEB324 Data Structures & Algorithms
Double Linked List Operations Dr. David Tsai 2010/4/12.
Linked Lists CSE 2451 Matt Boggus. Dynamic memory reminder Allocate memory during run-time malloc() and calloc() – return a void pointer to memory or.
418115: II. Linked List A linked list can be thought of a chain of linked list elements. A linked list element contains a single data item, and contains.
LIST PROCESSING.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Linked List Variations
CSCI2100B Linked List Jeffrey
Data Structure Lecture-5
Doubly-linked list library.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Data Structures: Doubly Linked List1 Doubly linked list l The linear linked list is accessed from the first node each time we want to insert or delete.
C Intro.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23.
Agenda  Review: pointer & array  Relationship between pointer & array  Dynamic memory allocation.
Discussion: Week 3/26. Structs: Used to hold associated data together Used to group together different types of variables under the same name struct Telephone{
Linked Lists list elements are stored, in memory, in an arbitrary order explicit information (called a link) is used to go from one element to the next.
Class 3: Linked Lists. cis 335 Fall 2001 Barry Cohen What is a linked list? n A linked list is an ordered series of ‘nodes’ n Each node contains some.
Computer Science 210 Computer Organization Pointers and Dynamic Storage.
Dynamic Allocation and Linked Lists. Dynamic memory allocation in C C uses the functions malloc() and free() to implement dynamic allocation. malloc is.
Last meeting..Doubly Linked List  InsertToFront  InsertToEnd  Search  DeleteNode.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Lecture 13 Static vs Dynamic Memory Allocation
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Linked Lists. A linear linked list is a collection of objects, called nodes, each of which contains a data item and a pointer to the next node in the.
Chapter 19 Data Structures. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Data Structures A data structure.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
ENEE150 – 0102 ANDREW GOFFIN Dynamic Memory. Dynamic vs Static Allocation Dynamic  On the heap  Amount of memory chosen at runtime  Can change allocated.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
CSCS-200 Data Structure and Algorithms Lecture
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
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.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
CS32 Discussion Section 1B Week 3 TA: Hao Yu (Cody)
Programming Circular Linked List.
Dynamic Allocation Review Structure and list processing
Linked List :: Basic Concepts
Computer Science 210 Computer Organization
Linked List.
Lecture No.03 Data Structures Dr. Sohail Aslam
Data Structures and Algorithms
Chapter 19 Data Structures
Linked Lists.
CSC172 Data Structures Linked Lists (C version)
A Doubly Linked List There’s the need to access a list in reverse order prev next data dnode header 1.
Linked List Sudeshna Sarkar.
Programmazione I a.a. 2017/2018.
Chapter 19 Data Structures
קורס תכנות שיעור 13: רשימות מקושרות.
Linked Lists (continued)
Computer Science 210 Computer Organization
Review & Lab assignments
Data Structures and Algorithms
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
ECE 103 Engineering Programming Chapter 63 Queue Implementation
CS148 Introduction to Programming II
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Queues: Implemented using Linked Lists
BY PROF. IRSHAD AHMAD LONE.
Linked Lists Dr. Jose Annunziato.
Module 13 Dynamic Memory.
Chapter 19 Data Structures -struct -dynamic memory allocation
Presentation transcript:

Linked Lists Chris Wright Winter 2006

Malloc void* malloc(size_t size); sizeof Returns a pointer to a block of memory of size size. If there is not enough memory it will return NULL. sizeof Gives the size_t of an item Works perfectly with malloc Data* dataptr = (Data*)malloc(sizeof(Data));

Free The counterpart to malloc Frees the memory set aside by malloc Must be used anytime you are done with a malloc’d block of memory. Data* dataptr = (Data*)malloc(sizeof(Data)); // Code using dataptr free(dataptr); dataptr = (Data*)malloc(sizeof(Data));

Linked List

Linked List add_end

Linked List delete_node

List.h (1) struct node_struct { int data; struct node_struct* next; struct node_struct* prev; }; typedef struct node_struct node; struct list_struct node* head; node* tail; typedef struct list_struct list;

List.h (2) list* create_list(); void delete_list(list* listptr); void add_end(list* listptr, int data); void delete_node(list* listptr, node* nodeptr);

List.c (1) list* create_list(){ list* listptr = (list *)malloc(sizeof(list)); listptr->head = NULL; listptr->tail = NULL; return listptr; } void delete_list(list* listptr){ while(listptr->head != NULL){ remove(listptr, listptr->head); free(listptr);

List.c (2) void add_end(list* listptr, int data) { node* last_node = listptr->tail; node* new_node = (node *)malloc(sizeof(node)); new_node->data = data; new_node->prev = last_node; new_node->next = NULL; if(last_node != NULL) last_node->next = new_node; listptr->tail = new_node; if(listptr->head == NULL) listptr->head = new_node; }

List.c (3) void delete_node(list* listptr, node* nodeptr) { node* prev_node = nodeptr->prev; node* next_node = nodeptr->next; if(prev_node == NULL) listptr->head = next_node; else prev_node->next = next_node; if(next_node == NULL) listptr->tail = prev_node; next_node->prev = prev_node; free(nodeptr); }