Linked Lists: // Node.h

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Linked lists

-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::InsertAtFront(int info)


{
Node *q = new(Node);
q -> data = info;
q -> nextPtr = head;
head = q;
}

void List::InsertAtBack(int info)


{
if (IsEmpty())
{
Node *q = new(Node);
q -> data = info;
q ->nextPtr = NULL;
head = q;
}
else
{
Node *p = head;
while (p->nextPtr != NULL)
p = p->nextPtr;
Node *q = new(Node);
q -> data = info;
q ->nextPtr = NULL;
p ->nextPtr = q;
}
}

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"

using namespace std;

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

Node* AddItemFront(int x, Node *L)


{
Node *P;
P = new(Node);
P -> Data = x;
P -> Next = L;
L = P;
return L;
}

.L x

L 10 20 30 NULL
L

4
L 10 20 30 NULL

x
L

Node* AddItemRear(int x, Node *L)


{
Node *P, *Q;
if (L = = NULL)
{
Q = new(Node);
Q -> Data = x;
Q -> Next = NULL;
L = Q;
}
else
{
P = L;
while (P->Next != NULL)
{
P = P -> Next;
}
Q = new(Node);
Q -> Data = x;
Q -> Next = NULL;
P -> Next = Q;
}
return L;
}

5
.L x
"Found…." •
"Not Found…." •

bool SearchItem(int x, Node *L)


{
Node *P = L;
if (P == NULL)
return false;
else
if (P -> Data == x)

5
return true;
else
return SearchItem(x, P -> Next);
}
6

x ) L x
(

L 10 20 30 NULL

L 30 10 NULL

20 L

Node* DeleteItem(int x, Node *L)


{
Node *P = L, *Q;
if (P -> Data == x)
{
L = L ->Next;
delete P;
}
else
{
while ((P->Next)->Data != x)
P = P -> Next;
Q = P -> Next;
P -> Next = Q -> Next;
delete Q;
}
return L;
}
7
.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

Node* SortList(Node *L)


{
Node *P;
int np;
int temp;
do
{
np = 0;
P = L;
while (P->Next != NULL)
{
if (P->Data > (P->Next)->Data)
{
temp = P->Data;
P->Data = (P->Next)->Data;
(P->Next)->Data = temp;
np++;
}
P = P->Next;
}
} while (np > 0);
return L;
}

7
(Circular linked lists) -2

. NULL

L 20 10 30 NULL

L 10 20 30

L :1

Node* Circular(Node *L)


{
Node *P;
if (L != NULL)
{
P = L;
while (P->Next != NULL)
P = P->Next;
P -> Next = L;
}
return L;
}

L :2
.(Q)

void PrintCircular(Node *Q)


{
Node *P;
if (Q != NULL)
{
cout<<Q->Data<<"\t";
P = Q->Next;
while (P != Q)

8
{
cout<<P->Data<<"\t";
P = P->Next;
}
}
}

(Doubly-linked lists) -3

Previous -
Next -

First *Prev Data *Next Last


Null 16 26 56 46 Null

:(Last) (First)

struct Node
{
Node *Prev;
int Data;
Node *Next;
}

struct Dbl_list
{
Node *First;
Node *Last;
}

- Dbl_List AddFront(int x, Dbl_List L) //


- Dbl_List AddRear(int x, Dbl_List L) //
- void ShowListForward(Dbl_List L) //

- void ShowListBackward(Dbl_List L) //

- Dbl_List DeleteItem(int x, Dbl_List L) // x

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;
}

Dbl_List DeleteItem(int x, Dbl_List L)


{
Node *P;
if (L.First->Data == x)
{
P = L.First;
L.First = L.First ->Next ;
L.First ->Prev = NULL;
delete P;
}
else
{
if (L.Last->Data == x)
{

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

You might also like