רשימות. רשימה משורשרת typedef struct link{ int data; struct link * next; }link;

Slides:



Advertisements
Similar presentations
CSCE 3110 Data Structures & Algorithm Analysis
Advertisements

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.
CSCE 3110 Data Structures & Algorithm Analysis
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
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.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Doubly Linked Lists * In a Doubly Linked List each item points to both its predecessor and successor.
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.
Linked Lists Dr. Tim Margush University of Akron © 2009.
“380.txt”“370.txt”“toTLV.txt” 07:05,1,2,4 07:30,1,5 07:40,2,3,4 08:00,2,3,4 07:15,3,4,5,6 07:30,1,2,3,4 08:00,1,3,5 08:30,1,2,3,4,5,6 07:05,1,2,4 07:15,3,4,5,6.
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.
Abstract Data Type: List (optional, not required).
Doubly Linked List. COMP104 Doubly Linked Lists / Slide 2 Motivation * Doubly linked lists are useful for playing video and sound files with “rewind”
Pointers Example Use int main() { int *x; int y; int z; y = 10; x = &y; y = 11; *x = 12; z = 15; x = &z; *x = 5; z = 8; printf(“%d %d %d\n”, *x, y, z);
פוינטרים ומבנים. אתחול והדפסת מערך #include int * readArray(int size){ int *a, i; a= (int *) malloc (size* sizeof(int)); if (!a) return NULL; for (i=0;i
CS 192 Lecture 19 Winter 2003 February 9-10, 2004 Dr. Shafay Shamail.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
Lecture 25 Self-Referential Structures Linked Lists
Linked Lists. Dynamic Data Structure Applications –where amount of required memory is determined at run-time.
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.
1 Objectives of these slides: to describe linked lists in C 6 – Lists.
Algorithms and Data Structures
Computer Programming for Engineering Applications ECE 175 Intro to Programming.
 Head pointer  Last node  Build a complete linked list  Node deletion  Node insertion  Helpful hints.
Complex Structures Nested Structures Self referential structures A structure may have Data variables Internal structures/unions Pointer links Function.
Lists (2). Circular Doubly-Linked Lists with Sentry Node Head.
1 Queues and Lists. QUEUES Very similar to stacks The only difference between them is in the order in which elements are processed. A stack uses a last-in/first-out.
Programming Linked Lists. Collections Store collection of data  Online store - Items  University – Students  Library – books Until now we used arrays.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
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
CSCE 3110 Data Structures & Algorithm Analysis More on lists. Circular lists. Doubly linked lists.
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.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
Array Sort. Sort Pass 1 Sort Pass 2 Sort Pass 3.
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.
CPT: Lists/ Computer Programming Techniques Semester 1, 1998 Objective of these slides: –to describe linked lists in C 15. Lists.
الطابور QUEUE (abstract data type) واحد من هياكل البيانات الخطية الشائعة الاستخدام داخل البرامج. يحتوي علي عناصر من نفس النوع. من أنواع البيانات الخطية.
 Structures typedef struct student{ char name[30]; long id; int grades[8]; } Student_type, *pStudent_type;
 Dynamic Memory Allocation  Linked Lists  void main() {{ ◦ FILE *file; ◦ char file_name[] = “file.txt”; ◦ if ((file = fopen(file_name, "w")) ==
1 Data Structures and Algorithms Linked List. 2 Lists Lists The Linked List ADT Linked List The Linked List Class Definition Linked List Class implementation.
CPSC 252 Linked Lists III Page 1 Variations on Singly Linked Lists Inserting or deleting at the front of a list is different from at any other point in.
מבני נתונים רשימה מקושרת, מחסנית ותור
Linked List.
Doubly Linked List Review - We are writing this code
CSCE 3110 Data Structures & Algorithm Analysis
Problems with dynamic arrays
Linked list.
Queue data structure.
CSC172 Data Structures Linked Lists (C version)
CSCE 3110 Data Structures & Algorithm Analysis
CSC215 Homework Homework 11 Due date: Dec 19, 2016.
Recursion.
קורס תכנות שיעור 14: הסוף.
הרצאה 06 רשימות מקושרות קרן כליף.
Linked Lists Chris Wright Winter 2006.
קורס תכנות שיעור 13: רשימות מקושרות.
Linked Lists: Implementation of Queue & Deque
Programming Linked Lists.
מבוא לתכנות ב- Java תרגול 10 - רשימות מקושרות.
LINKED LIST.
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
프로그래밍2 및 실습 Sort Code 전명중.
General List.
Linked Lists.
컴퓨터 프로그래밍 기초 - 13th : 마지막 수업 -
Presentation transcript:

רשימות

רשימה משורשרת typedef struct link{ int data; struct link * next; }link;

סוגי הכנסה לרשימה

העומד בתור התאורטי void insertLast(link * head, link * newLink){ while (head->next!=NULL) head=head->next; head->next=newLink; newLink->next=NULL; }

"אני אחריך" void insertAfter (link *where, link *newLink){ newLink->next=where->next; where->next=newLink; }

"אני לפניך" link * insertBefore (link *head, link *where, link *newLink){ if (head==where){ newLink->next=head; return newLink; } while (head->next!=where) head=head->next; newLink->next=head->next; head->next=newLink; return head; }

"אני רק שאלה" link * insertFirst (link *head,link *newLink){ newLink->next=head; return newLink; }

פונקציות עזר void printList(link *head){ while (head){ printf("%d -> ", head->data); head=head->next; } printf("\n"); } link * createLink(int data, link * next){ link * l=(link *)malloc(sizeof (link)); if (l){ l->data=data; l->next=next; } return l; }

main void main(){ link * head; head=createLink(5,NULL); printList(head); head=insertFirst(head, createLink(7,NULL)); printList(head); insertLast(head,createLink(9,NULL)); printList(head); head=insertBefore(head,head->next,createLink(3,NULL)); printList(head); insertAfter(head->next->next,createLink(4,NULL)); printList(head); }

סוגי הוצאה מרשימה

"יש כרטיס מועדון??" link * removeFirst(link * head){ link *temp=head; head=head->next; free(temp); return head; }

משפרי עמדה link * removeLast(link * head){ link * temp=head; if (head->next==NULL){ free(head); return NULL; } while (head->next->next != NULL) head=head->next; free(head->next); head->next=NULL; return temp; }

"שכחתי לקנות במבה" link * removeLink(link *head, link *toRemove){ link * temp=head; if (head==toRemove) return removeFirst(head); while (head->next != toRemove) head=head->next; head->next=head->next->next; free(toRemove); return temp; }

הפיכת פונקציה ל void link * insertFirst(link *head, link *newLink){ newLink->next=head; return newLink; } void insertFirst(link **head, link *newLink){ newLink->next=*head; *head=newLink; }

הסתכלות רקורסיבית int findMax(link * head){ int restMax; if (head==NULL) return 0; restMax = findMax(head->next); if (restMax > head->data) return restMax; return head->data; }

רשימה דו כוונית

הגדרת המבנה typedef struct dlink{ int data; struct dlink * next; struct dlink * prev; }dlink;

הוספת חוליה void insertAfter1 (dlink *where, dlink *newLink){ newLink->next=where->next; newLink->prev=where; if (where->next!=NULL){ where->next->prev=newLink; } where->next=newLink; }

מחיקת חוליה dlink *removeLink1(dlink *head,dlink *toRemove){ if (toRemove->next != NULL) toRemove->next->prev = toRemove->prev; if (toRemove->prev != NULL) toRemove->prev->next = toRemove->next; else head=toRemove->next; free(toRemove); return head; }