Tuesday, 19 August 2014

C Program to create a linear linked list interactively and print out the list and the total number of items in the list



#include
#include
#define NULL 0
struct linked_list
{
int number;
struct linked_list *next;
};
typedef struct linked_list node; /* node type defined */
main()
{
node *head;
void create(node *p);
int count(node *p);
void print(node *p);
head = (node *)malloc(sizeof(node));
create(head);
printf("\n");
printf(head);
printf("\n");
printf("\nNumber of items = %d \n", count(head));
}
void create(node *list)
{
printf("Input a number\n");
printf("(type -999 at end): ");
scanf("%d", &list -> number); /* create current node */
if(list->number == -999)
{
list->
next = NULL;
}
else /*create next node */
{
list->
next = (node *)malloc(sizeof(node));
create(list->next); */ Recursion occurs */
}
return;
}
void print(node *list)
{
if(list->next != NULL)
{
printf("%d-->",list ->number); /* print current item */
if(list->next->next == NULL)
printf("%d", list->next->number);
print(list->next); /* move to next item */
}
return;
}
int count(node *list)
{
if(list->next == NULL)
return (0);
else return(1+ count(list->next));
}

Assign Value Of Pointer In C

#include<stdio.h>
int main()
{
int num[2] = {10,20};
const int *ptr;

ptr = &num[0];

*ptr = 30;

printf("%d",*ptr);

return(0);
}

Splitting a string into tokens in C

#include 
#include 
#include 

#define MAX_LINE_LEN 256
#define NULL_TERM '\0'

/* A function that will print the content of the token_list */
int printcharlist(char **tok_list)
{
    char **pptr = tok_list;

    while (*pptr) {
        printf ("++ %s\n", *pptr);
        pptr++;
    }

    return 0;
}

/* returns the string between 2 pointers (not forgetting to append NULL_TERM */
char *getStr(char *start, char *end)
{
    int length=end-start;
    int i=0;
    char *result = (char *)malloc (sizeof (char) * (length+1));

    while (i < length) {
    result[i] = *start;
    start++;
    i++;
    }

    result[i] = NULL_TERM;

    return result;
}

Finding Tokens in a String Using Strock

#include
#include
#include

void main()
{
char str[80];
char s[]={' ',',',':',';'};
char *t;

clrscr();
printf("enter value\n");
gets(str);
/* get the first token */
t = strtok(str, s);
printf("token is ::\n");
/* walk through other tokens */
while( t != NULL )
{


printf( " %s\n", t );

t = strtok(NULL, s);
;

}

Finding Tokens in a String Using C Language

#include
#include
#include

void main()
{
char a[80];
int i=0;
clrscr();
printf("Enter the string\n");
gets(a);
while(a[i]!='\0')
{
 for(;a[i]==' ';i++);
 {
for(;a[i]!=' '&&a[i]!='\0';i++)
printf("%c",a[i]);
}
printf("%c \n",a[i]);}
getch();
}

Remove Space In String Using C

#include
#include
#include

void main()
{
char a[80];
int i=0;
clrscr();
printf("Enter the string\n");
gets(a);
while(a[i]!='\0')
{
 for(;a[i]==' ';i++);
 {
for(;a[i]!=' '&&a[i]!='\0';i++)
printf("%c",a[i]);
}
printf("%c \n",a[i]);}
getch();
}

Accept 10 numbers and store in an array, print sorted array in descending and ascending order


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,temp;
clrscr();
printf("Enter 10 nos\n");

for(i=0;i<10 i="" nbsp="" span="">
 scanf("%d",&a[i]);
 for(i=0;i<10 i="" span="">
 { 
 for(j=i+1;j<10 j="" span="">
 {
 if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<10 i="" span="">
 printf(" %d ",a[i]);
 printf(" Nos sorted in ascending order\n");
 printf("\n");
 for(i=9;i>=0;i--)
printf(" %d ",a[i]);
printf(" Nos sorted in descending order\n");
getch();
}