Monday, December 30, 2013

Delete same data in single linked list


#include <stdio.h>
#include <malloc.h>

typedef struct slink {
int data;
struct slink *next;
}node;


int a[] = {1, 2, 3, 3, 1, 2, 2, 1, 8, 9};


void append(node **q, int num)
{
node *temp = *q, *r = NULL;

r = (node *)malloc(sizeof(node));
r->data = num;
r->next = NULL;

if(*q == NULL)
*q = r;
else {
while(NULL != temp->next)
temp = temp->next;
temp->next = r;
}
}

void display(node *temp)
{
printf("\nThe list: ");
while(temp) {
printf("%d ", temp->data);
temp = temp->next;
}
}


void red_del(node **q)
{
node *temp = *q, *new = NULL,  *old = NULL;
int data;

while (temp != NULL && temp->next != NULL) {
new = temp;
data = new->data;
while(new->next != NULL) {
if(data == new->next->data) {
old = new->next;
new->next = new->next->next;
free(old);
} else 
new = new->next;
}
temp = temp->next;
}
}

void revlist(node **q)
{
node *temp1 = *q, *temp2 = NULL, *temp3 = NULL;

if (!temp1) return;

while(temp1) {
temp2 = temp1->next;
temp1->next = temp3;
temp3 = temp1;
temp1 = temp2;
}
if(temp3) *q = temp3;
}

int main()
{
node *head = NULL;


for (i = 0; i < 10; i++)
append(&head, a[i]);
display(head);

revlist(&head);
display(head);

red_del(&head);
display(head);

return 0;
}


Test
The list: 1 2 3 3 1 2 2 1 8 9
The list: 9 8 1 2 2 1 3 3 2 1
The list: 9 8 1 2 3 

Best method for setting & reading a hardware register

#include <stdio.h>


char *basecalc(unsigned int num, unsigned int base)
{
        static char buff[32];
        char *s = &buff[31];
        *s = '\0';

        while(num) {
                *(--s) = "0123456789ABCDEF"[num%base];
                num /= base;
        }
        return s;
}

void setreg(unsigned int *reg, unsigned int nofbits, unsigned int bitpos, unsigned int value)
{
        unsigned int mask;
        mask  = (~(~0 << nofbits) << bitpos);
        value = value << bitpos;
        *reg  = (*reg & ~mask) | (value & mask);
}

unsigned int getreg(unsigned int *reg, unsigned int nofbits, unsigned int bitpos)
{
        return (*reg >> bitpos) & (( 1 << nofbits) - 1) ;
}

int main()
{
        int num, nofbits, bitpos, value;
        printf("Enter the num: ");
        scanf("%d", &num);
        printf("Enter the nofbits: ");
        scanf("%d", &nofbits);
        printf("Enter the bitpos: ");
        scanf("%d", &bitpos);
        printf("Enter the value: ");
        scanf("%d", &value);

        printf("Read reg: %s\n", basecalc(getreg(&num, nofbits, bitpos), 2));

        printf("The num: %s\n", basecalc(num, 2));
        setreg(&num, nofbits, bitpos, value);
        printf("The result: %s\n", basecalc(num, 2));
        return 0;

}

Linux style
static void setreg(void __iomem *reg, u32 nofbits, u32 bitpos, u32 value)
{
        u32 mask;
        mask = (~(~0 << nofbits) << bitpos);
        value <<= bitpos;
        iowrite32(((ioread32(reg) & ~mask) | (value & mask)), reg);
}

static u32 getreg(void __iomem *reg, u32 nofbits, u32 bitpos)
{
        return (ioread32(reg) >> bitpos) & (~(~0 << nofbits));
}


Test 1
Enter the num: 25
Enter the nofbits: 2
Enter the bitpos: 3
Enter the value: 1

Read reg: 11
The num: 11001
The result: 1001

Test 2
Enter the num: 153
Enter the nofbits: 2
Enter the bitpos: 3
Enter the value: 2
The num: 10011001
The result: 10010001