0% found this document useful (0 votes)
52 views

Operation On Singly Linked List

This document contains code for several operations on singly linked lists including creation, insertion, deletion and display. It defines a node struct with an info field and next pointer. Functions are defined to create a new list by inserting nodes, insert a new node at a given position, delete a node with given info, and display the list by traversing from the start node.

Uploaded by

arunsmile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Operation On Singly Linked List

This document contains code for several operations on singly linked lists including creation, insertion, deletion and display. It defines a node struct with an info field and next pointer. Functions are defined to create a new list by inserting nodes, insert a new node at a given position, delete a node with given info, and display the list by traversing from the start node.

Uploaded by

arunsmile
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 23

OPERATION ON SINGLY LINKED LIST

#include <stdio.h>
#include <malloc.h>
#define NULL 0
struct element_list
{
int info;
struct element_list *next;
};
tyedef struct element_list node;
main!"
{
node *start;
int #ey$choice$ne%info$delinfo$osition$&uit;
char c;
node *create!";
node *insert!";
node *delet!";
'oid dislay!";
start ( NULL;
&uit ( 0;
do
{
rintf!)*ro+ram for sin+ly lin#ed list,n,n)";
rintf!),t,t-hoice,t,t.tion,n)";
rintf!),t,t- ,t,t-reate,n)";
rintf!),t,t/ ,t,t/nsert,n)";
rintf!),t,t0 ,t,t0elete,n)";
rintf!),t,t1 ,t,t1uit,n)";
rintf!),t,t2nter choice 3 )";
do
c ( +etchar!";
%hile !strchr!)-c/i0d1&)$c" (( NULL";
s%itch!c"
{
case 4-4 3
case 4c4 3
start ( create!start";
rintf!),n***** Lin#ed list *****,n)";
dislay!start";
5rea#;
case 4/4 3
case 4i4 3
start ( insert!start";
rintf!),n***** Lin#ed list *****,n)";
dislay!start";
5rea#;
case 404 3
case 4d4 3
start ( delet!start";
5rea#;
case 414 3
case 4&4 3
&uit ( 6;
}
}
%hile !7&uit";
rintf!),n)";
return;
}
node *create!start"
node *start;
{
node *tem$ *re';
int element;
re' ( start ( NULL;
rintf!),2nter the inte+er !89999 to exit" 3 )";
scanf!):d)$;element";
%hile !element 7( 89999"
{
tem ( !node*"malloc!si<eof!node"";
tem8>info ( element;
tem8>next ( NULL;
if !start (( NULL"
start ( tem;
else
re'8>next ( tem;
re' ( tem;
rintf!),2nter the inte+er !89999 to exit" 3 )";
scanf!):d)$;element";
}
return start;
}
'oid dislay!start"
node *start;
{
rintf!),n=oot8>)";
%hile !start 7( NULL"
{
rintf!):d8>)$start8>info";
start ( start8>next;
}
rintf!)NULL,n,n)";
}
node *insert!start"
node *start;
{
node *ne%node$ *tem;
int i$ne%info$osition;
rintf!),n2nter the element to 5e inserted 3 )";
scanf!):d)$;ne%info";
do
{
rintf!),n2nter the osition of insertion 3 )";
scanf!):d)$;osition";
}
%hile !osition < 6";
ne%node ( !node*"malloc!si<eof!node"";
ne%node8>info ( ne%info;
if !!osition (( 6" >> !start (( NULL""
{
ne%node8>next ( start;
start ( ne%node;
}
else
{
tem ( start;
i ( ?;
%hile !!i <osition" ;; !tem8>next 7( NULL""
{
tem ( tem8>next;
@@i;
}
ne%node8>next ( tem8>next;
tem8>next ( ne%node;
}
return start;
}
node *delet!start"
node *start;
{
node *tem$ *re';
int i$ delinfo;
rintf!),n2nter the element to 5e deleted 3 )";
scanf!):d)$;delinfo";
if !start (( NULL"
rintf!),n-an4t delete 8 list emty,n)";
else
{
re' ( NULL;
tem ( start;
%hile !!tem 7( NULL" ;; !tem8>info 7( delinfo""
{
re' ( tem;
tem ( tem8>next;
}
if !tem (( NULL"
rintf!)2lement not found,n)";
else
{
if !re' (( NULL"
start ( start8>next;
else
re'8>next ( tem8>next;
rintf!),n***** Lin#ed list *****,n)";
dislay!start";
}
}
return start;
}
PROGRAM FOR LINKED LIST
#include <stdio.h>
tyedef struct itemlist
{
int data;
struct itemlist *next;
} L/NA;
L/NA itemB ({ CC$ NULL };
L/NA item? ({ 60$ ;itemB };
L/NA item6 ({ 9$ ;item? };
main!"
{
L/NA *item;
rintf!)*ro+ram of a lin#ed list.,n,n)";
for !item ( ;item6; item ; item ( item8>next"
rintf!) 0ata ( :d ,n)$ item8>data";
}
SHELL SORT
#include <stdio.h>
main!"
{
int i$D$#$count$n$'EF0G$tem;
rintf!)*ro+ram for shell sort,n)";
rintf!)2nter si<e of the 'ector 3 )";
scanf!):d)$;n";
rintf!),n2nter :d elements,n)$n";
for !i ( 6; i <( n; i@@"
scanf!):d)$;'EiG";
count ( 6;
%hile !count <( n"
{
count ( count * B @ 6;
}
do
{
count ( count H B;
i ( count @ 6;
rintf!),n/ncrement ( :d,n)$count";
rintf!),n2lements at :d are s%aed 3 ,n)$count";
%hile !i <( n"
{
tem ( 'EiG;
D ( i;
%hile !!'ED 8 countG" > tem"
{
rintf!)s%ain+ :d and :d,n)$tem$'ED8countG";
'EDG ( 'ED 8 countG;
D ( D 8 count;
if !D <( count"
5rea#;
}
'EDG ( tem;
i@@;
}
rintf!),nIector after assin+,n)";
for !# ( 6; # <( n; #@@"
rintf!):Fd)$'E#G";
rintf!),n)";
}
%hile !count 7( 6";
rintf!),n,tJorted 'ector,n)";
for !i ( 6; i <( n; i@@"
rintf!):Fd)$'EiG";
rintf!),n)";
return!0";
}
SELECTION SORT
#include <stdio.h>
main!"
{
int i$D$#$n$'EF0G$tem;
rintf!)*ro+ram for selection sort,n,n)";
rintf!)2nter si<e of the 'ector 3 )";
scanf!):d)$;n";
rintf!),n2nter :d elements,n)$n";
for !i ( 0; i < n; i@@"
scanf!):d)$;'EiG";
rintf!),n,t.ri+inal 'ector ,n)";
for !i ( 0; i < n; i@@"
rintf!):Fd)$'EiG";
rintf!),n)";
for !i ( 0; i < n86; i@@"
{
# ( i;
for !D ( i@6; D < n; D@@"
{
if !'E#G > 'EDG"
# ( D;
}
if !# 7( i"
{
tem ( 'EiG;
'EiG ( 'E#G;
'E#G ( tem;
}
}
rintf!),n,tJorted 'ector ,n)";
for !i ( 0; i < n; i@@"
rintf!):Fd)$'EiG";
rintf!),n)";
}
REVERSE THR STRING
#include <stdio.h>
main!"
{
int n;
'oid re'erse!";
rintf!)*ro+ram for re'erse the strin+.,n,n)";
rintf!)2nter num5er of characters in strin+ 3 )";
scanf!):d)$ ;n";
rintf!)2nter the strin+,n)";
re'erse!n";
}
'oid re'erse!n"
int n;
{
char s;
if!n (( 6"
{
s ( +etchar!";
utchar!s";
}
else
{
s ( +etchar!";
re'erse!88n";
utchar!s";
}
return;
}
RADIX SORT
#include <stdio.h>
#define NULL 0
struct 'ector
{
int info;
struct 'ector *next;
};
tyedef struct 'ector element;
element *<ero;
element *lastE60G$ *firstE60G;
main!"
{
element *node$ *tr;
'oid in!";
'oid out!";
element *radix!";
rintf!)*ro+ram for radix sort,n)";
node ( !element*"malloc!si<eof!element"";
in!node";
rintf!),n,t***** .ri+inal list *****,n)";
out!node";
rintf!),n)";
<ero ( node;
node ( radix!node";
rintf!),n,t***** Jorted list *****,n)";
out!node";
rintf!),n)";
return;
}
'oid in!record"
element *record;
{
int num;
rintf!)2nter element !89999 to 2nd" 3 )";
scanf!):d)$;record8>info";
if!record8>info (( 89999"
{
record8>next ( NULL;
return;
}
else
{
record8>next ( !element*"malloc!si<eof!element"";
in!record8>next";
}
return;
}
'oid out!record"
element *record;
{
if!record 7( NULL"
{
if!record8>info 7( 89999"
{
rintf!):Kd)$record8>info";
out!record8>next";
}
}
return;
}
element *radix!record"
element *record;
{
int lar$m$i$D$#$di+$oc;
element *nex$*r$*re';
int lar+e!";
int numdi+!";
int di+it!";
'oid udate!";
element *lin#!";
lar ( lar+e!record";
m ( numdi+!lar";
for !#(0; #<60; #@@"
{
firstE#G ( !element*"malloc!9*si<eof!element"";
lastE#G ( !element*"malloc!9*si<eof!element"";
}
for !D(6; D<(m; D@@"
{
for !i(0; i<60; i@@"
{
firstEiG ( NULL;
lastEiG ( NULL;
}
r ( record;
%hile !r8>next 7( NULL"
{
di+ ( di+it!r8>info$D";
nex ( r8>next;
udate!di+$r";
r ( nex;
}
if !r8>info 7( 89999"
{
di+ ( di+it!r8>info$D";
udate!di+$r";
}
oc ( 0;
%hile !lastEocG (( NULL"
oc @@;
record ( lin#!oc$record";
rintf!),t***** Ne% .rdered list *****,n)";
out!record";
rintf!),n)";
}
return!record";
}
int lar+e!record"
element *record;
{
element *sa'e;
int ;
( 0;
sa'e ( record;
%hile!sa'e8>next 7( NULL"
{
if!sa'e8>info > "
{
( sa'e8>info;
}
sa'e ( sa'e8>next;
}
return!";
}
int numdi+!lar+e"
int lar+e;
{
int tem$ num;
tem ( lar+e;
num ( 0;
%hile !tem 7( 0"
{
@@num;
tem ( tem H 60;
}
return!num";
}
int di+it!num$D"
int num$D;
{
int i$di+$tem;
tem ( num;
for !i(0; i<D; i@@"
{
di+ ( tem : 60;
tem ( tem H 60;
}
if !di+ 7( NULL"
rintf!)0i+it num5er :d of :d ( :d,n)$D$num$di+";
return!di+";
}
'oid udate!di+$r"
element *r;
int di+;
{
int i;
if !firstEdi+G (( NULL"
{
firstEdi+G ( r;
lastEdi+G ( r;
}
else
{
firstEdi+G8>next ( r;
firstEdi+G ( r;
}
r8>next ( NULL;
return;
}
element *lin#!oc$record"
element *record;
int oc;
{
element *r;
int i;
record ( lastEocG;
for !i(oc@6; i<60; i@@"
{
r ( firstEi86G;
if!firstEiG 7( NULL"
r8>next ( lastEiG;
else
firstEiG ( r;
}
return!record";
}
QUICK SORT
#include <stdio.h>
main!"
{
int i$n$r$s$'EF0G;
'oid sort!";
rintf!)*ro+ram for &uic# sort,n)";
rintf!)2nter si<e of the 'ector 3 )";
scanf!):d)$;n";
rintf!),n2nter :d elements,n)$n";
for !i ( 0; i < n; i@@"
scanf!):d)$;'EiG";
s ( 0;
r ( n 8 6;
sort!'$s$r";
rintf!),n,tJorted 'ector,n)";
for !i ( 0; i < n; i@@"
rintf!):Fd)$'EiG";
rintf!),n)";
return!0";
}
'oid sort!'6$first$last"
int '6EF0G$first$last;
{
int i$D$$tem$o#$#;
if !last > first"
{
i ( first;
D ( last;
( '6EfirstG;
o# ( 0;
%hile !7o#"
{
do
{
@@i;
}
%hile !!'6EiG <( " ;; !i <( last"";
%hile !!'6EDG >( " ;; !D > first""
{
88D;
}
if !D < i"
o# ( 6;
else
{
rintf!),nJ%ain+ :d and :d,n)$'6EiG$'6EDG";
tem ( '6EiG;
'6EiG ( '6EDG;
'6EDG ( tem;
for !# ( 0; # < last; #@@"
rintf!):Fd)$'6E#G";
rintf!),n)";
}
}
rintf!),nJ%ain+ :d and :d,n)$'6EfirstG$'6EDG";
tem ( '6EfirstG;
'6EfirstG ( '6EDG;
'6EDG ( tem;
for !# ( 0; # < last; #@@"
rintf!):Fd)$'6E#G";
rintf!),n)";
sort!'6$first$D86";
sort!'6$i$last";
}
}
STACK WITH ARRAY
#include <stdio.h>
int first$ chec#;
'oid main!"
{
int stac#E60G$ element$ &uit;
char c;
int o!";
'oid ush!";
'oid dislay!";
rintf!)*ro+ram of stac# %ith array,n)";
first ( 86;
&uit ( 0;
do
{
rintf!),n,t.tions,t,t-hoice)";
rintf!),n,t*ush,t,t*)";
rintf!),n,t*o,t,t.)";
rintf!),n,t2xit,t,t2)";
rintf!),n,t2nter choice 3 )";
do
c ( +etchar!";
%hile !strchr!)*.o2e)$c" (( NULL";
s%itch!c"
{
case 4*4 3
case 44 3
rintf!),n2nter an element to 5e ushed 3 )";
scanf!):d)$;element";
ush!stac#$element";
if !chec#"
{
rintf!),n,t**** Jtac# *****,n)";
dislay!stac#";
if !first (( 9"
rintf!),nJtac# o'erflo%,n)";
}
else
rintf!),nJtac# o'erflo%...don4t ush,n)";
5rea#;
case 4.4 3
case 4o4 3
element ( o!stac#";
if !chec#"
{
rintf!)*oed element ( :d,n)$element";
rintf!),n,t**** Jtac# *****,n)";
dislay!stac#";
}
else
rintf!),nJtac# underflo%...don4t o,n)";
5rea#;
case 424 3
case 4e4 3
&uit ( 6;
}
}
%hile !7&uit";
rintf!),n)";
}
'oid ush!stac#$element"
int stac#EG$element;
{
if !first (( 9"
chec# ( 0;
else
{
chec# ( 6;
@@first;
stac#EfirstG ( element;
}
}
'oid dislay!stac#"
int stac#EG;
{
int i;
if !first (( 86"
rintf!),n***** 2mty *****,n)";
else
{
for !i(first; i>(0; 88i"
rintf!):Kd)$stac#EiG";
}
rintf!),n)";
}
int o!stac#"
int stac#EG;
{
int srs;
if !first (( 86"
{
srs ( 0;
chec# ( 0;
}
else
{
chec# ( 6;
srs ( stac#EfirstG;
88first;
}
return srs;
}
STACK WITH LINKED LIST
#include <stdio.h>
#include <malloc.h>
struct node
{
int element;
struct node *next;
};
'oid main!"
{
struct node *start;
int &uit;
char c;
struct node *ush!";
struct node *o!";
'oid dislay!";
rintf!)*ro+ram of stac# %ith lin#ed list,n)";
start ( NULL;
&uit ( 0;
do
{
rintf!),n,t.tions,t,t-hoice)";
rintf!),n,t*ush,t,t*)";
rintf!),n,t*o,t,t.)";
rintf!),n,t2xit,t,t2)";
rintf!),n,t2nter choice 3 )";
do
c ( +etchar!";
%hile !strchr!)*.o2e)$c" (( NULL";
s%itch!c"
{
case 4*4 3
case 44 3
start ( ush!start";
rintf!),n,t**** Jtac# *****,n)";
dislay!start";
5rea#;
case 4.4 3
case 4o4 3
start ( o!start";
rintf!),n,t**** Jtac# *****,n)";
dislay!start";
5rea#;
case 424 3
case 4e4 3
&uit ( 6;
}
}
%hile !7&uit";
rintf!),n)";
}
'oid dislay!record"
struct node *record;
{
rintf!),n=oot)";
%hile !record 7( NULL"
{
rintf!)8> :d)$record8>element";
record ( !record8>next";
}
rintf!)8>NULL,n)";
return;
}
struct node *ush!first"
struct node *first;
{
struct node *ne%_node;
int ne%_element;
rintf!)2nter an element to 5e ushed 3 )";
scanf!):d)$;ne%_element";
ne%_node ( !struct node *"malloc!si<eof!struct node"";
ne%_node8>element ( ne%_element;
ne%_node8>next ( first;
first ( ne%_node;
return!first";
}
struct node *o!first"
struct node *first;
{
struct node *tem;
int element;
if!first (( NULL"
rintf!),n***** 2mty *****,n)";
else
{
rintf!)*oed element ( :d,n)$first8>element";
tem ( first8>next;
free!first";
first ( tem;
if !first (( NULL"
rintf!),n***** 2mty *****,n)";
}
return!first";
}
STACK OPERATIONS
#include <stdio.h>
#define LMN F0
enum 5oolean
{
true ( 6$ false ( 0$ ne+one ( 86
};
enum 5oolean ush!stac#$ rs$ s"
int *rs;
char stac#ELMNG$ s;
{
if !*rs (( 0"
return false;
else
{
*rs ( *rs 8 6;
stac#E*rsG ( s;
}
return true;
}
enum 5oolean o!stac#$ rs$ s"
int *rs;
char stac#ELMNG$ *s;
{
if !*rs (( LMN"
return false;
else
{
*s ( stac#E*rsG;
*rs ( *rs @ 6;
}
return true;
}
enum 5oolean ee!stac#$ rs$ s$ ch"
int rs$ ch;
char stac#ELMNG$ *s;
{
int si;
if !rs (( LMN"
return false;
si ( rs @ ch;
if !si >( LMN >> si < rs"
return ne+one;
*s ( stac#EsiG;
return true;
}
enum 5oolean chan+e!stac#$ rs$ s$ ch"
int rs$ ch;
char stac#ELMNG$ s;
{
int si;
if !rs (( LMN"
return false;
si ( rs @ ch;
if !si >( LMN >> si < rs"
return ne+one;
stac#EsiG ( s;
return true;
}
'oid dirslay!stac#$ rs"
int rs;
char stac#ELMNG;
{
int i;
for !i ( 0; i < rs; i@@"
rintf!) )";
for !i ( rs; i < LMN; i@@"
rintf!):c)$ stac#EiG";
if !i 7( C0"
rintf!),n)";
if !rs (( LMN"
rintf!),t***** 2mty *****,n)";
else
{
for !i ( 0; i < rs; i@@"
rintf!) )";
rintf!)O)";
if !i 7( K9"
rintf!),n)";
}
}
'oid main!"
{
int rs ( LMN$ ch$ &uit;
char stac#ELMNG$ s$ c;
enum 5oolean fla+;
rintf!)*ro+ram for stac# oerations,n)";
&uit ( 0;
do
{
rintf!),n,t.tions,t,t-hoice)";
rintf!),n,t*ush,t,tU)";
rintf!),n,t*o,t,t.)";
rintf!),n,t*ee,t,t*)";
rintf!),n,t2mty,t,tL)";
rintf!),n,t-han+e,t,t-)";
rintf!),n,t2xit,t,t2)";
rintf!),n,t2nter choice 3 )";
do
c ( +etchar!";
%hile !strchr!)Uu.o*Lm-c2e)$c" (( NULL";
fflush!stdin";
s%itch!c"
{
case 4U4 3
case 4u4 3
rintf!),n2nter an element to 5e ushed 3 )";
s ( +etc!stdin";
if!ush!stac#$ ;rs$ s" (( false"
rintf!)Jtac# full,n)";
dirslay!stac#$ rs";
5rea#;
case 4.4 3
case 4o4 3
if!o!stac#$ ;rs$ ;s" (( true"
rintf!)*oed element ( :c,n)$s";
else
rintf!),n,t**** 2mty *****,n)";
dirslay!stac#$ rs";
5rea#;
case 4*4 3
case 44 3
rintf!),n2nter an index into stac# 3 )";
scanf!):d)$;ch";
fla+ ( ee!stac#$ rs$ ;s$ ch";
if!fla+ (( false"
rintf!),n,t***** 2mty *****,n)";
else
if !fla+ (( ne+one"
rintf!)/ndex not exist,n)";
else
rintf!)2lement at :d ( :c,n)$ch$s";
dirslay!stac#$ rs";
5rea#;
case 4L4 3
case 4m4 3
rintf!),n0o you %ant emty stac# !PHN" Q 3 )";
s ( +etc!stdin";
if !s (( 4P4 >> s (( 4y4"
{
rs ( LMN;
rintf!),n,t***** 2mty *****,n)";
}
dirslay!stac#$ rs";
5rea#;
case 4-4 3
case 4c4 3
rintf!),n2nter an index into stac# 3 )";
scanf!):d)$;ch";
fflush!stdin";
rintf!),n2nter the 'alue to 5e ut 3 )";
s ( +etc!stdin";
fla+ ( chan+e!stac#$ rs$ s$ ch";
if!fla+ (( false"
rintf!),n,t***** 2mty *****,n)";
else
if !fla+ (( ne+one"
rintf!)/ndex not exist,n)";
dirslay!stac#$ rs";
5rea#;
case 424 3
case 4e4 3
&uit ( 6;
}
}
%hile !7&uit";
rintf!),n)";
}

You might also like