Linked List.

Slides:



Advertisements
Similar presentations
Linked Lists Mohammed Almashat CS /03/2006.
Advertisements

Linked Lists.
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
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.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Doubly-linked list library.
Chapter 6 Structures By C. Shing ITEC Dept Radford University.
Senem Kumova Metin Spring2009 BINARY TREES && TREE TRAVERSALS Chapter 10 in A Book on C.
Computer Programming Link List (Insertion, Printing and Deletion functions) Lecture 23.
Computer Science C++ High School Level By Guillermo Moreno.
Recursion practice. Problem 0 Using recursion (and no arrays), write the code to read in a series of numbers (until EOF) and then print them backwards.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Linked Lists A linked list is a series of connected nodes Each node contains at least –A piece of data (any type) –Pointer to the next node in the list.
Linked Lists. Example We would like to keep a list of inventory records – but only as many as we need An array is a fixed size Instead – use a linked.
1 Review of Class on Nov 30:. 2 Chapter 12: Structures and ADTs  Outline  Declaring Structures  Accessing a Member in a structure variable  Initialization.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
Reference: Vinu V Das, Principles of Data Structures using C and C++
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Linked Lists EENG 212 ALGORITHMS And DATA STRUCTURES.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
 Head pointer  Last node  Build a complete linked list  Node deletion  Node insertion  Helpful hints.
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
CIRCULAR L INKED L IST Insert as a first node Insert as a last node Delete first node Delete last node Insert after a node Insert before a node Search.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
Linked list: a list of items (nodes), in which the order of the nodes is determined by the address, called the link, stored in each node C++ Programming:
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
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)
Linked List. Insert a node at the beginning #include struct node { int data; struct node* next; }; struct node* head; void Insert(int x) { node *temp.
CSCE 3110 Data Structures & Algorithm Analysis
CSCE 3110 Data Structures & Algorithm Analysis
5.13 Recursion Recursive functions Functions that call themselves
Lectures linked lists Chapter 6 of textbook
UNIT – I Linked Lists.
Review Deleting an Element from a Linked List Deletion involves:
Linked list.
Data Structures 7th Week
Queue data structure.
Tree data structure.
Chapter 20: Binary Trees.
Stack Lesson xx   This module shows you the basic elements of a type of linked list called a stack.
Recursion.
Chapter 21: Binary Trees.
Tree data structure.
Tree data structure.
Linked List Insert as a first node Insert as a last node
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Tree data structure.
Review & Lab assignments
Recursive Linked Lists
Linked List.
LINKED LIST.
Linked Lists Adapted from Dr. Mary Eberlein, UT Austin.
Philip Bernhard, PhD Spring 2018
Linked Lists.
CS148 Introduction to Programming II
프로그래밍2 및 실습 Sort Code 전명중.
Recursive Linked Lists
CS148 Introduction to Programming II
Linked Lists Dr. Jose Annunziato.
Linked Lists.
Module 13 Dynamic Memory.
Presentation transcript:

Linked List

Linked List #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *link; }; // release the nodes of a linked list void release_linked_list(struct node **head) {struct node *p; while(*head) { p = *head; *head = (*head)->link; free(p); } *head = NULL; }

int size(struct node *head) // return the number of nodes in a linked list { // insert your code } // insert a new node after trail // if trail == NULL, the newnode is the new head of this linked list void linked_list_insert(struct node **head, struct node* trail, int data) { struct node* p = (struct node *) malloc(sizeof(struct node)); p->data = data; if (*head) { if (trail==NULL) { p->link = *head; *head = p; } else { p->link = trail->link; trail->link = p;} else { p->link = NULL; (*head) = p; } return;

// delete all nodes with data=k void linked_list_delete_data(struct node **head, int k) { // insert your code } void linked_list_reverse_data(struct node **head) // return a node with data == k struct node* linked_list_find(struct node *p, int k) { while(p && p->data != k) p = p->link; return p; } void output_linked_list(struct node* head) { while(head) { printf("%d\n",head->data); head = head->link;} return;}

int main() // Do not modify the main program { int op,data; struct node *head = NULL; scanf("%d%d", &op,&data); while(op != 0) { switch(op) { case 1: linked_list_insert(&head,NULL,data); // insert break; case 2: linked_list_delete_data(&head, data); // delete case 3: printf("%d %s\n", data, linked_list_find(head,data) ? "found" : "not found"); // search case 4: printf("%d elements in linked list\n",size(head)); // size case 5: linked_list_reverse_data(head); // reverse all nodes case 6: output_linked_list(head); // list all nodes } release_linked_list(&head); return 0;

Write a function size which returns the number of nodes in a linked list. Sample Input & Output (The black characters are input, the red characters are the output, and the blue characters are comments.) 1 1 1 2 1 3 1 4 1 5 4 0 5 elements in linked list 0 0

Write a function linked_list_delete_data which deletes all nodes with data == k.  Sample Input & Output (The black characters are input, the red characters are the output, and the blue characters are comments.) 1 1 1 2 1 3 1 4 1 5 4 0 6 elements in linked list 2 1 5 0 6 0 2 3 4 5 0 0