data structurecs6.yolasite.com/resources/linked_lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص...

17
صفحة1 من17 Data structure An organization of information, usually in memory, for better algorithm efficiency, such as queue, stack, linked list, heap, dictionary, and tree, or conceptual unity, such as the name and address of a person. It may include redundant information, such as length of the list or number of nodes in a sub-tree. A specific family of algorithms for implementing an abstract data type. Abstract Data Type (ADT) A set of data values and associated operations that are precisely specified independent of any particular implementation. Mathematical description of an object with set of operations on the object. Algorithm A computer algorithm is a detailed step-by-step method for solving a problem by using a computer. Program = Algorithm + Data Structures A high level, language independent, description of a step-by-step process

Upload: others

Post on 01-Aug-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 1صفحة

Data structure

An organization of information, usually in memory, for better algorithm

efficiency, such as queue, stack, linked list, heap, dictionary, and tree, or

conceptual unity, such as the name and address of a person. It may

include redundant information, such as length of the list or number of

nodes in a sub-tree.

A specific family of algorithms for implementing an abstract data type.

Abstract Data Type (ADT)

A set of data values and associated operations that are precisely specified

independent of any particular implementation.

Mathematical description of an object with set of operations on the

object.

Algorithm

A computer algorithm is a detailed step-by-step method for solving a

problem by using a computer.

Program = Algorithm + Data Structures

A high level, language independent, description of a step-by-step process

Page 2: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 2صفحة

LINKED LIST

* 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

* Head: pointer to the first node

* The last node points to NULL

A linked list is a data structure that consists of a sequence of data records such

that in each record there is a field that contains a reference (i.e., a link) to the

next record in the sequence.

A linked list whose nodes contain two fields: an integer value and a link to the next node

Linked lists are among the simplest and most common data structures; they

provide an easy implementation for several important abstract data structures,

including stacks, queues and arrays.

The principal benefit of a linked list over a conventional array is that the order

of the linked items may be different from the order that the data items are stored

in memory or on disk. For that reason, linked lists allow insertion and removal

of nodes at any point in the list, with a constant number of operations.

On the other hand, linked lists by themselves do not allow random access to the

data, or any form of efficient indexing. Thus, many basic operations — such as

obtaining the last node of the list, or finding a node that contains a given datum,

or locating the place where a new node should be inserted — may require

scanning most of the list elements.

Each record of a linked list is often called an element or node. The field of each

node that contains the address of the next node is usually called the next link or

next pointer. The remaining fields are known as the data, information, value

or payload fields. The head of a list is its first node, and the tail is the last node

Page 3: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 3صفحة

Linear and circular lists

In the last node of a list, the link field often contains a null reference, a special

value that is interpreted by programs as meaning "there is no such node". A less

common convention is to make it point to the first node of the list; in that case

the list is said to be circularly linked; otherwise it is said to be open or linear.

A circular linked list

Single and double linked lists

Single-linked lists contain nodes which have a data field as well as a next field,

which points to the next node in the linked list.

A singly-linked list whose nodes contain two fields:

an integer value and a link to the next node

In a double-linked list, each node contains, besides the next-node link, a second

link field pointing to the previous node in the sequence. The two links may be

called forward(s) and backwards, or next and previous.

A doubly-linked list whose nodes contain three fields: an integer value, the link

forward to the next node, and the link backward to the previous node

Sentinel nodes

In some implementations, an extra sentinel or dummy node may be added before

the first data record and/or after the last one. This convention simplifies and

accelerates some list-handling algorithms; by ensuring that all links can be

safely dereferences and that every list always has a "first" and "last" node.

A sentinel node is a programming language used to speed up some operations on

linked lists and trees. It refers to a special type of object that represents the end

of a data structure. Linked list data structures may use a sentinel object to

indicate the end of a list. Similarly, a tree data structure can use a sentinel to

indicate a node without children. It is not always necessary to use a sentinel

node; often null is used instead.

Page 4: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 4صفحة

LINKED LIST OPERATIONS

Our node data structure will have two fields. We also keep a variable firstNode

which always points to the first node in the list, or is null for an empty list.

Traversal of a singly-linked list is simple, beginning at the first node and

following each next link until we come to the end:

The following code inserts a node after an existing node in a singly

linked list. The diagram shows how it works. Inserting a node before an existing

one cannot be done directly; instead, you have to keep track of the previous

node and insert a node after it.

record Node {

data; // The data being stored in the node

Node next // A reference to the next node,

null for last node

}

record List {

Node firstNode // points to first node of

list; null for empty list

}

node := list.firstNode

while node not null

(do something with node.data)

node = node.next

function insertAfter(Node node, Node newNode)

// insert newNode after node

newNode.next := node.next

node.next := newNode

Page 5: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 5صفحة

Inserting at the beginning of the list requires a separate function. This

requires updating first

Similarly, we have functions for removing the node after a given node, and for

removing a node from the beginning of the list. The diagram demonstrates the

former. To find and remove a particular node, one must again keep track of the

previous element

Many of the special cases of linked list operations can be eliminated by

including a dummy element at the front of the list. This ensures that there are no

special cases for the beginning of the list and renders both insertBeginning() and

removeBeginning() unnecessary. In this case, the first useful data in the list will

be found at list.firs.

function insertBeginning(List list, Node newNode) // insert

node before current first node

newNode.next := list.firstNode

list.firstNode := newNode

function removeAfter(node node) // remove node past this one

obsoleteNode := node.next

node.next := node.next.next

destroy obsoleteNode

function removeBeginning(List list) // remove first node

obsoleteNode := list.firstNode

list.firstNode := list.firstNode.next // point past deleted node

destroy obsoleteNod

Page 6: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 6صفحة

اإلضافــــــــــــة

#include "stdafx.h"

#include <iostream>

using namespace std;

//------ class IntNode for creating new node -------------------//

class IntNode {

public :

IntNode(int el, IntNode *ptr = 0) {info = el; next = ptr;}

int info;

IntNode *next;

};

//------ class IntLList for dealing with nodes -----------------//

class IntLList {

public:

IntLList() {head = tail =0; }

void AddToHead(int);

void AddToTail(int);

void DisplayList( );

private:

IntNode *head, *tail;

};

void IntLList::AddToHead(int data)

{

IntNode *newnode;

newnode = new IntNode(data,0);

newnode->next = head;

head = newnode;

if (tail==0)

tail = head;

}

إضافة عنصر الى رأس القائمة

إضافة عنصر الى ذيل القائمة

القائمةعرض عناصر

Page 7: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 7صفحة

void IntLList::AddToTail(int data)

{

IntNode *newnode; newnode = new IntNode(data,0);

tail->next = newnode;

tail = newnode;

if (head==0)

head = tail;

}

void IntLList::DisplayList()

{

IntNode *current;

current = head;

cout << "head = " << head << "\n";

while(current != 0)

{

cout << current->info << " " << current << "\n";

current=current->next;

}

cout << "tail = " << tail << "\n";

cout << "----------------------" << "\n";

}

void main()

{

IntLList mag; mag.AddToHead(50);

mag.AddToHead(90);

mag.AddToHead(60);

mag.DisplayList();

mag.AddToTail(88);

mag.AddToTail(77);

mag.AddToHead(66);

mag.DisplayList();

}

Page 8: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 8صفحة

اإللغـــــــــــــــــاء

#include "stdafx.h" #include <iostream>

using namespace std;

//------ class IntNode for creating new node ------------------//

class IntNode {

public :

IntNode(int el, IntNode *ptr = 0) {info = el; next = ptr;}

int info;

IntNode *next;

};

//------ class IntLList for dealing with nodes ----------------//

class IntLList {

public:

IntLList() {head = tail =0; }

void AddToHead(int);

void AddToTail(int);

void DeleteFromHead();

void DeleteFromTail();

void DeleteNode(int);

void DisplayList();

private:

IntNode *head, *tail;

};

void IntLList::AddToHead(int data)

{

IntNode *newnode;

newnode = new IntNode(data,0);

newnode->next = head;

head = newnode;

if (tail==0)

tail = head;

}

void IntLList::AddToTail(int data)

{

IntNode *newnode;

newnode = new IntNode(data,0);

tail->next = newnode;

tail = newnode;

if (head==0)

head = tail;

}

Page 9: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 9صفحة

void IntLList::DeleteFromHead)(

{

if(head!=0)

head = head->next;

}

void IntLList::DeleteFromTail( )

{ IntNode *current; current = head; while(current->next != tail) {

current=current->next; } tail=current;

}

void IntLList::DisplayList ( )

}

IntNode *current;

current = head;

cout << "head = " << head << "\n";

while(current != 0)

}

cout << current->info << " " << current << "\n";

current=current->next;

}

cout << "tail = " << tail << "\n";

cout << "----------------------" << "\n";

}

Page 10: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 10صفحة

void main( )

{ IntLList mag; mag.AddToHead(50); mag.AddToHead(90); mag.AddToHead(60);

mag.DisplayList( ); mag.DeleteFromHead( ); mag.DisplayList( ); mag.AddToTail(88); mag.AddToTail(77);

mag.AddToHead(66);

mag.DisplayList( );

mag.DeleteFromTail( );

mag.DisplayList( ); }

Page 11: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 11صفحة

البحث عن قيمة معينة من داخل السلسلة

#include "stdafx.h" #include <iostream>

using namespace std;

//------ class IntNode for creating new node -----------------//

class IntNode {

public :

IntNode(int el, IntNode *ptr = 0) {info = el; next = ptr;}

int info;

IntNode *next;

};

//------ class IntLList for dealing with nodes ---------------//

class IntLList {

public:

IntLList( ) {head = tail =0; }

void AddToHead(int);

void AddToTail(int);

void DeleteFromHead( );

void DeleteFromTail( );

void DeleteNode(int);

void FindNode(int);

void DisplayList( );

private:

IntNode *head, *tail;

};

void IntLList::AddToHead(int data)

{

IntNode *newnode; newnode = new IntNode(data,0);

newnode->next = head;

head = newnode;

if (tail==0)

tail = head;

}

void IntLList::AddToTail(int data)

{

IntNode *newnode; newnode = new IntNode(data,0);

tail->next = newnode;

tail = newnode;

}

Page 12: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 12صفحة

void IntLList::DeleteFromHead()

{

if(head!=0)

head = head->next;

if(head==0)

tail=head;

}

void IntLList::DeleteFromTail()

{

IntNode *current;

current = head;

while(current->next != tail)

{

current=current->next;

}

tail=current;

tail->next=0;

}

void IntLList::FindNode(int val)

{

IntNode *current;

current = head;

while(current != tail->next)

{

if (current->info == val)

cout << current->info << " " << current << "\n";

current=current->next;

}

}

void IntLList::DisplayList()

{

IntNode *current;

current = head;

cout << "head = " << head << "\n";

while(current != tail->next)

{

cout << current->info << " " << current << "\n";

current=current->next;

}

cout << "tail = " << tail << " " << tail->next << "\n";

cout << "----------------------" << "\n";

}

Page 13: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 13صفحة

void main()

{

IntLList mag;

mag.AddToHead(50);

mag.AddToHead(90);

mag.AddToHead(60);

mag.DisplayList( );

mag.DeleteFromHead( );

mag.DisplayList( );

mag.AddToTail(88);

mag.AddToTail(77);

mag.AddToHead(66);

mag.AddToHead(50);

mag.DisplayList( );

mag.DeleteFromTail( );

mag.DisplayList( );

mag.FindNode(50);

}

Page 14: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 14صفحة

البحث عن قيمة معينة وإلغائها

#include "stdafx.h" #include <iostream>

using namespace std;

//------ class IntNode for creating new node ----------------//

class IntNode {

public :

IntNode(int el, IntNode *ptr = 0) {info = el; next = ptr;}

int info;

IntNode *next;

};

//------ class IntLList for dealing with nodes --------------//

class IntLList {

public:

IntLList() {head = tail =0; }

void AddToHead(int);

void AddToTail(int);

void DeleteFromHead();

void DeleteFromTail();

void DeleteNode(int);

void FindNode(int);

void DelNode(int);

void DisplayList();

private:

IntNode *head, *tail;

};

void IntLList::AddToHead(int data)

{

IntNode *newnode;

newnode = new IntNode(data,0);

newnode->next = head;

head = newnode;

if (tail==0)

tail = head;

}

void IntLList::AddToTail(int data)

{

IntNode *newnode;

newnode = new IntNode(data,0);

tail->next = newnode;

tail = newnode;

}

Page 15: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 15صفحة

void IntLList::DeleteFromHead( ) {

if(head!=0)

head = head->next;

if(head==0)

tail=head;

}

void IntLList::DeleteFromTail()

{

IntNode *current;

current = head;

while(current->next != tail)

{

current=current->next;

}

tail=current;

tail->next=0;

}

void IntLList::FindNode(int val)

{

IntNode *current;

current = head;

while(current != tail->next)

{ if (current->info == val)

cout << current->info << " " << current << "\n";

current=current->next;

}

cout << "----------------------" << "\n";

}

Page 16: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 16صفحة

void IntLList::DelNode(int val)

{ IntNode *current, *previous;

current = previous = head;

if(head->info==val)

head = current->next;

while(current != tail->next) {

if (current->info == val)

{

previous->next = current->next;

}

previous = current;

current=current->next;

}

}

void IntLList::DisplayList()

{ IntNode *current;

current = head;

cout << "head = " << head << "\n";

while(current != tail->next)

{

cout << current->info << " " << current << "\ n";

current=current->next;

{

cout << current->info << " " << current << "\n";

cout << "----------------------" << "\n";

}

Page 17: Data structurecs6.yolasite.com/resources/Linked_Lists123.pdf · 2011-03-12 · 17 نم 2 ةحفص LINKED LIST * A linked list is a series of connected nodes * Each node contains at

17 من 17صفحة

void main( )

{

IntLList mag;

mag.AddToHead(50);

mag.AddToHead(90);

mag.AddToHead(60);

mag.DisplayList( );

mag.DeleteFromHead( );

mag.DisplayList( );

mag.AddToTail(88);

mag.AddToTail(77);

mag.AddToHead(66);

mag.AddToHead(50);

mag.DisplayList( );

mag.DeleteFromTail( );

mag.DisplayList( );

mag.FindNode(50);

mag.DelNode(50);

mag.DisplayList( );

}