Pages

Showing posts with label c programming. Show all posts
Showing posts with label c programming. Show all posts

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

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;

}


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

Simple Linked list

#include
#include
struct node{
int val;
struct node *next; // to store the next value
};

void insert(struct node**);
void delete(struct node**);
void traverse(struct node**);
int main()
{
struct node *head=NULL;
int opt=0;

while(1){
printf("Select an option \n 1.Insert 2.Delete 3.Exit 4. Traverse\n");
scanf("%d",&opt);
switch(opt){
case 1:{insert(&head); break; }
case 2:{delete(&head);break;}
case 3:{ exit(0);break; }
case 4:{traverse(&head);}
default:{printf("Invalid no");}
}
}
}


void traverse(struct node **head) // function to traverse
{ // input Address of head
printf("\n");
struct node *ptr;
if(*head==NULL){
printf("List Empty");
return;
}
ptr=*head;
while(ptr!=NULL){
printf(" %d",ptr->val);
ptr=ptr->next;
}
printf("\n");

}

void insert(struct node **head) // function to insert new node
{ // input address of head
struct node *ptr,*temp;

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);
temp=*head;
if(temp==NULL){
ptr->next=NULL;
*head=ptr;
return;
}
while(temp->next!=NULL)temp=temp->next;
temp->next=ptr;
}

void delete(struct node **head) // function to delete a node
{ // input address of head
struct node *ptr=*head;
struct node *prevptr=NULL;
int val;
if(*head==NULL){
printf("List empty");
return;
}
printf("Enter the value to delete\n");
scanf("%d",&val);
while((ptr->val!=val)&&(ptr->next!=NULL)){
prevptr=ptr;
ptr=ptr->next;
}

if(ptr->val!=val){
printf("Value not found\n");
return;
}
if(prevptr==NULL){ //if selected element has no previous element
*head=ptr->next;
free(ptr);
return;

}
prevptr->next=ptr->next;
traverse(head);
free(ptr);
}

c search replace string example

#include
#include
int main()
{
char replaceitem[100];
char str[300];
char newstr[300];
char searchitem[100];
int flag=0;
int i=0;
int index=0;
int j=0;
int k=0;

printf("Enter String\n");
fgets(str,300,stdin);

printf("Enter Search item\n");
fgets(searchitem,100,stdin);

printf("Enter replacement item\n");
fgets(replaceitem,100,stdin);

for( i = 0;i < strlen(str); i++) {
for(j=0;searchitem[j]==str[i+j];j++){
if(searchitem[j+1]=='\0')
break;
}
if((j==strlen(searchitem)-1)){
for(k=0;k
newstr[index++]=replaceitem[k];
flag=1;
}
i+=strlen(searchitem)-2;
}
if(flag==1){
flag=0;
continue;
}
newstr[index++]=str[i];
}
newstr[index]='\0';
puts(newstr);
return 0;
}

combine 2, 32 bit no to create 64 bit no C

#define shift(val)val=val<<32
#define concat(val,num2)val=val|num2
#define call(val,num2) shift(val);concat(val,num2)
int main()
{
int num1;
int num2;
unsigned long long val;

scanf("%d",&num1);
scanf("%d",&num2);
val=(long long)num1;
call(val,num2);
printf("%lld",val);

}

Traversing and adding file structure to a tree node

using System.Drawing;
using System.Text;
using System.Threading;
using System.IO;
public void ListDir(string src, TreeNode d){
try{
DirectoryInfo dinfo = new DirectoryInfo(@"" + src);
FileInfo[] finfo = dinfo.GetFiles();
for (int j = 0; j < finfo.Length; j++)
{
d.Nodes.Add(@"" + finfo[j].Name);
}

DirectoryInfo[] dname = dinfo.GetDirectories();
TreeNode[] treend = new TreeNode[dname.Length];
for (int i = 0; i < dname.Length; i++)
{
treend[i] = new TreeNode(dname[i].Name);
d.Nodes.Add(treend[i]);
}
for (int i = 0; i < dname.Length; i++)
{
ListDir(dname[i].FullName, treend[i]);
Thread.Sleep(5);
}
}