0% found this document useful (0 votes)
213 views50 pages

مفاهيم هياكل البيانات - عبدالفتاح المشرقي PDF

The document provides an introduction to data structures in C++. It discusses common data structures like stacks, queues, linked lists and binary trees. It provides code examples to demonstrate how to implement stacks and queues using arrays. Stacks follow the LIFO principle while queues follow the FIFO principle. Linked lists provide dynamic memory allocation and allow elements to be inserted/deleted. The document aims to help readers understand fundamental data structures and their implementation in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
213 views50 pages

مفاهيم هياكل البيانات - عبدالفتاح المشرقي PDF

The document provides an introduction to data structures in C++. It discusses common data structures like stacks, queues, linked lists and binary trees. It provides code examples to demonstrate how to implement stacks and queues using arrays. Stacks follow the LIFO principle while queues follow the FIFO principle. Linked lists provide dynamic memory allocation and allow elements to be inserted/deleted. The document aims to help readers understand fundamental data structures and their implementation in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

‫‪Data Structures‬‬

‫جامعة إب‬
‫مركز الحاسوب وتقنية المعلومات‬

‫عبد الفتاح عبد الرب‬


‫المشرقي‬

‫مايو ‪3102‬‬

‫‪Page 1 of 50‬‬
Page 2 of 50
C + +

INTODUCTION TO DATA STRUCTURES IN C++


Sanchit Karve
http://www.dreamincode.net/forums/topic/10157-data-structures-in-c-tutorial/

Assumptions 1
Introduction 2
Stacks 3
Queues 4
Linked Lists 5
Stacks using linked lists 6
Queues using linked lists 7
Circular linked lists 8
Binary search trees 9
Contact Me 11

ASSUMPTIONS
classes
C++

OOP

. C + +

INTRODUCTION

Advantages of Data Structure


1
2
3
4

Data Structures Types


Static DS 1
Vectors Arrays a
Strings b
Tables c
Records d
Page 3 of 50
Dynamic DS 2
Linear a
Stack i
Queue ii
Set iii
File iv
List v
Non-Linear b
Tree i
Graph ii

STACK 1
QUEUE 2
LINKED LISTS 3
BINARY TREES 4

DOUBLY LINKED LISTS


CIRCULAR LINKED LISTS
.

STACKS

TOP

Page 4 of 50
TOP TOP

processors
ESP TOP

popping
pushing

Page 5 of 50
Page 6 of 50
a push
1 top 1
MAX top 2
a top a
3
STACK IS FULL a
1 top b

pop
-1 top 1
STACK IS EMPTY a
b
2
data a
NULL b
1 top c
data d

Page 7 of 50
#include <iostream>

using namespace std;

#define MAX 10 //

class Stack
{
private:
int arr[MAX]; //
int top; //

public:
Stack() //
{
top = -1; // 1
}

void push(int a);


int pop();
};

void Stack::push(int a) //
{
top++; // 1
if(top < MAX)
{
//
arr[top] = a;
}
else
{
cout << "STACK FULL!!" << endl;
top--;
}
}

int Stack::pop() //
{
if(top == -1)
{
cout << "STACK IS EMPTY!!!" << endl;
return NULL;
}

Page 8 of 50
else
{
int data = arr[top]; //
arr[top] = NULL; //
top--; // 1
return data; //
}
}

int main()
{
Stack a;
a.push(3);
cout << "3 is Pushed\n";
a.push(10);
cout << "10 is Pushed\n";
a.push(1);
cout << "1 is Pushed\n\n";

cout << a.pop() << " is Popped\n";


cout << a.pop() << " is Popped\n";
cout << a.pop() << " is Popped\n";

return 0;
}

OUTPUT

3 is Pushed
10 is Pushed
1 is Pushed

1 is Popped
10 is Popped
3 is Popped

Last In first Out LIFO

top
-1
arr 1 top
10 10
linked
lists
queue

Page 9 of 50
QUEUE

First In First Out – FIFO First Come First Served


FRONT END
FRONT
FRONT

Stack Queue

Page 10 of 50
Page 11 of 50
-1
rear
front
3 4 5

Page 12 of 50
1 front 1
Queue is Empty a
2
rear 0=j a
rear j + 1 i
temp j + 1 1
j temp 2
ii
1 rear 1
front = -1 rear = -1 2
front = 0

rear = -1 front = -1 1
1 front a
1 rear b
2
1 rear a
MAX rear b
Queue is Full i
1 rear ii
iii
rear 3

#include <iostream>

using namespace std;

#define MAX 5 //

class Queue
{
private:
int t[MAX];
int rear; //
int front; //

public:
Queue()//
{
front = -1;
rear = -1;
}
Page 13 of 50
void del();
void add(int item);
void display();
};

void Queue::del()//
{
int tmp;
if(front == -1)
{
cout << "Queue is Empty";
}
else
{
for(int j = 0; j <= rear; j++)
{
if((j+1) <= rear)
{
tmp = t[j+1];
t[j] = tmp;
}
else
{
rear--;

if(rear == -1)
front = -1;
else
front = 0;
}
}
}
}

void Queue::add(int item)//


{
if(front == -1 && rear == -1)
{
front++;
rear++;
}
else
{
rear++;
if(rear == MAX)
{
cout << "Queue is Full\n";
rear--;
return;
}
}
t[rear] = item;
}
Page 14 of 50
void Queue::display()//
{
if(front != -1)
{
for(int i = 0 ; i <= rear ; i++)
cout << t[i] << " ";
}
else
cout << "EMPTY";
}

int main()
{
Queue a;//
int data[5] = {32, 23, 45, 99, 24};

cout << "Queue before adding Elements: ";


a.display();
cout << endl << endl;

for(int i = 0 ; i < 5 ; i++)


{
a.add(data[i]);
cout << "Addition Number : " << (i+1) << " : ";
a.display();
cout << endl;
}

cout << endl;


cout << "Queue after adding Elements: ";
a.display();
cout << endl << endl;

for(i = 0 ; i < 5 ; i++)


{
a.del();
cout << "Deletion Number : " << (i+1) << " : ";
a.display();
cout << endl;
}

return 0;
}

Page 15 of 50
OUTPUT
Queue before adding Elements: EMPTY

Addition Number : 1 : 32
Addition Number : 2 : 32 23
Addition Number : 3 : 32 23 45
Addition Number : 4 : 32 23 45 99
Addition Number : 5 : 32 23 45 99 24

Queue after adding Elements: 32 23 45 99 24

Deletion Number : 1 : 23 45 99 24
Deletion Number : 2 : 45 99 24
Deletion Number : 3 : 99 24
Deletion Number : 4 : 24
Deletion Number : 5 : EMPTY

Linked Lists
linked list
POINTER DATA

Dynamic Data Structure

Static Data Structure

pointers

12 15 29 45

12 15 29 45

Page 16 of 50
12 12, 15, 29, 45
29 15

NULL

NULL

15 15
22 12
15

q 1
q 2
p q 3
p = q 4

p
5
p
5
q

11

-10 p
q 5
q

q -10 5

q -10 5

Page 17 of 50
t q 1
p 2
p a
p b
NULL p c
3
q = p a
q q b
t c
t d
NULL t e
t q f

-10 5

111

q -10 5

-10 5

-10 5

100

Page 18 of 50
p

-10 5 100

q t

77

-10 5 100

-10 5 100

-10 5 100

-10 5 100

77

-10 5 100 77

q t

Page 19 of 50
c
t q 1
i = 0 2
q = p 3
c i 4
q a
NULL q b
i
ii
i c
t 5
t 6
q t 7
t q 8

-10 5 100 77

3 -700

i c

0 3
p

-10 5 100 77

i c

1 3
p

-10 5 100 77

Page 20 of 50
i c

2 3
p

-10 5 100 77

i c

3 3
p

-10 5 100 77

i c

3 3
p

-10 5 100 77

q
-700

i c

3 3
p

-10 5 100 -700 77

q t

Page 21 of 50
r q 1
q = p 2
q 3
q p a
q b
c
r = q 4
NULL q 5
q a
q r i
q ii
iii
r = q b
q c
6

-10

-10 5 100 -700 77

-10 5 100 -700 77

q
p

-10 5 100 -700 77

5 100 -700 77

Page 22 of 50
-700

5 100 -700 77

q
r

5 100 -700 77

r q

5 100 -700 77

r q

5 100

-700 77

5 100 77

200

5 100 77

q r

Page 23 of 50
p

5 100 77

r q

5 100 77

r q

5 100 77

r q

5 100 77

r q

#include <iostream>
using namespace std;

struct node //
{
int data;
node *next;
};

class LinkList
{
private:
node *p;
public:
LinkList();
void add_as_first( int num );
void append( int num );
void addafter( int c, int num );
void del( int num );
void display();
int count();
~LinkList();
};

Page 24 of 50
LinkList::LinkList()//
{
p = NULL;
}

void LinkList::add_as_first(int num)//


{
node *q;

q = new node;
q -> data = num;
q -> next = p;
p = q;
}

void LinkList::append(int num)//


{
node *q,*t;

if( p == NULL )
{
p = new node;
p -> data = num;
p -> next = NULL;
}
else
{
q = p;
while(q -> next != NULL )
q = q -> next;

t = new node;
t -> data = num;
t -> next = NULL;
q -> next = t;
}
}

void LinkList::addafter( int c, int num)//


{
node *q, *t;
int i;
for(i = 0, q = p; i < c; i++)
{
q = q -> next;
if( q == NULL )
{
cout << "\nThere are less than " << c << " elements.";
return;
}
}

Page 25 of 50
t = new node;
t -> data = num;
t -> next = q -> next;
q -> next = t;
}

void LinkList::del( int num )//


{
node *q,*r;
q = p;
if(q -> data == num )
{
p = q -> next;
delete q;
return;
}

r = q;
while( q != NULL )
{
if( q -> data == num )
{
r -> next = q -> next;
delete q;
return;
}

r = q;
q = q -> next;
}
cout << "\nElement " << num << " not Found.";
}

void LinkList::display()//
{
node *q;
cout << endl;

for(q = p ; q != NULL ; q = q -> next )


cout << endl << q -> data;

Page 26 of 50
int LinkList::count()//
{
node *q;
int c = 0;
for(q = p; q != NULL; q = q -> next )
c++;

return c;
}

LinkList::~LinkList()//
{
node *q;
if( p == NULL )
return;

while( p != NULL )
{
q = p -> next;
delete p;
p = q;
}
}

int main()
{
LinkList ll;
cout << "No. of elements = " << ll.count();
ll.append(12);
ll.append(13);
ll.append(23);
ll.append(43);
ll.append(44);
ll.append(50);

ll.add_as_first(2);
ll.add_as_first(1);

ll.addafter(3,333);
ll.addafter(6,666);

ll.display();
cout << "\nNo. of elements = " << ll.count();

ll.del(333);
ll.del(12);
ll.del(98);
cout << "\nNo. of elements = " << ll.count();
return 0;
}

Page 27 of 50
OUTPUT
No. of elements = 0
1
2
12
13
333
23
43
666
44
50
No. of elements = 10
Element 98 not found.
No. of elements = 8

class
p

LinkList(); //‫الباني‬
void append( int num ); //‫اإلضافة في نهاية القائمة‬
void add_as_first( int num ); //‫اإلضافة في بداية القائمة‬
void addafter( int c, int num ); //‫إضافة البيانات بعد موقع محدد‬
void del( int num ); //‫حذف بيانات محددة‬
void display(); //‫عرض بيانات القائمة المتصلة‬
int count(); //‫عدد العناصر في القائمة المتصلة‬
~LinkList(); //‫الهادم‬

q = q -> next
delete del destructor
new

doubly linked lists circular linked lists

Page 28 of 50
STACKS USING LINKED LISTS
MAXIMUM

#include <iostream>

using namespace std;

struct node//
{
int data;
node *next;
};

class LLStack
{
private:
node* top;

public:
LLStack()//
{
top=NULL;
}

void push(int n)//


{
node *tmp;
tmp = new node;
if(tmp == NULL)
cout << "\nSTACK FULL";

tmp -> data=n;


tmp -> next=top;
top = tmp;
}

int pop()//
{
if(top == NULL)
{
Cout << "\nSTACK EMPTY";
return NULL;
}
node *tmp;
int n;
tmp = top;
Page 29 of 50
n = tmp -> data;
top = top -> next;
delete tmp;
return n;
}

~LLStack()//
{
if(top==NULL)
return;

node *tmp;
while(top != NULL)
{
tmp = top;
top = top -> next;
delete tmp;
}
}
};

int main()
{
LLStack s;
s.push(11);
s.push(101);
s.push(99);
s.push(78);
cout << "Item Popped = " << s.pop() << endl;
cout << "Item Popped = " << s.pop() << endl;
cout << "Item Popped = " << s.pop() << endl;
return 0;
}

QUEUES USING LINKED LIST

#include <iostream>

using namespace std;

struct node//
{
int data;
node *next;
};

Page 30 of 50
class LLQueue
{
private:
node *front, *rear;

public:
LLQueue()//
{
front = NULL;
rear = NULL;
}

void add(int n)//


{
node *tmp;
tmp = new node;
if(tmp == NULL)
cout << "\nQUEUE FULL";

tmp -> data = n;


tmp -> next = NULL;
if(front == NULL)
{
rear = front = tmp;
return;
}
rear->next=tmp;
rear=rear->next;
}

int del()//
{
if(front == NULL)
{
Cout << "\nQUEUE EMPTY";
return NULL;
}
node *tmp;
int n;
n = front -> data;
tmp = front;
front = front -> next;
delete tmp;
return n;
}

Page 31 of 50
~LLQueue()//
{
if(front == NULL)
return;
node *tmp;
while(front != NULL)
{
tmp = front;
front = front -> next;
delete tmp;
}
}
};

int main()
{
LLQueue q;
q.add(11);
q.add(22);
q.add(33);
q.add(44);
q.add(55);
cout << "\nItem Deleted = " << q.del();
cout << "\nItem Deleted = " << q.del();
cout << "\nItem Deleted = " << q.del();
return 0;
}

CIRCULAR LINKED LISTS

*: *
: Windows
#include <windows.h>
#include <conio.h>
CirLinkedList wait() slideshow()
main() slideshow()

Page 32 of 50
#include <windows.h>
#include <iostream>
#include <conio.h>

using namespace std;

class CirLinkedList
{
private:
struct node//
{
int data;
node *next;
};

node *p;

public:
CirLinkedList();//
CirLinkedList(CirLinkedList& l);//
~CirLinkedList();//
void add(int);//
void del();//
void addatbeg(int);//
void display();//
void slideshow(float,int,int);//
int count();//
void wait(float);//
bool operator ==(CirLinkedList);
bool operator !=(CirLinkedList);
void operator =(CirLinkedList);
};

CirLinkedList::CirLinkedList()
{
p=NULL;
}

CirLinkedList::CirLinkedList(CirLinkedList& l)
{
node *x;
p = NULL;
x = l.p;
if(x == NULL)
return;

Page 33 of 50
for(int i = 1;i <= l.count(); i++)
{
add(x -> data);
x = x -> next;
}
}

CirNextedList::~CirNextedList()
{
node *q,*t;
q = p;
t = p;
if(p == NULL)
return;

while(q -> next != t)


{
p = q;
q = q -> next;
delete p;
}
p = q;
delete p;
}

void CirNextedList::add(int n)
{
if(p == NULL)
{
node *q;
q = new node;
q -> data = n;
q -> next = q;
p = q;
return;
}
node *q;
q = p;
while(q -> next != p)
q = q -> next;

node *t;
t = new node;
t -> data = n;
t -> next = p;
q -> next = t;
}

void CirNextedList::display()
{
if(p == NULL)
{
cout << "EMPTY LIST\n";

Page 34 of 50
return;
}
node *q;
q = p;
for(int i = 1; i <= this -> count(); i++)
{
cout << q -> data << endl;
q = q -> next;
}
}

int CirNextedList::count()
{
node *q;
q = p;
int c = 0;
if(p == NULL)
return 0;
else
c++;
while(q -> next != p)
{
c++;
q = q -> next;
}
return c;
}

void CirNextedList::del()
{
if(p == NULL)
return;

if(p -> next == p)


{
p = NULL;
}
else
{
node *q;
q = p;
while(q -> next != p )
q = q -> next;

q -> next = p -> next;


q = p;
p = (q -> next == NULL ? NULL : p -> next);
delete q;
}
}

Page 35 of 50
void CirNextedList::addatbeg(int n)
{
node *q,*t;
q = p;
while(q -> next != p)
q = q -> next;

t = new node;
t -> data = n;
t -> next = p;
q -> next = t;
p = t;
}

void CirNextedList::slideshow(float dlay,int x,int y)


{
/* if(p == NULL)
{
gotoxy(x,y);
cout << "EMPTY LIST\n";
return;
}
node *q;
q = p;
while(!kbhit())
{
gotoxy(x,y);
cout << " ";
gotoxy(x,y);
cout << q -> data;
wait(dlay);
q = q -> next;
}*/
}

void CirNextedList::wait(float t)
{
long time = GetTickCount()+(t*1000L);
while(GetTickCount() <= time)
{
/* WAIT !!! */
}
}

bool CirNextedList::operator ==(CirNextedList t)


{
if(t.p == NULL && p == NULL)
return 1;

if(this -> count() != t.count())


return 0;

node *q;
Page 36 of 50
q = p;
bool flag;
flag = 1;
node *a;
a = t.p;
for(int i = 1; i <= count(); i++)
{
if(a -> data != q -> data)
flag = 0;

a = a -> next;
q = q -> next;
}
if(a -> data != q -> data)
flag = 0;
return flag;
}

bool CirNextedList::operator !=(CirNextedList t)


{
return !(this -> operator == (t));
}

int main()
{

CirNextedList a;
a.add(1);
a.add(2);
a.add(3);
a.add(4);
a.addatbeg(128);
a.del(); // 128 is deleted
cout<<"\nLIST DATA:\n";
a.display();

CirNextedList b=a;
if(b!=a)
cout<<endl<<"NOT EQUAL"<<endl;
else
cout<<endl<<"EQUAL"<<endl;
a.slideshow(1,13,13);

return 0;
}

slideshow()
wait()
kbhit()
Page 37 of 50
1

struct node
{
int data;
node *next; //
node *prev; //
};

display 3

void NextList::display(int type)


{
if(type==1)
{
//
}
else
{
//
}
}

BINARY SEARCH TREES

BINARY
TREES

root main node


right half left half
15 11

Page 38 of 50
53 5 41 24 23 19 14 18 7 13 87 1 34 16 32

32 32 1
32 16 32 16 2
34 32 34 34 3
32 1 1 4
16 16 1 1
32 87 87 5
34 87 34 87 34
16 13 32 13 13 6
13 1 13 16
1
53 7
34 53 32 53 53 8
87 53 34
41 41 53 87
41 53 41

Page 39 of 50
1

5114118112123124141

preorder inorder transversal


postorder

PREORDER
1
2
3

Page 40 of 50
INORDER
1
2
3

Page 41 of 50
POSTORDER
1
1
2

recursive

inorder

Page 42 of 50
#include <iostream>

using namespace std;

#define YES 1
#define NO 0

class Tree
{
private:
struct leaf
{
int data;
leaf *l;
leaf *r;
};
leaf *p;

public:
Tree();
~Tree();
void destruct(leaf *q);
Tree(Tree& a);
void findparent(int n,int &found,leaf* &parent);
void findfordel(int n,int &found,leaf *&parent,leaf* &x);
void add(int n);
void transverse();
void in(leaf *q);
void pre(leaf *q);
void post(leaf *q);
void del(int n);
};

Tree::Tree()
{
p = NULL;
}

Tree::~Tree()
{
destruct(p);
}

Page 43 of 50
void Tree::destruct(leaf *q)
{
if(q!=NULL)
{
destruct(q -> l);
del(q -> data);
destruct(q -> r);
}
}
void Tree::findparent(int n,int &found,leaf *&parent)
{
leaf *q;
found = NO;
parent = NULL;

if(p == NULL)
return;

q = p;
while(q != NULL)
{
if(q -> data == n)
{
found = YES;
return;
}
if(q -> data > n)
{
parent = q;
q = q -> l;
}
else
{
parent = q;
q = q -> r;
}
}
}

Page 44 of 50
void Tree::add(int n)
{
int found;
leaf *t,*parent;
findparent(n, found, parent);
if(found == YES)
cout << "\nSuch a Node Exists";
else
{
t = new leaf;
t -> data = n;
t -> l = NULL;
t -> r = NULL;

if(parent == NULL)
p = t;
else
parent -> data > n ? parent -> l = t : parent -> r = t;
}
}

void Tree::transverse()
{
int c;
cout << "\n1.InOrder\n2.Preorder\n3.Postorder\nChoice: ";
cin >> c;
switch(c)
{
case 1:
in(p);
break;
case 2:
pre(p);
break;
case 3:
post(p);
break;
}
}

void Tree::in(leaf *q)


{
if(q != NULL)
{
in(q -> l);
cout << "\t" << q -> data << endl;
in(q -> r);
}

Page 45 of 50
void Tree::pre(leaf *q)
{
if(q != NULL)
{
cout << "\t" << q -> data << endl;
pre(q -> l);
pre(q -> r);
}
}

void Tree::post(leaf *q)


{
if(q != NULL)
{
post(q -> l);
post(q -> r);
cout << "\t" << q -> data <<endl;
}

void Tree::findfordel(int n,int &found,leaf *&parent,leaf *&x)


{
leaf *q;
found = 0;
parent = NULL;
if(p == NULL)
return;

q = p;
while(q != NULL)
{
if(q -> data == n)
{
found = 1;
x = q;
return;
}
if(q -> data > n)
{
parent = q;
q = q -> l;
}
else
{
parent = q;
q = q -> r;
}
}
}

Page 46 of 50
void Tree::del(int num)
{
leaf *parent,*x,*xsucc;
int found;

// If EMPTY TREE
if(p == NULL)
{
cout << "\nTree is Empty";
return;
}
parent = x = NULL;
findfordel(num, found, parent, x);
if(found == 0)
{
cout << "\nNode to be deleted NOT FOUND";
return;
}

// If the node to be deleted has 2 leaves


if(x -> l != NULL && x -> r != NULL)
{
parent = x;
xsucc = x -> r;

while(xsucc -> l != NULL)


{
parent = xsucc;
xsucc = xsucc -> l;
}
x -> data = xsucc -> data;
x = xsucc;
}

// if the node to be deleted has no child


if(x -> l == NULL && x -> r == NULL)
{
if(parent -> r == x)
parent -> r = NULL;
else
parent -> l = NULL;

delete x;
return;
}

Page 47 of 50
// if node has only right leaf
if(x -> l == NULL && x -> r != NULL )
{
if(parent -> l == x)
parent -> l = x -> r;
else
parent -> r = x -> r;

delete x;
return;
}

// if node to be deleted has only left child


if(x -> l != NULL && x -> r == NULL)
{
if(parent -> l == x)
parent -> l = x -> l;
else
parent -> r = x -> l;

delete x;
return;
}
}

int main()
{
Tree t;
int data[] = {32,16,34,1,87,13,7,18,14,19,23,24,41,5,53};
for(int iter = 0 ; iter < 15 ; i++)
t.add(data[iter]);

t.transverse();
t.del(16);
t.transverse();
t.del(41);
t.transverse();
return 0;
}

OUTPUT

1.InOrder
2.Preorder
3.Postorder
Choice: 1
1
5
7
13
14

Page 48 of 50
16
18
19
23
24
32
34
41
53
87

1.InOrder
2.Preorder
3.Postorder
Choice: 2
32
18
1
13
7
5
14
19
23
24
34
87
41
53

1.InOrder
2.Preorder
3.Postorder
Choice: 3
5
7
14
13
1
24
23
19
18
53
87
34
32
Press any key to continue.

Page 49 of 50
NOTE
Turbo Visual C++
C++

inorder
Sorting {BST} Binary Search Trees

leaf 1
2
3
4

CASE 1
CASE 2
NULL
NULL

CASE 3

CASE 4
inorder

16
5 5
3 2

Compression
Sorting Binary Searching Algorithms

Contact me

[email protected]
[email protected]
[email protected]

Page 50 of 50

You might also like