Upload
others
View
4
Download
0
Embed Size (px)
Citation preview
منتديات فيجول سي العزب / حقوق النشز 1
(Data Structures)
مزعي سليمان / تنسيك وتزتية SudaNix/ شزح وتنفيذ
رجة النتشة /مديز المنتدى
20092010فيجوال سي العزب / حقوق الطبع والنشز محفوظة لمنتديات
منتديات فيجول سي العزب / حقوق النشز 2
( Data Structures)
Definition Arrays
( Array)
: A اعالن عن مصفٌفة
:كىد
int A[5];
5.
:االجابة
.
( Data Structures)
/!
3
.
يلجؤوى اىل التفلري بًًاكل اخزىاملربجمنيكل يذه اسباب جتعل
منتديات فيجول سي العزب / حقوق النشز 3
"
!!
.
A 5
problems
.
فّلز
Data Structures اعتكد اى االى وضح سبب احلاجة اىل
!! املصفٌفات هُ االساس يف معظه هَاكل البَاىات: االجابة
منتديات فيجول سي العزب / حقوق النشز 4
Stack
:
.
top
.
.
.
:
2 46810 .
:
246810 .
(Stack)
.
منتديات فيجول سي العزب / حقوق النشز 5
:
108642 .
.
Last in First Out
LIFO
1 .
2 "
3 LIFO .
4 .
5
-
top
- top pointer int
10
top 1.
* push .
* pop
:اىظس الصٌز التٌضَحَة
خالصة
منتديات فيجول سي العزب / حقوق النشز 6
"
++
"
++
// Stack using OOD
#include <iostream>
#include <stdlib.h>
using namespace std;
class Stack
{
private:
enum { MAX=10,MIN=-1}; // MAXIMUM,MINIMUM STACK CONTENT
int array[MAX]; // Contains all the Data
int top; //Contains location of Top most Data pushed onto Stack
public:
Stack();
void push(int); // push number to array
int pop(); // pop it from array
bool isEmpty(); // check if array is Empty!
bool isFull(); // check if it full
};
Stack::Stack():
top(MIN)
{}
bool Stack::isEmpty()
{
if(top == MIN)
return true;
else
return false;
}
bool Stack::isFull()
{
if(top == MAX -1)
return true;
else
return false;
}
منتديات فيجول سي العزب / حقوق النشز 7
void Stack::push(int i)
{
if(isFull())
{
cout<<"\nArray is Full";
exit(1);
}
else
{
++top;
array[top]=i;
}
}
int Stack::pop()
{
int number;
if(isEmpty())
{
cout<<"\nArray is Empty";
exit(1);
}
else
{
number=array[top];
top--;
}
return number;
}
int main()
{
Stack s;
// push numbers 2,4,8
s.push(2);
cout<<"\n2 is pushed\n";
s.push(4);
cout<<"\n4 is pushed\n";
s.push(8);
cout<<"\n8 is pushed\n";
// pop numbers
cout<<"\n"<<s.pop()<<" is poped"<<endl;
cout<<s.pop()<<" is poped"<<endl;
منتديات فيجول سي العزب / حقوق النشز 8
cout<<s.pop()<<" is poped"<<endl;
return 0;
}
:
// Stack using C Language
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
#define MIN -1
struct Stack
{
int array[MAX];
int top;
};
void push(Stack *,int);
int pop(Stack *);
void initializeStack(Stack *);
int main()
{
Stack s;
initializeStack(&s);
push(&s,2);
printf("\n2 is pushed\n");
push(&s,4);
printf("\n4 is pushed\n");
push(&s,8);
printf("\n8 is pushed\n");
// pop numbers
printf("\n %d is pushed\n",pop(&s));
printf("\n %d is pushed\n",pop(&s));
printf("\n %d is pushed\n",pop(&s));
return 0;
}
void initializeStack(Stack *s)
{
(*s).top=MIN;
}
void push(Stack *s,int number)
{
if( (*s).top == MAX -1 )
منتديات فيجول سي العزب / حقوق النشز 9
{
printf("\nArray is Full");
exit(1);
}
(*s).top+=1;
(*s).array[(*s).top]=number;
}
int pop(Stack *s)
{
if( (*s).top == MIN )
{
printf("\nArray is Empty");
exit(1);
}
int n=(*s).array[(*s).top];
(*s).top--;
return n;
}
g++ compiler رمذ انزشجمخ ة-
:
1 top
2 .
3 .
4 Trees
5 .
6 .
منتديات فيجول سي العزب / حقوق النشز 10
" :
.
: !!
:
:
int array[10];
for(int i=0;i<10;i++)
array[i]=i*2+1;
for(int i=9;i>=0;i--)
cout<<array[i];
1319
19171
:
two operations :
1 push.
2 pop
push
push( )
77
:
:
push(7);
pop
pop() :
منتديات فيجول سي العزب / حقوق النشز 11
:
int var=pop();
++
:
:
1- int main()
2- {
3- Stack s;
4- // push numbers 2,4,8
5- s.push(2);
6- cout<<"\n2 is pushed\n";
7- s.push(4);
8- cout<<"\n4 is pushed\n";
9- s.push(8);
10- cout<<"\n8 is pushed\n";
11- // pop numbers
12- cout<<"\n"<<s.pop()<<" is poped"<<endl;
13- cout<<s.pop()<<" is poped"<<endl;
14- cout<<s.pop()<<" is poped"<<endl;
15- return 0;
16- }
3Stack
!!
s Stack( )
s stack constructor
s :
10 array + top.
top -1 constructor .
منتديات فيجول سي العزب / حقوق النشز 12
5 push 2.
push 10
"
2
.
:
o
o 2
top index
:
index .
2
10 top "
-1
push
منتديات فيجول سي العزب / حقوق النشز 13
2
push
:
push(int i)
{
if(isFull())
{
cout<<"\nArray is Full";
exit(1);
}
else
{
++top;
array[top]=i;
}
}
.
top "
110
2 array[0] .
79
top 2.
32.
:
1"
".9
.
منتديات فيجول سي العزب / حقوق النشز 14
121314.
pop .
:
.
:
int x;
cout<<x;
:
int x=1;
scope
grabage
1 .
"
:
:
while(there is no new lesson)
{
cout<<" دليل+ املكدس هو مصفوفة ";
}
10
index .
منتديات فيجول سي العزب / حقوق النشز 15
pop
:
pop()
{
int number;
if(isEmpty())
{
cout<<"\nArray is Empty";
exit(1);
}
else
{
number=array[top];
top--;
}
return number;
}
!!
top 1.
push top 2.
:
:
number=array[top];
top :
:
number=array[2];
top 1.
.
.
منتديات فيجول سي العزب / حقوق النشز 16
:
:كود
Stack s;
s.push(10);
s.push(20);
s.pop();
s.push(30);
!!
.
: Stack v0.1 :
.
103200 .
1010
.
!!
:
؟ top كم قيمة املتغري
!!
؟ top كم قيمة املتغري
هي حم جيذ ، فهي رقىو ثحم انمشكهخ انشئسيخ في اننسخخ انسبثقخ ، حيث رجعم انمسزخذو : اننسخخ انمطىسح
class هى انزي سيذخم عذد انعنبصش، ونيس انشخص انزي قبو ثكزبثخ ال
.كمب رشغت في رنك ، 1 ايضب ، 20 فهى كزنك ، 7اي ارا اسدد ان يكىن عذد انعنبصش
منتديات فيجول سي العزب / حقوق النشز 17
.
":
.
: Stack v0.1 :
:
// Stack v0.1 using OOD
#include <iostream>
#include <stdlib.h>
using namespace std;
class Stack
{
private:
enum { MAX=10,MIN=-1};// MAXIMUM,MINIMUM STACK CONTENT
int *pArray; // pointer to array.
int top; //Contains location of Top most Data pushed onto Stack
int size; // contain the number of item(integer) you want
public:
Stack(int n=MAX);
~Stack();
void push(int); // push number to array
int pop(); // pop it from array
bool isEmpty(); // check if array is Empty!
bool isFull(); // check if it full
};
20
2030
1053 .
منتديات فيجول سي العزب / حقوق النشز 18
Stack::Stack(int n):
top(MIN),size(n)
{
pArray=new int[n];
}
Stack::~Stack()
{
delete pArray;
pArray=0;
}
bool Stack::isEmpty()
{
return (top==MIN?ture:false);
}
bool Stack::isFull()
{
return (top==size -1 ?ture:false);
}
void Stack::push(int i)
{
if(isFull())
{
cout<<"\nArray is Full";
exit(1);
}
else
{
++top;
*(pArray+top)=i;
// or use this : pArray[top]=i;
}
}
int Stack::pop()
{
int number;
if(isEmpty())
{
cout<<"\nArray is Empty";
exit(1);
}
else
{
number=pArray[top];
منتديات فيجول سي العزب / حقوق النشز 19
//or use this: number=*(pArray+top);
top--;
}
return number;
}
int main()
{
int number;
cout<<"\nEnter The Number you want\n\t\t>> ";
cin>>number;
Stack s(number);
// push numbers 2,4,8
s.push(2);
cout<<"\n2 is pushed\n";
s.push(4);
cout<<"\n4 is pushed\n";
s.push(8);
cout<<"\n8 is pushed\n";
// pop numbers
cout<<"\n"<<s.pop()<<" is poped"<<endl;
cout<<s.pop()<<" is poped"<<endl;
cout<<s.pop()<<" is poped"<<endl;
return 0;
}
push pop top
.
:
array[10];
!!
:
:
pArray=new int[x];
Dynamic Array
dynamic
منتديات فيجول سي العزب / حقوق النشز 20
"
malloc stdlib.h ".
.
:
int x=2;
if(x==3)
int a=new int;
a
:
:
int x=2;
if(x==3)
int a;
a .
static array dynamic array
منتديات فيجول سي العزب / حقوق النشز 21
stack
stack ".
scope
:
int main()
{
int x=2;
{
int y=4;
}
cout<<x;
cout<<y;
return 0;
}
y scope
free store heap ، heap
.
scope ال heap
:
:
int main()
{
int x=2;
int *y;
{
y=new int;
*y=4;
}
cout<<x;
منتديات فيجول سي العزب / حقوق النشز 22
cout<<*y;
delete y;
return 0;
}
100%
y 4 .
.
block :
:
int *y=new int;
" Memory Leak
fatal error
y
."
:
:
int main()
{
int x=2;
{
int *y=new int;
*y=4;
}
cout<<x;
cout<<*y;
y
منتديات فيجول سي العزب / حقوق النشز 23
return 0;
}
:
y block
stack.
block y
block .
.
linked List
:
:
int main()
{
Stack s;
s.push(2);
s.push(4);
s.push(8);
...etc
// pop numbers
return 0;
}
.
template.
منتديات فيجول سي العزب / حقوق النشز 24
Queue
: :
" .
Enqueue"
.
" Dequeue "
!!
!!
"" !!
CPU ال
:
.
Priority Queue " "
Queue
Data Structure
.
منتديات فيجول سي العزب / حقوق النشز 25
"
FIFO
first in first out
.
FIFO
.
FIFO .
.
Queue
FIFO Queue.
: لذا تابع معها
Queue
منتديات فيجول سي العزب / حقوق النشز 26
""
.
"
"125 -> "124 ->"123
o انظز الصور التوضيحية:
.
Queue
Queue .
Queue
:
1 Enqueue
2 dequeue
Queue
Queue :
".
.
front .
back .
منتديات فيجول سي العزب / حقوق النشز 27
.
.
C ,C++
FIFO Queue C++
:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Queue
{
private:
enum {MAX=10};
int array[MAX];
int back,front;
public:
Queue();
void enqueue(int e);
int dequeue();
bool isFull();
bool isEmpty();
};
Queue::Queue():
back(0),front(0)
{}
void Queue::enqueue(int e)
{
if(isFull())
{
cout<<"\nQueue is Full";
exit(1);
}
منتديات فيجول سي العزب / حقوق النشز 28
back=(back+1)%MAX;
array[back]=e;
}
int Queue::dequeue()
{
if(isEmpty())
{
cout<<"\nQueue is Empty";
exit(1);
}
front=(front+1)%MAX;
return array[front];
}
bool Queue::isFull()
{
if( (back+1)%MAX == front )
return true;
else
return false;
}
bool Queue::isEmpty()
{
if( back == front )
return true;
else
return false;
}
int main()
{
Queue q;
cout<<"\nenqueue(2)";
q.enqueue(2);
cout<<"\nenqueue(4)";
q.enqueue(4);
cout<<"\nenqueue(6)";
q.enqueue(6);
cout<<"\nDequeue elements\n";
cout<<"dequeue: "<<q.dequeue()<<endl;
cout<<"dequeue: "<<q.dequeue()<<endl;
cout<<"dequeue: "<<q.dequeue()<<endl;
return 0;
}
منتديات فيجول سي العزب / حقوق النشز 29
.
Queue:
:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Queue
{
private:
enum {MAX=10};
int *pArray;
int back,front;
int size;
public:
Queue(int s=MAX);
~Queue();
void enqueue(int e);
int dequeue();
bool isFull();
bool isEmpty();
};
Queue::Queue(int s):
size(s),back(0),front(0)
{
pArray=new int[s];
}
Queue::~Queue()
{
delete [] pArray;
pArray=0;
}
void Queue::enqueue(int e)
{
if(isFull())
{
cout<<"\nQueue is Full";
exit(1);
منتديات فيجول سي العزب / حقوق النشز 30
}
back=(back+1)%size;
pArray[back]=e;
}
int Queue::dequeue()
{
if(isEmpty())
{
cout<<"\nQueue is Empty";
exit(1);
}
front=(front+1)%size;
return pArray[front];
}
bool Queue::isFull()
{
if( (back+1)%size == front )
return true;
else
return false;
}
bool Queue::isEmpty()
{
if( back == front )
return true;
else
return false;
}
int main()
{
Queue q(10);
cout<<"\nenqueue(2)";
q.enqueue(2);
cout<<"\nenqueue(4)";
q.enqueue(4);
cout<<"\nenqueue(6)";
q.enqueue(6);
cout<<"\nDequeue elements\n";
cout<<"dequeue: "<<q.dequeue()<<endl;
cout<<"dequeue: "<<q.dequeue()<<endl;
cout<<"dequeue: "<<q.dequeue()<<endl;
return 0;
}
منتديات فيجول سي العزب / حقوق النشز 31
main :
:
1-int main()
2-{
3- Queue q(10);
4- cout<<"\nenqueue(2)";
5- q.enqueue(2);
6- cout<<"\nenqueue(4)";
7- q.enqueue(4);
8- cout<<"\nenqueue(6)";
9- q.enqueue(6);
10- cout<<"\nDequeue elements\n";
11- cout<<"dequeue: "<<q.dequeue()<<endl;
12- cout<<"dequeue: "<<q.dequeue()<<endl;
13- cout<<"dequeue: "<<q.dequeue()<<endl;
14- return 0;
15-}
Queue
constrcutor q .
q :
front + back + size
pArray
.
front
back
.
size .
منتديات فيجول سي العزب / حقوق النشز 32
: توضيحيةالصورة انظز ال
++++++++++++++++++++++++++
5 enqueue 2 .
:
:
void Queue::enqueue(int e)
{
if(isFull())
{
cout<<"\nQueue is Full";
exit(1);
}
back=(back+1)%size;
pArray[back]=e;
}
: قبل االدخال توضيحيةالصورة انظز ال
منتديات فيجول سي العزب / حقوق النشز 33
.
!!
back
back.
2 :
back %size "
011 .
back=1 , front =0
2 pArray[back]
:
pArray[1]
:ًهرا هٌ اًل ًاحد يف الطابٌز
74 :
back
منتديات فيجول سي العزب / حقوق النشز 34
back = 2 , front = 0
.
:
!!
!!
back
10 back 9
.
.
!!
isFull()
:
if( (back+1)%size == front )
منتديات فيجول سي العزب / حقوق النشز 35
front back .
back + 1 = front
.
back=9
:
:
if( back+1 == front )
back 9110 out of boundary
" "
enqueue
dequeue :
111213 :
.
back =3 , front = 0
: توضيحيةالصورة انظز ال
منتديات فيجول سي العزب / حقوق النشز 36
2 .
dequeue .
dequeue :
:
int Queue::dequeue()
{
if(isEmpty())
{
cout<<"\nQueue is Empty";
exit(1);
}
front=(front+1)%size;
return pArray[front];
}
..
:
""
front %
.
pArray[front];
:
منتديات فيجول سي العزب / حقوق النشز 37
:
:
:
.
:
isEmpty()
:
if(back == front)
.
منتديات فيجول سي العزب / حقوق النشز 38
:
1 index .
2 .
منتديات فيجول سي العزب / حقوق النشز 39
o
Refresh
: انظز الصور التوضيحية
.
!!
انظز الصور التوضيحية :
Linked ListSingle
منتديات فيجول سي العزب / حقوق النشز 40
:
o انظز الصور التوضيحية :
nodes .
" .
"
1 data.
2 pointer to node
.جممىعة العكد تلىى لها قائمة متصلة : تذكز دائما
منتديات فيجول سي العزب / حقوق النشز 41
Head NULL
Head
o انظز الصور التوضيحية :
head
NULL
.
NULL 0
منتديات فيجول سي العزب / حقوق النشز 42
head
!!
o انظز الصور التوضيحية:
: هري الصٌزة لكائنة بدًن مصاكل
:ما هريأ
(head)
!!
.
:
head
!!
!!
منتديات فيجول سي العزب / حقوق النشز 43
heap
heap
o انظز الصور التوضيحية:
heap
heap .
heap RAM
منتديات فيجول سي العزب / حقوق النشز 44
heap stack
stack identifier
:
int x;
:
:
int
x :
:
x=3;
:
:
int *p=&x;
*p=3;
. يو الهتًجة يٌ نفشًا يف الربنامج
5
5 int int.
منتديات فيجول سي العزب / حقوق النشز 45
heap
:
:
int *p=new int;
*p=3;
مهنة جدا الصورة ،، انظز الصور التوضيحية :
heap new
.
new *p
head
p
scope stack .
منتديات فيجول سي العزب / حقوق النشز 46
اإلجابة
:
delete p;
:انظز الصور التوضيحية
p
stray pointer
p
NULL
:بعد التعيني انظز الصور التوضيحية
Memory Leak
heap delete
منتديات فيجول سي العزب / حقوق النشز 47
p
o خرية األانظز الصور التوضيحية:
1( single Linked List)
2 ( double Linked List)
3- ( circular Linked List)
منتديات فيجول سي العزب / حقوق النشز 48
( single Linked List )
.
o انظز الصور التوضيحية:
:
(double Linked List)
o انظز الصور التوضيحية:
node :
12.
منتديات فيجول سي العزب / حقوق النشز 49
:
(circular Linked List)
o انظز الصور التوضيحية:
o
NULL
NULL
double Linked List
.
منتديات فيجول سي العزب / حقوق النشز 50
Linked ListDouble
1 (data)
2. (pNext)
3. (pPrevious) .
:
hello world .
Linked ListDouble
Linked ListDouble
1(data)
2-
NULL
3
NULL .
( circular Linked List)
دعها نشايده باستخدام !!باستخدام الطابىر " بًتزا يت "يل تذكز بزنامج
؟ الكىائم املتصلة املفزدة ، ونشىف ما يى الفزق بًهًم ، عملًا
منتديات فيجول سي العزب / حقوق النشز 51
(Queue) هذا هو شكله باستخدام الطابور ، انظز الصور التوضيحية
(Linked List)
:
:
منتديات فيجول سي العزب / حقوق النشز 52
20 "مدٍس املطعه ، ًمعى املداو"
20
: فاذا كان الطابٌز قبل حضٌز املدٍس بورا الصكل
:
stack queue
:
1. AddFirst
2. AddLast
3. AddAt
4. RemoveFirst
5. RemoveLast
6. RemoveFrom
منتديات فيجول سي العزب / حقوق النشز 53
:
data structures
class
class
.
.
.
1. Int
2. Node *pPrevious
3. Node *pNext
- .
// LinkedList , by Ahmad Essam.
#include <iostream>
using namespace std;
/////// Node Class //////
class Node
{
private:
int data;
Node *pNext;
Node *pPrevious;
public:
Node(int d):data(d),pNext(NULL),pPrevious(NULL){}
int
:
منتديات فيجول سي العزب / حقوق النشز 54
int getData()const{return data;}
void setData(int d){data=d;}
Node * getNext()const{return pNext;}
void setNext(Node *p){pNext=p;}
Node * getPrevious()const {return pPrevious;}
void setPrevious(Node *p){pPrevious=p;}
};
/////// LinkedList class ///////
class LinkedList
{
protected:
Node *pHead; //pointer to the first node
Node *pTail; //pointer to the last node
int nodeCounter; // count the number of nodes
Node * makeNode(int data); // make & return new node
void remove(Node *tmp); // remove node from linked list
public:
LinkedList();
~LinkedList();
// operations
void addFirst(int data);
void addLast(int data);
void addBefore(int index,int data);
void removeFirst();
void removeLast();
void displayInOrder()const;
void displayInReverseOrder()const;
void destroyList();
int getSize()const{return nodeCounter;}
};
LinkedList::LinkedList():
pHead(NULL),pTail(NULL),nodeCounter(0)
{}
LinkedList::~LinkedList()
{
destroyList();
}
منتديات فيجول سي العزب / حقوق النشز 55
void LinkedList::displayInOrder()const
{
if( !pHead )
{
cout<<"\nNULL";
return ;
}
for(Node *cur=pHead;cur!=NULL;cur=cur->getNext())
cout<<cur->getData()<<" -> ";
cout<<"NULL"<<endl;
}
void LinkedList::displayInReverseOrder()const
{
if( !pHead )
{
cout<<"\nNULL";
return ;
}
for(Node *cur=pTail;cur!=NULL;cur=cur->getPrevious())
cout<<cur->getData()<<" -> ";
cout<<"NULL"<<endl;
}
Node * LinkedList::makeNode(int data)
{
nodeCounter++;
return new Node(data);
}
void LinkedList::addFirst(int data)
{
// make new node
Node *pNew=makeNode(data);
// make the new node as the first node in the linked list
if( !pHead ) // if linkedlist is empty
pHead=pTail=pNew;
else // if there are nodes
{
pNew->setNext(pHead);
pHead->setPrevious(pNew);
pHead=pNew;
منتديات فيجول سي العزب / حقوق النشز 56
}
}
void LinkedList::addLast(int data)
{
// make new node
Node *pNew=makeNode(data);
// make the new node as the last node in the linked list
//if there is no node in the linked list , add node.
//otherwise move the new node to the last.
if( !pHead )
pHead=pTail=pNew;
else
{
pTail->setNext(pNew);
pNew->setPrevious(pTail);
pTail=pNew;
}
}
void LinkedList::addBefore(int index,int data)
{
// check the index to see if it's legal
if( index <1 || index > getSize() )
{
cerr<<"\nError: index is too hight/low ";
return;
}
if(index == 1)
{
addFirst(data);
return ;
}
// make new Node
Node *pNew=makeNode(data);
Node *pCur=pHead;
for(int i=1;i<index -1;i++,pCur=pCur->getNext());
pNew->setNext(pCur->getNext());
pCur->setNext(pNew);
}
منتديات فيجول سي العزب / حقوق النشز 57
void LinkedList::removeFirst()
{
if( !pHead )
{
cerr<<"\nThere is no any node to remove!!";
return ;
}
Node *pTmp=pHead;
pHead=pHead->getNext();
if(pHead)
pHead->setPrevious(NULL);
else
pHead=pTail=NULL;
remove(pTmp);
}
void LinkedList::remove(Node *tmp)
{
delete tmp;
nodeCounter--;
};
void LinkedList::removeLast()
{
if( !pHead )
{
cerr<<"\nThere is no any node to remove!!";
return ;
}
Node *pTmp=pTail;
pTail=pTail->getPrevious();
if(pTail)
pTail->setNext(NULL);
else
pTail=pHead=NULL;
remove(pTmp);
}
void LinkedList::destroyList()
{
Node *pTmp=pHead;
منتديات فيجول سي العزب / حقوق النشز 58
Node *pCur;
while( pTmp != NULL)
{
pCur=pTmp;
pTmp=pTmp->getNext();
remove(pCur);
}
}
/////// main //////////
int main()
{
LinkedList *p=new LinkedList;
for(int i=0;i<5;i++)
p->addLast(2*i+1);
cout<<"\nPrint linked list in order\n";
p->displayInOrder();
cout<<"\nPrint linked list in reverse order\n";
p->displayInReverseOrder();
cout<<"\nadd 20 befor the second node. \n";
p->addBefore(2,20);
cout<<"\nPrint linked list in order\n";
p->displayInOrder();
return 0;
}
// EOF
.: Node انفئخ انطجقخ
:
class Node
{
private:
int data;
Node *pNext;
Node *pPrevious;
public:
Node(int d):data(d),pNext(NULL),pPrevious(NULL){}
int getData()const{return data;}
void setData(int d){data=d;}
منتديات فيجول سي العزب / حقوق النشز 59
Node * getNext()const{return pNext;}
void setNext(Node *p){pNext=p;}
Node * getPrevious()const {return pPrevious;}
void setPrevious(Node *p){pPrevious=p;}
};
data pNext Node
pPrevious
.
access method
.
constructor 0 garbage" data
Node
LinkedList
(LinkedList ) الطبقة
.
1 (protected)
:
protected:
Node *pHead;
Node *pTail;
int nodeCounter;
Node * makeNode(int data);
void remove(Node *tmp);
Node
.
منتديات فيجول سي العزب / حقوق النشز 60
:
1 pHead pTail .
2 nodeCounter .
3 makeNode int parameter .
4. remove .
:
:( public ) انقسى االخش - 2
:
public:
LinkedList();
~LinkedList();
// operations
void addFirst(int data);
void addLast(int data);
void addBefore(int index,int data);
void removeFirst();
void removeLast();
void displayInOrder()const;
void displayInReverseOrder()const;
void destroyList();
int getSize()const{return nodeCounter;}
:
protected
protected private inheritance
protected , public
private
منتديات فيجول سي العزب / حقوق النشز 61
1 LinkedList .
2 LinkedList.
3 addFirst .
4 addLast .
5 addBefore .
6 removeFirst .
7 removeLast .
8 displayInOrder .
9 displayInReverseOrder .
10 destroyList .
11. getSize .
:
:
int main()
{
1- LinkedList *list=new LinkedList;
2-
3- for(int i=0;i<5;i++)
4- list->addLast(2*i+1);
5- cout<<"\nPrint linked list in order\n";
6- list->displayInOrder();
7- cout<<"\nPrint linked list in reverse order\n";
8- list->displayInReverseOrder();
9- cout<<"\nadd 20 befor the second node. \n";
10- list->addBefore(2,20);
11- cout<<"\nPrint linked list in order\n";
12- list->displayInOrder();
return 0;
}
: LinkedList heap stack.
heap :
منتديات فيجول سي العزب / حقوق النشز 62
:
LinkedList list;
heap stack constructor .
nodeCounter NULL=0 pHead
pTail
: اذا شكل الكائنة املتصلة سٌف ٍكٌن
23: loop
:
:
pHead
.
pTail
pTail .
منتديات فيجول سي العزب / حقوق النشز 63
.
1 NULL
.
:
pHead pTail pNext
pPrevious .
:
:
منتديات فيجول سي العزب / حقوق النشز 64
5 displayInOrder
.
7 : displayInReverseOrder
.
9 addBefore
:
11 .
متت حبند اهلل