You are on page 1of 22

Linked List

!!
-:
() nodes .
:

NULL

NULL

HEAD

HEAD


" " " " .
" " "" .
:

.1

data

.2

pointer to another node

Node

.

head NULL
0

HEAD

NULL

NULL

HEAD

NULL

HEAD

head
... NULL
.

!! head
!!

:

NULL

HEAD

NULL

HEAD

!!
head !!
.
/

HEAD

head


" "
" ".

:

node



.
:
:

-1

single Linked List

-2
-3

double Linked List

circular List Linked

: : single Linked List

NULL

single Linked List

HEAD


) ( nodes

:
:

null


: .
struct node
{
;int number
;node *next
;}
;typedef node *node_ptr
struct


next
.
/

node number
node
5

typedef * node_ptr
node

typedef

typedef

-:
:


( - - - ).

.
:


-1 .
-2 ( ).
-3 ( ).
-4 .
:


( : ( ) .
)node_ptr make_new_node(int x
{
;node_ptr p
;p=new node
;p->number =x
;p->next =NULL
;return p
}

p:
x

number
next NULL
/

: ( ).

1 NULL

node
Null

NULL

node
Null

NULL

node

node
Null

)void insert_last(node_ptr &first


{
;int x
;"cout<<"Enter value in new node \n
;cin>>x
;node_ptr p,q
)if(first==NULL
{
;)first=make_new_node (x
}
else
{
;p=first
)while(p->next!=NULL
{
; p=p->next
}
/

;)q=make_new_node (x
;p->next =q
}
}

.1 q ,p
.
.2 p first .
.3 NULL :
node_ptr

q p

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

3
NULL
node

node

node

Null
3

p=first

first p

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

next
/

NULL


node

node

node

Null
3

p->next=2

p=1

:
p=p->next


node

node

node

Null
3

p->next=3

p=2

p->next NULL
NULL 3 3
p=p->next
node

node

node

Null
2

p->next=Null

p=3

p->next NULL
NULL
q

p->next =q

NULL
/

: ( ).

:
)void insert_begin(node_ptr &first
{
;int x
;"cout<<"Enter value in new node\n
;cin>>x
;p

node_ptr

;)p=make_new_node(x
;p->next=first
;first=p
}

;p->next=first


q=1
node

node

node

Null
3

;first=p


node

node

node

Null
3

p->next=Null
/

10

p=3

temp->next=1
node

node

node

node

Null
3

node

node

node

Null
3


50 :
-1 , .
node

node

head

Null

-2
node

node

head

Null

node

-3

.
node

node
Null

node
/

11

head

:
)void insert_middle(node_ptr &first, int n
{
;int x
;"cout<<"Entr value in new node \n
;cin>>x
;node_ptr p,q
)if(first==NULL
{
;)first=make_new_node (x
}
else
{
;p=first
)while(p->number!=n &&p->next!=NULL
{
; p=p->next
}
;node *q =new node
;q->number=n
;)q=make_new_node (x
;q->next=p->next
;p->next =q
}
}
.

p .1
.2 number n
.
.3 q .
.4 .

12

.1 ( ) .
.2 ) ) .
.3 .

: ( )

removeFirst


-1 .
-2 .

)void delete_first(node_ptr &first


{
)if(first==NULL
{
;"cout<<"NO,nodes found .....\n
}
else
{
;node_ptr p
;p=first
;first=first->next
;delete p
}
}

: ) )

delete Last


:
-1 .
-2 NULL .
-3 .
/

13

)void delete_last(node_ptr &first


{
;node_ptr p,d
;p=first
)if(first==NULL
;"cout<<" NO;nodes found......\n
)while(p->next->next!=NULL
{
;p=p->next
}
;d=p->next
;p->next=NULL
;delete d
}

:
number
, ......50
:
-1 .
-2 .
-3

.
)void delete_middle(node_ptr&q, int m
{
;node_ptr p,d
;p=q
)while(p->next->number!=m &&p->next->next!=NULL
{
;p=p->next
}
;d=p->next
;p->next=d->next
;delete d
}


m NULL
d p
/

14

d->next

p
. d


#include<iostream.h>
struct node
{
int number;
node *next;
};
typedef node *node_ptr;
// to use program easily
// prototype functions
//*******************************************
node_ptr make_new_node(node_ptr &first,int x);
void display_list(node_ptr first );
void create_nodes(node_ptr &first,int n);
void order_insert(node_ptr &first,int x);
void insert_begin(node_ptr &first);
void insert_last(node_ptr &first);
void insert_middle (node_ptr &first,int n);
void delete_first(node_ptr &first);
void delete_last (node_ptr &first);
void delete_middle(node_ptr&q,int m) ;
void edit_node (node_ptr &first,int s);
int count(node_ptr &q);
int summation(node_ptr &q);
//*****************************************
int main()
{
node_ptr first;
first=NULL;
int choose;
int n,m;
done:
cout<<"\n\n\t\t WHAT DO YOU WANT TO DO ??\n\n"
<<"\t*************************************\n"
<<"\t*\t 1- Create alist
*\n"
<<"\t*\t 2- Display list
*\n"
<<"\t*\t 3- Insert_first
*\n"
<<"\t*\t 4- Insert_end
*\n"
<<"\t*\t 5- Insert_position
*\n"
<<"\t*\t 6- Delete_first
*\n"
<<"\t*\t 7- Delete_end
*\n"
15

<<"\t*\t 8- Delete_position
*\n"
<<"\t*\t 9- Edit node
*\n"
<<"\t*\t 10- Numbers of Nodes
*\n"
<<"\t*\t 11- Summation nodes
*\n"
<<"\t*\t 12- Exit program
*\n"
<<"\t*************************************\n";
cin>>choose;
switch(choose)
{
case 1:
cout<<"\t Enter number of nodes to create\n";
cin>>n;
create_nodes(first,n);
goto done;
break;
case 2:
display_list (first);
goto done;
break;
case 3:
insert_begin (first);
goto done;
break;
case 4:
insert_last (first);
goto done;
break;
case 5:
cout<<"Enter position number:\n";
cin>>n;
insert_middle(first,n);
goto done;
break;
case 6:
delete_first(first);
goto done;
break;
case 7:
delete_last (first);
goto done;
break;
case 8: cout<<"\t Enter value of node to delete\n";
cin>>m;
delete_middle(first,m);
16

goto done;
break;
case 9:
cout<<"\t Enter number of node to edit\n";
cin>>m;
edit_node(first,m);
goto done;
break;
case 10: cout<<"Numbers of Nodes is :";
cout<<count(first)<<"\n";
goto done;
break;
case 11:
cout<<"Nodes Summation is :";
cout<<summation(first)<<"\n";
goto done;
break;
default:
char ok;
retry:
cout<<"Are you sure you want to exit [y / n ]\n";
cin>> ok;
if(ok=='n')
{
goto done;
}
else if(ok=='y')
{
}
else
{
cout<<"You are not press acorrect character ,please Retry do\n";
cout<<"Press [y] to continue or press [n] to exit program\n";
goto retry;
}
break;
}
return 0;
}

// functions details
//------------------// TO MAKE anew node
17

//*********************
node_ptr make_new_node(int x)
{
node_ptr p;
p=new node;
p->number =x;
p->next =NULL;
return p;
}
// TO display anodes
// **********************
void display_list(node_ptr first )
{
node_ptr p;
if(first==NULL)
cout<<"NO, nodes founds>>>";
else
{
cout<<"\t The Data in the nodes are :\n";
p=first;
while(p!=NULL)
{
cout<<"Value of node ---> ";
cout<<p->number <<endl;
p=p->next;
}
}
}
//
TO CREATE lists of nodes
//
****************************
void create_nodes(node_ptr &first,int n)
{
int x;
cout<<"\t\t Enter the data in nodes\n";
for(int i=1;i<=n;i++)
{
cout<<" Node # "<<i<<endl;
cin>>x;
order_insert(first,x);
}
}
//
Order insert
//*********************************
void order_insert(node_ptr &first,int x)
{
18

node_ptr p;
if(first==NULL)
{
first=make_new_node (x);
}
else if(x==first->number )
{
cout<<"\t This value in node is Exist \n";
}
else if(x<first->number )
{
p=make_new_node (x);
p->next =first;
first=p;
}
else
order_insert(first->next,x);
}
//
Insert in the Begining
// **************************
void insert_begin(node_ptr &first)
{
int x;
cout<<"Enter value in new node\n";
cin>>x;
node_ptr p;
p=make_new_node(x);
p->next=first;
first=p;
}
// Insert in the last
// ************************
void insert_last(node_ptr &first)
{
int x;
cout<<"Entr value in new node \n";
cin>>x;
node_ptr p,q;
if(first==NULL)
{
first=make_new_node (x);
}
else
{
p=first;
19

while(p->next!=NULL)
{
p=p->next ;
}
q=make_new_node (x);
p->next =q;
}
}
// Insert in the Position
// ************************
void insert_middle(node_ptr &first,int n)
{
int x;
cout<<"Entr value in new node \n";
cin>>x;
node_ptr p,q;
if(first==NULL)
{
first=make_new_node (x);
}
else
{
p=first;
while(p->number!=n &&p->next!=NULL)
{
p=p->next ;
}
node *q =new node;
q->number=n;
q=make_new_node (x);
q->next=p->next;
p->next =q;
}
}
//
TO delete first node
// ******************************************
void delete_first(node_ptr &first)
{
if(first==NULL)
{
cout<<"NO,nodes found .....\n";
}
else
{
node_ptr p;
20

p=first;
first=first->next;
delete p;
}
}
//
TO delete last Node
//
*****************
void delete_last(node_ptr &first)
{
node_ptr p,d;
p=first;
if(first==NULL)
cout<<" NO;nodes found......\n";
while(p->next->next!=NULL)
{
p=p->next;
}
d=p->next;
p->next=NULL;
delete d;
}
// To Delete Middle Node
// ************************
void delete_middle(node_ptr&q,int m)
{
node_ptr p,d;
p=q;
while(p->next->number!=m &&p->next->next!=NULL)
{
p=p->next;
}
d=p->next;
p->next=d->next;
delete d;
}
void edit_node(node_ptr &first,int s)
{
int x;
cout<<"enter value of node:\n";
cin>>x;
node_ptr p;
p=first;
while(p->number!=s && p->next!=NULL)
{
p=p->next;
21

}
p->number=x;
}
int count(node_ptr &q)
{
node_ptr p;
p=q;
int count=0;
while(p!=NULL)
{
count++;
p=p->next;
}
return count;
}
int summation(node_ptr&q)
{
node_ptr p;
p=q;
int sum=0;
while(p!=NULL)
{
sum+=p->number;
p=p->next;
}
return sum;
}

22

You might also like