هياكلبيانات

511 views
452 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
511
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

هياكلبيانات

  1. 1. Data structures And Algorithms
  2. 2. References :- -Data Structures and program design in C++-classical data structures-Starting out with C++ ( 5th edition)
  3. 3. Data and Information  Data is all things and facts that may be numbers ,letters ,images or sounds or a mixed of all of themthat are processed by computer programs .Information is the data after processing. ProcessingData Information
  4. 4. Processing operationsThe types of processing are1- insertion2- deletion3- merge4- sorting5- searching6- analysis.Data structure is the way how you c anConnect between data into memory to beone item.
  5. 5. Physical data structure is the space to store the data inmemory.Logical data structure is the opinion of user about howto program and deal with the data.Data structures types:1- Static data structure:Which has a fixed size like arrays , tables, records, etc.2- Dynamic Data Structurewhich has a variant size, it has two types:
  6. 6. ** linear data structures The data distribute in one level - Lists - Stacks - Queues** Non-linear data structures: The data distribute in many levels and used pointers ineach item to point to the next item into memory in orderto facilitate the insertion and deletion operations, like treeand graph. To select an appropriate data structure for specific data,there are many factors such as size of data, spacestorage, the dynamic of data and the programmingtechniques.
  7. 7. ArraysArray is a finite ordered set of elements ofthe same type, all integers, or allcharacters but not may contains both. Thesize of array can not change duringprogram’sexecution.Concept : An array allows you to store and work withmultiple values of the same data type.
  8. 8. Properties of Arrays 1- It has a limit number of elements, and in one type.2- It has fixed size.3- Easy to access any element into array using the address of first element.
  9. 9. There are many types of arrays: 1- One dimensional array.2- Two dimensional array.3- Three dimensional array. Even though an entire array has only one name,the elements may be accessed and used asindividual variables. This is possible because eachelement is assigned a number used as index topinpoint a specific element within an array.
  10. 10. Why use an array? There are many reasons, one of the most important isthat once the data is in the array it can be used morethan once without having to be input again.
  11. 11. One dimensional array  Note : the subscript (index) numbering in C++starts at zero.Concept :- Arrays may be initialized where they aredefined.int a[7] = {2, 5, 9, 3, 7, 8, 6}; a(0) a(1) a(2) a(3) a(4) a(5) a(6) 2 5 9 3 7 8 6
  12. 12. We can compute the address of any element (I) byusing the relation: Location (a(i)) = Base address + (I-1) * sizeWhere : Base address: is the address of the first element size : is the size of the location in memory
  13. 13. Two dimensional array It is a table consists of a number of rows and columns, so it has twoindices one for row number and one for column number. We can compute the address of any elements in two ways:1- Row-wise method In this way the elements store in memory row by rowsequentially. The equation in array a[N,M] is : location a[i,j]=base address+[M*(i-1)]+j-1]*size where: base address: address of first location M : is number of columns
  14. 14. 2- Column-wise method In this way the elements store in memory column by column sequentiallyThe equation to compute the location a[I,j] is : location a[i,j]=base address+[N*(j-1)]+i-1]*sizewhere: base address: address of first location N : is number of rows
  15. 15. Stacks  A Stack is a data structure that holds a sequence of elements.Unlike arrays and list new items may be inserted, or items may bedeleted at one end called the top of stack. The items is stored andretrieved in a Last-In-First-Out manner.Stack Operations: A stack has two primary operations: push and pop. The pushoperation causes a value to be stored or pushed onto the stack. Thepop operation retrieves a value from the stack.
  16. 16. In push operation, if the stack is not full, item is added to the top of the stack. If the stack is full, an error ofOverflow is returned, and the stack is unchanged.Algorithm PUSH  if top >= size of stack  error “stack overflow”  else increment top  add item. 
  17. 17. In pop operation, if the stack is not empty, item is deleted from the top of the stack. If the stack is empty, an error ofUnderflow is returned, and the stack is unchanged.Algorithm POP  if stack is empty  error “stack underflow”  else delete element  Decrement top  
  18. 18. The program is:- #include <iostream.h>void push (int s[],int item);bool empty();int i,top=5,item,s[5];void main(){cout<<"enter elements....."<<endl;for(i=0;i<top;i++)cin>>s[i];push(s,item);for(i=0;i<=top;i++)cout<<s[i];}
  19. 19. void push(int s[],int item){if(empty())cout<<"overflow";else{++top;cout<<"enter item "<<endl; cin>>item;s[top]=item; }}bool empty(){if(top>=5)return true;else return false;}
  20. 20. Applications of stack 1- In Arithmetic Expressions To keep the number and type of parenthesisThere are three forms of Arithmetic expressions** Infix Notation** Postfix Notation** prefix NotationEvaluate a postfix Notation:Each time an operand is read, it is pushed onto stack. When anoperator is reached, it is executed on the two top elements of thestack(poped and performed the indicated operation on them)The result will pushed on the stack, so that it will be available foruse as an operand of the next operator.
  21. 21. 2- In Recursion:  The concept of defining a function, so that it calls itself as a  subroutine. The main benefit is that it allows to take advantage of the repeated structure present in many problems, i.e. it is useful way for defining objects that have a repeated similar structural form, such as :Void message(){ cout<<“This is a recursive functionn”; message();}In this function there is no way to stop, so to be useful, it mustcontains a condition to stop as follows:
  22. 22. void message(int times)  { if(times >0) { cout<<“This is a recursive functionn”; message(times-1);} } Another example is the factorial of non-negative integer  number (n): 1 If n=0n!= n*(n-1)! If n>0 The function to find the factorial of non-negative integer number is asshown below:
  23. 23. The function is : Int fact (int n) { If(n==0) Return 1; Else Return n*fact(n-1)}Another example is the sum of elements in array like a[i] : a[0] if n=1 sum(a) = sum[a,n-1]+a[n-1] if n>1
  24. 24. Queue:  It is an ordered collection of items, which may be deleted at on end called the front of the queue, or may be inserted at the otherend called the rear of the queue.A queue provides access to it’s elements in First In First Out elements in queue are processed like (FIFO) order. Thecustomers standing in a grocery checkout line. The first customerin the line is the first one served.The figure below illustrate simple queue contains three elements A, B, C: front A B C rear
  25. 25. Operations on queue: --Insertion  to insert an element into a linear queue ,it must be check if it is full or not as shown below:Void add_q(int q[],int item){if (r>=size-1) cout<<“OVERFLOW”; elseq[r++]=item; if (f==0) f=1;}
  26. 26. --Deletion  to delete one element from a linear queue ,it must be check if it is empty ornot as shown below:Void del_q(int q[],int item){if (f=-1) cout<<“UNDERFLOW”; else {q[f]=-1; if (f==r) f=-1;r=-1;} else f++;}}
  27. 27. Pointers  A pointer is a variable contains an address of location in memory.Every pointer points to one object, but not more, or don’t points to any object i.e.has a value NULL.Two pointers may point to one object. When one object in memory lost i.e. nopointer points to it it’s called Garbage.In C++ to create a pointer points to object: int *p=new int
  28. 28. Linked Linear List A linked list is a series of connected nodes, where each node is a data structure consistsof two parts, Data and a pointer to the nextnode. The nodes of linked list are usuallydynamically allocated, used and then deleted,allowing the linked list to grow or shrink insize as the program runs. If new informationneeds to be added to linked list, the programsimply allocates another node and insert itinto the series. If a particular piece ofinformation needs to be removed from L.L.L,the program deletes the node containing thatinformation.
  29. 29. Advanta 

×