Professional Documents
Culture Documents
Linked Lists: // Node.h
Linked Lists: // Node.h
Linked Lists: // Node.h
-1
( )
.
. (Time Run)
(Next)
.NULL
10 20 30 NULL
Head
of list
.(Head of list)
: Node
// Node.h
class Node
{
friend class List; //Node List
private:
int data;
Node *nextPtr;
public:
Node() // constructor
{
}
};
1
: List
// List.h
# include <iostream>
using namespace std;
class List
{
private:
Node *head;
public:
List() // constructor
{
head = NULL;
}
bool IsEmpty()
{
return (head == NULL);
}
void InsertAtFront(int info);
void InsertAtBack(int info);
void Print();
};
void List::Print()
{
Node *p = head;
while (p != NULL)
{
cout<<p->data<<"\t";
p = p->nextPtr;
}
}
. head List
: List
2
(head = NULL) List() •
IsEmpty() •
InsertAtFront() •
InsertAtBack() •
. Print() •
L
:
//TestList.cpp
#include <iostream>
#include "Node.h"
#include "List.h"
int main()
{
List L;
L.InsertAtFront(10);
L.InsertAtFront(20);
L.InsertAtBack(30);
L.Print();
cout<<endl;
return 0;
}
PrintRecursive() List
.
void List::PrintRecursive()
{
if (head != NULL)
{
cout<<head->data<<"\t";
head = head->nextPtr;
PrintRecursive();
}
}
.L
3
int CountItemsIter(Node *L) int CountItemsRecur(Node *L)
{ {
int n = 0; if (L == NULL)
while (L != NULL) return 0;
{ else
n++; return 1+CountItemsRecur(L-
L = L -> Next; >Next);
} }
return n;
}
3
.L x
L 10 20 30 NULL
L 10 20 30 NULL
x L
.L x
L 10 20 30 NULL
L
4
L 10 20 30 NULL
x
L
5
.L x
"Found…." •
"Not Found…." •
5
return true;
else
return SearchItem(x, P -> Next);
}
6
x ) L x
(
L 10 20 30 NULL
L 30 10 NULL
20 L
L 10 20 30 NULL
L 30 20 10 NULL
L
6
Node* InvertList(Node *L)
{
Node *P = L;
Node *Q = NULL;
Node *R;
while (P != NULL)
{
R = new(Node);
R -> Data = P->Data ;
R -> Next = Q;
Q = R;
P = P->Next;
}
return Q;
}
8
.(Bubble Sort) L
L 20 30 10 NULL
L 10 20 30 NULL
7
(Circular linked lists) -2
. NULL
L 20 10 30 NULL
L 10 20 30
L :1
L :2
.(Q)
8
{
cout<<P->Data<<"\t";
P = P->Next;
}
}
}
(Doubly-linked lists) -3
Previous -
Next -
:(Last) (First)
struct Node
{
Node *Prev;
int Data;
Node *Next;
}
struct Dbl_list
{
Node *First;
Node *Last;
}
- void ShowListBackward(Dbl_List L) //
9
•
#include <iostream>
using namespace std;
struct Node
{
Node *Prev;
int Data;
Node *Next;
};
struct Dbl_List
{
Node *First;
Node *Last;
};
Dbl_List AddFront(int x, Dbl_List L)
{
Node *P;
if (L.First == NULL)
{
P = new(Node);
P->Prev = NULL;
P->Data = x;
P->Next = NULL;
L.First = P;
L.Last = P;
}
else
{
P = new(Node);
P->Prev = NULL;
P->Data = x;
P->Next = L.First;
L.First->Prev = P;
L.First = P;
}
return L;
}
Dbl_List AddRear(int x, Dbl_List L)
{
Node *P;
if (L.First == NULL)
{
P = new(Node);
P->Prev = NULL;
P->Data = x;
P->Next = NULL;
L.First = P;
10
L.Last = P;
}
else
{
P = new(Node);
P->Prev = L.Last;
P->Data = x;
P->Next = NULL;
L.Last->Next = P;
L.Last = P;
}
return L;
}
void ShowListFarward(Dbl_List L)
{
Node *P = L.First;
while (P != NULL)
{
cout<<P->Data<<"\t";
P = P->Next;
}
cout<<endl;
}
void ShowListBackward(Dbl_List L)
{
Node *P = L.Last;
while (P != NULL)
{
cout<<P->Data<<"\t";
P = P->Prev;
}
cout<<endl;
}
11
P = L.Last;
L.Last = L.Last ->Prev ;
L.Last ->Next = NULL;
delete P;
}
else
{
P = L.First;
while((P != NULL)&&(P->Data != x))
P = P -> Next;
if (P == NULL)
cout<<"Not Found.."<<endl;
else
{
(P->Prev)->Next = P->Next;
(P->Next)->Prev = P->Prev;
delete P;
}
}
}
return L;
}
void main()
{
Dbl_List L;
L.First = NULL;
L.Last = NULL;
L = AddFront(26,L);
L = AddFront(16,L);
L = AddRear(36,L);
L = DeleteItem(26,L);
ShowListFarward(L);
}
•
12