Pages

Wednesday, November 19, 2014

A Simple Implementation of nested List in Linux Kernel

/* Just for fun and basic usage of list and spinlock, caution , No clean up written*/
#include linux/module.h
#include linux/kernel.h
#include linux/moduleparam.h
#include linux/slab.h


static LIST_HEAD(sys_list);
struct system {

        struct list_head node;
        char name[10];
        struct list_head phy;
        spinlock_t phy_lock;

};

struct physical {
        struct list_head node;
        char m_name[10];
};

int add_list(char *name) {
        struct system *sys;
        sys= kmalloc(sizeof(struct system), GFP_KERNEL);
        strcpy(sys->name,name);
        INIT_LIST_HEAD(&sys->phy);
        list_add(&(sys->node), &sys_list);
}

int add_phylist(char *key, char *data) {
        int i = 0;
        struct system *itr;
        list_for_each_entry(itr, &sys_list, node) {

                if (strcmp(itr->name,key) == 0) {
                        spin_lock(&itr->phy_lock);
                        for (i=0 ; i<3 comment-3--="" i="">
                                struct physical *phy;
                                phy = kmalloc(sizeof(struct physical), GFP_KERNEL);
                                strcpy(phy->m_name,"abcd");
                                list_add(&(phy->node) , &(itr->phy));
                        }
                        spin_unlock(&itr->phy_lock);

                }
        }

}

int phyaddr_ret(struct list_head **head, char *key) {

         struct system *itr;
         list_for_each_entry(itr, &sys_list, node) {

                if (strcmp(itr->name,key) == 0) {
                        *head = &(itr->phy);
                }
        }

}
void myexit(){

        printk("Exit");

}


int myinit(){

        add_list("karthu");
        add_list("Abhi");
        add_phylist("karthu","M1");
        struct system *itr;
        struct physical *phy_itr;
        list_for_each_entry(itr, &sys_list, node) {

                printk ("%s", itr->name);
                list_for_each_entry(phy_itr, &(itr->phy), node) {
                        printk ("%s", phy_itr->m_name);
                }

        }
        printk ("----");
        struct list_head *head;
        phyaddr_ret(&head, "karthu");

        list_for_each_entry(phy_itr, head, node) {
                printk ("%s", phy_itr->m_name);
        }
      printk("Hello");
        return 0;
}


module_exit(myexit);
module_init(myinit);

Wednesday, May 1, 2013

Eve


Let me tell you a story

Long ago, there lived a baby sparrow named Eve.

Eve had beautifully bright eyes.   No other child had her kind of charm.

Young Eve loved to hear stories, Stories of charming princess, stories of kindness and love.

Eve lived in her stories.

One night she might be the princess, the other day an angel.

She dreamed of her charming prince.

She spends her nights staring at the stars wondering how big the universe is, yet how unique she is

For her the world seems to be of endless possibilities.

Eve wanted to fly, fly high, into the clouds, beyond the clouds, space, out of the galaxy.

Eva grow, her dreams grew.

Now as you assume, living in dream is a big mistake, she needs to wake up.

Possibilities and opportunities once seemed unlimited now seem to be limited. Every moment pass by possibility ends.

There are two possible endings to this story, one where Eve has to lead a miserable life and die.

But I won’t let my Eve die. I want her to fly high, keeps dreaming.

Each day is a new beginning.  Each day is a new life.

Let each day give her new dreams.   

I will never have her. She was never mine. 

Let her decide her own destiny.

Saturday, February 2, 2013

The Happiness


The most desired word in the world “Happiness”.

Everyone is in continuous search for it in one way or another. When somebody finds the happiness in the material world, someone tries to find it by detaching from the material world. 

The quest is same, just the path is different.

 But what is the measure, how happy are you?

I had a hard learning in this regard; Happiness and peace are highly cohesive.

For some period of time, I tried to find it in my studies, in computer programs. Endless hours I have spend in extra lab in my college, to find out things. The quest of learning had always kept me happy.

I should say my efforts to find happiness in gadgets always ended in disaster. I never cared about that.

The hard learning I had was that the happiness, sadness, success, failure are illusions of our mind.

My friend once told me “Once you decide that any external person or event cannot alter your state of mind, your peace of mind is guaranteed”

You may say great philosophy, but too difficult to implement?

Here is the second part of puzzle kicks in, a virtue that is essential to all.

“Patience”

I have had difficult times, but I never lost two things, Patience and Hope.

 If you have these two believe me you will come out of any sort of struggle.

As like told in the move The Best Exotic Marigold Hotel “Everything will be fine in the end, If it is not It is not the end.”

Keep up the hard work; it will pay you one day or another.

Wednesday, January 30, 2013

The Atheist


Recently my best friend wrote in his blog, that he doesn't see the god around anymore.  The god doesn't exist. 

The reason and the question were same as ever. The question many has asked, continuing to be asked If god “Why sufferings”

Doesn't the same question we have asked for centuries.  Why there is no definite answer.

The answer from me is also the same “I am not sure”

Our supercomputers computational power is no match for solving it.

Until something is not proved a definitive no, Always care about the possibility of a yes. In that sense I make a better atheist than my friend.  

The law of universe is that, everything has a purpose.  The universe is a fact, the purpose of it a mystery.

Does the suffering implies there is no god, Evil implies no god?

Just like there is darkness when there is no light, Evil will be there when there is no god.

Evil implies absence of god.

I always loved the idea of considering the god as an energy source.

That answers most of my questions.

Sunday, January 27, 2013

The Mark


The Mark, what mark? The mark every soul has to leave in this world. The mark that identifies someone leaved and flourished here.

How many of us ready to spend even ten minutes out of our life to see the markings. For thousands of years humans ruled this world.

From the time of Roman empire their legions, barbarians crusaders. Even before them, India vedic civilization, Egyptian empires, Mayans and traditions dates older than that.

The amount of accumulation of the knowledge is overwhelming.

Yet, It is painful rather than surprising to see how less and less we pay attention to them.

If these markings can speak, what they are going tell to us?

Are they going   to tell to us about the great battles they have fought? Or the big palaces they have created. Or how beautiful the earth was before fossil fuel and cities consumed it.   

It’s always felt to me that the tombs our ancestors are screaming to us. They are begging to us to stay away from the mistakes they have done.

Yet, we don’t listen, We Ignores.

History is not a story. It’s a warning, teaching. The one, who ignores it, cannot have a history of his own. He will be simply repeating the history itself, yet himself part of the history.

That’s the beauty of it. Isn’t it?

My father died in my early childhood, I am probably six then. He was a beautiful person. He told me stories of gods, goodness, and kindness. He shaped me to what I am now. I always wanted to be like him.. I was never like my mother. 

His magic always worked, I never allowed myself to be deviated from the path he has shown me.

Two decades passed after he passed away.  I wonder, what he would have told me if he was alive.  Whether he would be proud of me? I know he will be. 

I can still feel him, though my mother, his brothers and sisters and in the tears they have in their eyes. For me he lives among gods. The gods whom he had told me stories about!!!

What mark has he left for me? Is it the watch he left or the rare old coins from his collection? Or the property he has left for me.

The thought was disturbing me for weeks now. Then a moment of realization!!!
 The moment of realization can be joyful and painful at simultaneously, an emotion that cannot be expressed by words.

It was too obvious and the markings can never be missed, it is me. It was always me.  I am his legacy.

The continuation!!!  

The thought itself puts me in immense pressure.  The responsibility it brings can break my back. It has the power to destroy me with in me.

Destiny cannot be altered. Every human has a purpose in life, maybe I am destined to do this.

It is the purpose!!!

After all markings won’t fade away that fast. Do they?

Saturday, January 26, 2013

The confidence


This is my first attempt to write anything other than technical. The moment I decided to write about something and post in my Blog. As usual the first question was  “ What  I am going to write about”.  I was a good reader though out my life but never a good story teller.

Then suddenly a thought appeared in my mind.  What I am afraid about.  Is it that I am afraid of criticism or the fear of my personal thoughts going online? I must say the second part is still a scary scenario.

This is our problem isn’t it? Most of the people live in an imaginary scary cage. a fence which we creates our self. A cage is un breakable from outside.

Imaginary fear is equivalent to suicide I would say. The biggest crime one can do to himself.

Don’t confuse, I am no gurus who give preaching or a career guidance guy. I was always been a software guy and will always be. I am just writing the thoughts that are pouring on my head this late night.

Of course I am trying to break free from my own cage.

Fear is a necessary evil. If you find yourself to be not afraid of anything in the world, then my friend there is something seriously wrong with you.
 
Now where is the solution? For everything there needs to be a solution. 

Simple, the solution lies within you.  Every question has an answer embedded within you.

Never fear the fear, observe it, understand it conquer it. 

Sunday, August 5, 2012

Memory in linux

In a typical 32 bit machine, the address ranges from 0x00000000  to 2^32 – 1 (0xFFFFFFFF) . This entire address region will be split among RAM and device regions (address of devices and its registers)

/proc/meminfo will list the memory maps in the device.

In general, addresses which are mapped to RAM is known as physical address, where as address which are mapped to devices are known as bus address. The address can be architecture dependent and the details can be obtained from hardware manufacturer .

However none of these address can be used directly from a linux module. Physical address needs to be mapped to a virtual address to make use of it

sample output
abhi :# cat /proc/iomem 
00000000-0009f7ff : System RAM
0009f800-0009ffff : reserved
000a0000-000bffff : Video RAM area
000c0000-000c7fff : Video ROM
000ca000-000cafff : Adapter ROM
000dc000-000e3fff : reserved
000f0000-000fffff : System ROM
00100000-3feeffff : System RAM
  00100000-002baebc : Kernel code
  002baebd-0037661f : Kernel data
  003bc000-0041f57f : Kernel bss
3fef0000-3fefefff : ACPI Tables
3feff000-3fefffff : ACPI Non-volatile Storage
3ff00000-3fffffff : System RAM
50000000-50007fff : 0000:00:0f.0
50008000-5000bfff : 0000:00:10.0
d0000000-d7ffffff : 0000:00:0f.0
d8000000-d87fffff : 0000:00:0f.0
d8800000-d881ffff : 0000:00:10.0
  d8800000-d881ffff : mpt
d8820000-d883ffff : 0000:00:10.0
  d8820000-d883ffff : mpt
d8900000-d9cfffff : PCI Bus 0000:02
  d8900000-d8900fff : 0000:02:03.0
    d8900000-d8900fff : ehci_hcd
d9d00000-d9dfffff : PCI Bus 0000:03
d9e00000-d9efffff : PCI Bus 0000:0b
d9f00000-d9ffffff : PCI Bus 0000:13
da000000-da0fffff : PCI Bus 0000:1b
da100000-da1fffff : PCI Bus 0000:04
da200000-da2fffff : PCI Bus 0000:0c
da300000-da3fffff : PCI Bus 0000:14
da400000-da4fffff : PCI Bus 0000:1c
da500000-da5fffff : PCI Bus 0000:05
da600000-da6fffff : PCI Bus 0000:0d
da700000-da7fffff : PCI Bus 0000:15
da800000-da8fffff : PCI Bus 0000:1d
da900000-da9fffff : PCI Bus 0000:06
daa00000-daafffff : PCI Bus 0000:0e
dab00000-dabfffff : PCI Bus 0000:16
dac00000-dacfffff : PCI Bus 0000:1e
dad00000-dadfffff : PCI Bus 0000:07
dae00000-daefffff : PCI Bus 0000:0f
daf00000-daffffff : PCI Bus 0000:17
db000000-db0fffff : PCI Bus 0000:1f
db100000-db1fffff : PCI Bus 0000:08
db200000-db2fffff : PCI Bus 0000:10
db300000-db3fffff : PCI Bus 0000:18
db400000-db4fffff : PCI Bus 0000:20
db500000-db5fffff : PCI Bus 0000:09
db600000-db6fffff : PCI Bus 0000:11
db700000-db7fffff : PCI Bus 0000:19
db800000-db8fffff : PCI Bus 0000:21
db900000-db9fffff : PCI Bus 0000:0a
dba00000-dbafffff : PCI Bus 0000:12
dbb00000-dbbfffff : PCI Bus 0000:1a
dbc00000-dbcfffff : PCI Bus 0000:22
dbe00000-dbffffff : pnp 00:0c
dc400000-dc9fffff : PCI Bus 0000:02
  dc400000-dc40ffff : 0000:02:01.0
dca00000-dcafffff : PCI Bus 0000:03
dcb00000-dcbfffff : PCI Bus 0000:0b
dcc00000-dccfffff : PCI Bus 0000:13
dcd00000-dcdfffff : PCI Bus 0000:1b
dce00000-dcefffff : PCI Bus 0000:04
dcf00000-dcffffff : PCI Bus 0000:0c
dd000000-dd0fffff : PCI Bus 0000:14
dd100000-dd1fffff : PCI Bus 0000:1c
dd200000-dd2fffff : PCI Bus 0000:05
dd300000-dd3fffff : PCI Bus 0000:0d
dd400000-dd4fffff : PCI Bus 0000:15
dd500000-dd5fffff : PCI Bus 0000:1d
dd600000-dd6fffff : PCI Bus 0000:06
dd700000-dd7fffff : PCI Bus 0000:0e
dd800000-dd8fffff : PCI Bus 0000:16
dd900000-dd9fffff : PCI Bus 0000:1e
dda00000-ddafffff : PCI Bus 0000:07
ddb00000-ddbfffff : PCI Bus 0000:0f
ddc00000-ddcfffff : PCI Bus 0000:17
ddd00000-dddfffff : PCI Bus 0000:1f
dde00000-ddefffff : PCI Bus 0000:08
ddf00000-ddffffff : PCI Bus 0000:10
de000000-de0fffff : PCI Bus 0000:18
de100000-de1fffff : PCI Bus 0000:20
de200000-de2fffff : PCI Bus 0000:09
de300000-de3fffff : PCI Bus 0000:11
de400000-de4fffff : PCI Bus 0000:19
de500000-de5fffff : PCI Bus 0000:21
de600000-de6fffff : PCI Bus 0000:0a
de700000-de7fffff : PCI Bus 0000:12
de800000-de8fffff : PCI Bus 0000:1a
de900000-de9fffff : PCI Bus 0000:22
e0000000-efffffff : PCI MMCONFIG 0
  e0000000-efffffff : reserved
fec00000-fec0ffff : reserved
fee00000-fee00fff : reserved
fffe0000-ffffffff : reserved




  

How to get RAM info in linux?

Simple give cat /proc/meminfo

Sample output

abhi:/sys/class# cat /proc/meminfo
MemTotal:      1036092 kB
MemFree:        600532 kB
Buffers:        150752 kB
Cached:         141268 kB
SwapCached:          0 kB
Active:         233532 kB
Inactive:       161568 kB
HighTotal:      131008 kB
HighFree:          252 kB
LowTotal:       905084 kB
LowFree:        600280 kB
SwapTotal:     2650684 kB
SwapFree:      2650684 kB
Dirty:             360 kB
Writeback:           0 kB
AnonPages:      103064 kB
Mapped:          42420 kB
Slab:            28744 kB
SReclaimable:    23412 kB
SUnreclaim:       5332 kB
PageTables:       1668 kB
NFS_Unstable:        0 kB
Bounce:              0 kB
WritebackTmp:        0 kB
CommitLimit:   3168728 kB
Committed_AS:   274432 kB
VmallocTotal:   114680 kB
VmallocUsed:      4596 kB
VmallocChunk:   109660 kB
HugePages_Total:     0
HugePages_Free:      0
HugePages_Rsvd:      0
HugePages_Surp:      0
Hugepagesize:     4096 kB

Friday, August 3, 2012

Beauty of Life
 One of my best shot
 Cola
 Sunset in Singapore

Tuesday, May 15, 2012




Some more PICS 
























Beauty of nature is irresistible. Of all the beauty nature has, flowers got the most of it 









Thursday, February 9, 2012

Some of the pics I have taken

Well I am not a professional photographer. Not even close to a good photographer. But I am going to share some pics I have taken. All the copyrights for this pics belongs to me alone.

After all Joy of happiness is in sharing it.

A cool evening, empty road and miles ahead left to explore...

Flow of a river...






Saturday, March 19, 2011

Tricky, generic function pointer

#include
/* This is our generic function pointer */
typedef void (*fp)(void);
int func1(int a)
{
printf("Inside Func 1. Received %d\n", a);
return 2;
}

char func2(char b)
{
printf("Inside Func 2. Received %c\n", b);
return 'b';
}

int main()
{
fp ptr1;
int temp1;
char temp2;

ptr1 = (fp) func1;

temp1 = ((int (*)(int)) ptr1)(1);
ptr1 = (fp) func2;
// ptr1('g');
temp2 = ((char (*)(char))ptr1)('a');
printf("Inside Main. Received %d %c\n", temp1, temp2);
return 0;
}

Passing a function pointer to a function

#include

int any_function(int(*pfun)(int, int), int x, int y){
return pfun(x, y);
}

int sum(int x, int y){
return x + y;
}

int product(int x, int y){
return x * y;
}

int difference(int x, int y){
return x - y;
}

int main(void){
int a = 10;
int b = 5;
int result = 0;
int (*pf)(int, int) = sum; /* Pointer to sum function */

result = any_function(pf, a, b);
printf("\nresult = %d", result );

result = any_function(product,a, b);
printf("\nresult = %d", result );

printf("\nresult = %d\n", any_function(difference, a, b));
return 0;
}

Simple function pointer in C

#include

int sum(int x, int y){

return x + y;

}

int product(int x, int y){

return x * y;

}

int difference(int x, int y){

return x - y;

}

int main(void){

int a = 10;

int b = 5;

int result = 0;

int (*pfun[3])(int, int); /* Function pointer array declaration */


/* Initialize pointers */

pfun[0] = sum;

pfun[1] = product;

pfun[2] = difference;


/* Call all three functions through pointers in an expression */

result = pfun[1](pfun[0](a, b), pfun[2](a, b));

printf("\n\nThe product of the sum and the difference = %d\n", result);

return 0;

}


Some string manipulations is shell script

var="hai hello world tom"

pattern output
echo ${var%%hello*} //hai
echo ${var%%tom} //hai hello world
echo ${var##*hello} //world tom
echo ${var##hello*} //hai hello world tom
echo ${#var} // 19

Some shell script Experiments

1. Capture Ctrl+c Signal using Shell script

trap 'echo pressed c' 2

read f


2. Getting current PID using shell script

#!/bin/sh

echo $$

3. Use of arrays in shell script

newmap['name']="Abc"

newmap['designation']="SSE"

newmap['company']="xyz"


echo ${newmap['company']}

echo ${newmap['name']}

echo "after"

newmap=(Abd SSE HRD)

echo ${newmap[0]}

echo ${newmap[1]}

echo ${newmap[2]}

for i in newmap

do

echo $i

done

Saturday, August 7, 2010

Linux Commands

I am posting some Linux commands which might be use full for others

Create RAID on Linux

mdadm --create /dev/md0 –level=5 –raid-devices=3 /dev/sdb /dev/sdc /dev/sdd

show raid details

mdadm –- detail /dev/md0

Stop raid

mdadm –-stop /dev/md0

Remove raid

mdadm –-remove /dev/md0


Installing RPM

rpm –ivh blahblah.rpm


Upgrading rpm

rpm –uvh blahblah.rpm


Removing RPM

rpm –ev blahblah.rpm


Creating rpms


compile driver
rename blahblah_(uname –a)
tar –cvzf blahblah_prebuild.tgz blahblah_(uname –a)
rpmbuild –bb ‘target=i386’ abc.spec

To extract rpm

rpm2cpio any.rpm cpio -idv

To load usb modules

modprobe usb-storage
modprobe oxci_hcd
modprobe ehc_hcd


Search contents of the file in current folder recursively

find . xargs grep –n
  
Turning on hibernation in windows from command prompt

powercfg –h on


Compiling Linux source

make mrproper
make clean

make menuconfig

make all && make modules && make modules_install && make install

How to Applying patch

patch p1 < abc.patch

cd back up

cpio –icuvd < /dev/st0

To find current partition

df

Generate ctags

ctags –R .

To see the version of a module

modinfo –F version

Enabling scsi logs

sysctl -w dev.scsi.logging_level=0xffffffff


To see change log of a Linux source rpm

rpm -q --changelog -p kernel......rpm less


Creating tar

tar –cvzf abc.tar *

Uncompress tar

tar –xvzf abc.*

Checking Kernel Version

uname –a

64 bit or 32 bit

uname -m

To see os version

cat /etc/SuSE-release in sles

cat /etc/redhat-release in RHEL

Saturday, April 24, 2010

Binary Search tree with insert delete, print option C

#include
#include
struct node{
int val;
struct node *left;
struct node *right;
};

void insert(struct node**,int);
void insert_init(struct node**);
void inorder(struct node**);
void preorder(struct node**);
void postorder(struct node**);
void search_init(struct node**,struct node **,struct node **);
void search(struct node **,int,struct node**,struct node **);
void delete_init(struct node **,struct node**,struct node **);
void delete(struct node **,struct node **,struct node**);


int main()
{

struct node *root=NULL;
struct node *parent=NULL;
struct node *curnode=NULL;
int opt=0;

while(1){
printf("\n Select an option \n 1.Insert 2.Inorder 3. Preorder 4. Postorder 5.Seatch 6.delete 7.Exit\n");
scanf("%d",&opt);
switch(opt){
case 1:{ insert_init(&root);break; }
case 2:{ printf("\nInorder Traversal\n"); inorder(&root);break; }
case 3:{ printf("\nPreorder Traversal\n");preorder(&root);break; }
case 4:{ printf("\nPostorder Traversal\n");postorder(&root);break; }
case 5:{ search_init(&root,&parent,&curnode);break; }
case 6:{ delete_init(&root,&parent,&curnode);break; }
case 7:{ exit(0);}
}
}

}

void insert_init(struct node **root) //function toread insert value
{ //input: address of root
int val;

printf("Enter an integer value\n");
scanf("%d",&val);
insert(root,val);

}
void insert(struct node **root,int val) //function for insert
{ //input address of root and input value
if(*root == NULL){
*root=(struct node *)malloc(sizeof(struct node));
(*root)->val=val;
(*root)->left=NULL;
(*root)->right=NULL;
return;
}
if((*root)->val==val){
printf("\nValue Already exists in the tree\n");
return;
}
if((*root)->val > val)
insert(&(*root)->left,val);
else
insert(&(*root)->right,val);

}
void inorder(struct node **root) //function to perform inorder traversal
{ // input to function ,address of root
if(*root == NULL)return;
inorder(&(*root)->left);
printf(" %d",(*root)->val);
inorder(&(*root)->right);
}
void preorder(struct node **root) //function to perform preorder traversal
{
if(*root == NULL)return;
printf(" %d",(*root)->val);
preorder(&(*root)->left);
preorder(&(*root)->right);

}
void postorder(struct node **root) //function to perform postorder traversal
{
if(*root == NULL)return;
preorder(&(*root)->left);
preorder(&(*root)->right);
printf(" %d",(*root)->val);
}
void search_init(struct node **root,struct node **parent,struct node **curnode)
{
int searchitem;

*parent==NULL;
printf("Enter the search item\n");
scanf("%d",&searchitem);
search(root,searchitem,parent,curnode);
}
void search(struct node **root,int searchitem,struct node **parent,struct node **curnode) //function to perform search operation
{ //inputs: root address, search item parent
if(*root == NULL) {
printf("\nItem not Found\n");
*curnode=*root;
return;
}
if(searchitem == (*root)->val) {
printf("\nItem found in the tree\n");
*curnode=*root;
return;
}
*parent=*root;
if(searchitem>(*root)->val) {
search(&(*root)->right,searchitem,parent,curnode);
} else {
search(&(*root)->left,searchitem,parent,curnode);
}
}
void delete_init(struct node **root,struct node **parent,struct node **curnode) //function to read delete item
{
int item;
struct node *temp;

printf("\n Enter the item to delete");
scanf("%d",&item);
search(root,item,parent,curnode);
if(curnode==NULL)return;
delete(root,curnode,parent);
}
void delete(struct node **root,struct node **nodetodel,struct node **parent) //function to delete a node
{

struct node *tempparent;
struct node *swapnode;

if(((*nodetodel)->right == NULL )&&((*nodetodel)->left == NULL)) {
if(*parent==NULL) {
*root=NULL;
free(*nodetodel);
}else {
if((*parent)->val > (*nodetodel)->val) {
(*parent)->left=NULL;
}else {
(*parent)->right=NULL;
}
free(*nodetodel);
}
return;
}

if((*nodetodel)->right!=NULL) {

swapnode=*nodetodel;
tempparent=swapnode;
swapnode=swapnode->right;
while(swapnode->left != NULL) { //traverse to leftmost node
tempparent=swapnode;
swapnode=swapnode->left;
}
(*nodetodel)->val=swapnode->val;
*parent=tempparent;
if( (*parent)->val > swapnode->val ) {

(*parent)->left=NULL;
} else {
(*parent)->right=NULL;
}
if(swapnode->right != NULL)
(*parent)->left=swapnode->right; //new condition(for debug)
free(swapnode);
return;
}
if((*nodetodel)->left != NULL) {
swapnode=*nodetodel;
tempparent=swapnode;
swapnode=swapnode->left;

while(swapnode->right != NULL) { //traverse to right most node
tempparent=swapnode;
swapnode=swapnode->right;
}
(*nodetodel)->val=swapnode->val;
*parent=tempparent;
if( (*parent)->val > swapnode->val ) {
(*parent)->left=NULL;
} else {
(*parent)->right=NULL;
}
if(swapnode->left != NULL)
(*parent)->right=swapnode->left; //new condition (for debug)

free(swapnode);
return;
}

}

Simple Linked Stack

#include
#include
struct node{
int val;
struct node *next;
};

void push(struct node**);
void pop(struct node**);

int main()
{
struct node *head=NULL;
int opt=0;

while(1){
printf("Select an option \n 1.Push 2.Pop 3.Exit\n");
scanf("%d",&opt);
switch(opt){
case 1:{push(&head); break; }
case 2:{pop(&head);break;}
case 3:{ exit(0); }
}
}
}
void push(struct node **head) //to insert at begining
{ //input is address of head
struct node *ptr;

ptr=(struct node*)malloc(sizeof(struct node));
if(ptr==NULL){
printf("Allocation failed\n");
return;
}
printf("Enter an integer value\n");
scanf("%d",&ptr->val);
ptr->next=*head;
*head=ptr;

}


void pop(struct node **head) //to pop an element out
{ //input is address of head if(*head==NULL){
printf("\n Stack Empty\n");
return;
}
struct node *ptr;
ptr=*head;
printf("the poped value is%d\n",ptr->val);
*head=ptr->next;
free(ptr);
}