Lec7(Linked Lists)

Embed Size (px)

Citation preview

Linked List

!! :- ( ) nodes . : HEAD

.

NULL

NULL

HEAD

" " " " . " " "" . :

1

/

data pointer to another node

1.

2. :

Node

. head NULL 0 HEAD NULL

HEAD

3

NULL

2

/

A HEAD

B

C

D NULL

head ... NULL .

!! head !! : HEAD

NULL

: HEAD

HEAD

NULL

!! head !! .3

/

head .

" " " ".

: node . :single Linked List double Linked List

1- 3-

:

2-

circular List Linked

4

/

: : single Linked List

. HEAD single Linked List

NULL

) ( nodes : null

:

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

node number node 5

struct

next . /

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 NULL6

/

: ( ).

:

1 NULL node Null

: NULLNull

node

:

NULL

node

node Null

)void insert_last(node_ptr &first { ;int x ;"cout>>"; else { coutnext =q; } } // TO delete first node // ****************************************** void delete_first(node_ptr &first) { if(first==NULL) { coutnext; } 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; coutx; 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

/