Sunday, 6 July 2014

Displaying the linked list elements

/*
Function to display the student with highest CGPA in a linked list containing student data.
 Use the following node structure for your linked list.
*/
struct node{
int stNo;
float CGPA;
struct node *next;
};

typedef struct node *NODEPTR;
void DisplayMax(NODEPTR plist)
{
NODEPTR p;
float maxCGPA=-1.0;
int maxstNo;
p = plist;
/*current node*/
if(p == NULL){
printf(“no node/data is available in the list\n”);
return;
}
do{
if(p->CGPA > maxCGPA){
maxCGPA = p->CGPA;
maxstNo = p->stNo;
}
p = p->next;
} while(p!= NULL);
printf(“The student number with max CGPA: %d\n”, maxstNo);
printf(“The student’s CGPA: %d\n”, maxCGPA);
}

C Program For insert , delete ,display a new item in the Linked List

#include<stdio.h>
#include<conio.h>
/*
you can insert a new item to the end of the Linked List, delete the last
item from the linked list and display all the list items in the linked list. Note that the Linked List
contains integer items.      */

/*contains malloc & free functions */
#define NULL 0
struct node{
int info;
struct node *next;
};
typedef struct node nodeptr;
int menu(void);
void insertEnd(nodeptr **, int);/*insert to the end of the list*/
void deleteEnd(nodeptr **, int *);/*delete from the end " " "*/
void displayList(nodeptr *);
nodeptr *getnode(void);
void freenode(nodeptr *);
int main()
{
int choice,x;
nodeptr *List= NULL; /*List points the beginning of the list*/
do{
choice = menu();
switch(choice){
case 1:
printf("Enter new data item:");
scanf("%d",&x);
insertEnd(&List,x);
break;
case 2:
deleteEnd(&List,&x);
printf("The deleted data item is: %d",x);
break;
case 3:
displayList(List);
break;
default:
if(choice == 4)
printf("\nEnd of the Program");
else
printf("\nEnter the choice again");
break;
}
}while(choice !=4);
return 0;
}

/*
Use double pointer if you are changing the
content of the List which is a pointer to the
beginning of the Linked List.
Use single pointer if you are not changing the
content of the List which is a pointer to the
beginning of the Linked List.*/

int menu(void)
{
int c;
printf("\nMain Menu:\n");
printf("1 - Insert an item\n");
printf("2 - Delete an item\n");
printf("3 - Display the Linked List items\n");
printf("4 - Quit\n\n");
scanf("%d",&c);
return c;
}
void insertEnd(nodeptr **plist, int x)
{
nodeptr *p, *q;
p=getnode();
p->info = x;
p->next = NULL;
if(*plist == NULL)
*plist = p;
else{
q = *plist;
while(q->next != NULL)
q = q->next;
q->next = p;
}
}
void deleteEnd(nodeptr **plist, int *px)
{
nodeptr *p,*q;
if(*plist == NULL){
printf("void deletion\n");
return;
}
p = *plist; /*current node*/
q = p->next; /*the node after the current node*/
if(q == NULL){
*px = p->info; /*the data of the deleted element*/
freenode(p);
*plist = NULL;
}
else{
while(q->next != NULL){
p = p->next; /*go to the next node*/
q = p->next;
}
p->next = NULL;
*px = q->info; /*the data of the deleted element*/
freenode(q);
}
}

/*
Use double pointer if you are changing the
content of the plist which is a pointer to the
beginning of the Linked List.
Use double pointer if you are changing the
content of the plist which is a pointer to the
beginning of the Linked List.
*/

void displayList(nodeptr *plist)
{
nodeptr *p;
p = plist; /*current node*/
if(p == NULL){
printf("no node/data is available in the list\n");
return;
}
while(p!= NULL){
printf("%5d", p->info); /*prints the current data*/
p = p->next;
}
}
nodeptr *getnode(void)
{
nodeptr *p;
p = (nodeptr *) malloc(sizeof(nodeptr));
return p;
}
void freenode(nodeptr *p)
{
free(p);
}

Linear linked list structure In C Programming

#include<stdio.h>
#include<conio.h>

/*contains malloc & free functions */
#define NULL 0
struct node{
int info;
struct node *next;
};
typedef struct node nodeptr;
int menu(void);
void insertEnd(nodeptr **, int);/*insert to the end of the list*/
void deleteEnd(nodeptr **, int *);/*delete from the end " " "*/
void displayList(nodeptr *);
nodeptr *getnode(void);
void freenode(nodeptr *);
int main()
{
int choice,x;
nodeptr *List= NULL; /*List points the beginning of the list*/
do{
choice = menu();
switch(choice){
case 1:
printf("Enter new data item:");
scanf("%d",&x);
insertEnd(&List,x);
break;
case 2:
deleteEnd(&List,&x);
printf("The deleted data item is: %d",x);
break;
case 3:
displayList(List);
break;
default:
if(choice == 4)
printf("\nEnd of the Program");
else
printf("\nEnter the choice again");
break;
}
}while(choice !=4);
return 0;
}

/*
Use double pointer if you are changing the
content of the List which is a pointer to the
beginning of the Linked List.
Use single pointer if you are not changing the
content of the List which is a pointer to the
beginning of the Linked List.*/

int menu(void)
{
int c;
printf("\nMain Menu:\n");
printf("1 - Insert an item\n");
printf("2 - Delete an item\n");
printf("3 - Display the Linked List items\n");
printf("4 - Quit\n\n");
scanf("%d",&c);
return c;
}
void insertEnd(nodeptr **plist, int x)
{
nodeptr *p, *q;
p=getnode();
p->info = x;
p->next = NULL;
if(*plist == NULL)
*plist = p;
else{
q = *plist;
while(q->next != NULL)
q = q->next;
q->next = p;
}
}
void deleteEnd(nodeptr **plist, int *px)
{
nodeptr *p,*q;
if(*plist == NULL){
printf("void deletion\n");
return;
}
p = *plist; /*current node*/
q = p->next; /*the node after the current node*/
if(q == NULL){
*px = p->info; /*the data of the deleted element*/
freenode(p);
*plist = NULL;
}
else{
while(q->next != NULL){
p = p->next; /*go to the next node*/
q = p->next;
}
p->next = NULL;
*px = q->info; /*the data of the deleted element*/
freenode(q);
}
}

/*
Use double pointer if you are changing the
content of the plist which is a pointer to the
beginning of the Linked List.
Use double pointer if you are changing the
content of the plist which is a pointer to the
beginning of the Linked List.
*/

void displayList(nodeptr *plist)
{
nodeptr *p;
p = plist; /*current node*/
if(p == NULL){
printf("no node/data is available in the list\n");
return;
}
while(p!= NULL){
printf("%5d", p->info); /*prints the current data*/
p = p->next;
}
}
nodeptr *getnode(void)
{
nodeptr *p;
p = (nodeptr *) malloc(sizeof(nodeptr));
return p;
}
void freenode(nodeptr *p)
{
free(p);
}

Wednesday, 11 June 2014

stack implementation using Linked List In C Programing

#include<stdio.h>
#include<conio.h>
void push_stack ();
void pop_stack ();
void display_stack ();

typedef struct node
{
int data_stack ;
struct node *link;
}d;
d*top=NULL;


main()
{
int d;
printf("\tMENU\n1.PUSH_STACK \n2.POP_STACK \n3.DISPLAY_STACK \n4.EXIT\n");
do
{
printf("\nEnter your choice\n");
scanf("%d",&d);
switch(d)
{
case 1:
push_stack ();
break;
case 2:
pop_stack ();
break;
case 3:
display_stack ();
break;
case 4:
break;
default:
printf("Invalid choice\n");
break;
}
}
while(d!=4);
}


void push_stack ()
{
int item;
d *temp;
printf("Enter the item\n");
scanf("%d",&item);
temp=(d*)malloc(sizeof(d));
temp->data_stack =item;
temp->link=top;
top=temp;
}

void pop_stack ()
{
d *temp;
if(top==NULL)
printf("Stack is empty\n");
else
{
temp=top;
printf("The element deleted = %d\n",temp->data_stack );
top=top->link;

free(temp);
}
}

void display_stack ()
{
d *save;
if(top==NULL)
printf("Stack is empty\n");
else
{
save=top;
printf("The elements of the stack are :");
while(save!=NULL)
{
printf("%d\t",save->data_stack );
save=save->link;
}
printf("\nTopmost element = %d\n",top->data_stack );
}
}

Create Stack And Insert Data Using C Programming

#include<stdio.h>
#include<conio.h>
#define col 4
#define row 4

void main()
{
            int i,n,j;
            float ax[row+1], ay[col+1],diff[row+1][col+1],x,p,ya1,ya2,ya3,ya4,ya,ha;

            clrscr();

            printf("Enter thae value of n:");
            scanf("%d",&n);

            printf("\n Enter thae values of X and Y:\n");
            for(i=0; i<=n; i++)
            {
                        scanf("%f %f",&ax[i],&ay[i]);
            }

            printf("Value of X:");
            scanf("%f",&x);

            ha=ax[1]-ax[0];

            for(i=0; i<=n-1; i++)
                        diff[i][1]=ay[i+1]-ay[i];
            for(j=2; j<=row; j++)
                        for(i=0; i<=n-j; i++)
                                    diff[i][j]=diff[i+1][j-1]-diff[i][j-1];

            p=(x-ax[0])/ha;
            ya1=p*diff[0][1];
            ya2=p*(p-1)*diff[0][2]/2;
            ya3=p*(p-1)*(p-2)*diff[0][3]/6;
            ya4=p*(p-1)*(p-2)*(p-3)*diff[0][4]/24;
            ya=ay[0]+ya1+ya2+ya3+ya4;
            printf("%f",ya);
            getcha();



}

Create Stack Using C Programming

#include<stdio.h>
#include<conio.h>
#define col 4
#define row 4

void main()
{
            int i,n,j;
            float ax[row+1], ay[col+1],diff[row+1][col+1],x,p,ya1,ya2,ya3,ya4,ya,ha;

            clrscr();

            printf("Enter thae value of n:");
            scanf("%d",&n);

            printf("\n Enter thae values of X and Y:\n");
            for(i=0; i<=n; i++)
            {
                        scanf("%f %f",&ax[i],&ay[i]);
            }

            printf("Value of X:");
            scanf("%f",&x);

            ha=ax[1]-ax[0];

            for(i=0; i<=n-1; i++)
                        diff[i][1]=ay[i+1]-ay[i];
            for(j=2; j<=row; j++)
                        for(i=0; i<=n-j; i++)
                                    diff[i][j]=diff[i+1][j-1]-diff[i][j-1];

            p=(x-ax[0])/ha;
            ya1=p*diff[0][1];
            ya2=p*(p-1)*diff[0][2]/2;
            ya3=p*(p-1)*(p-2)*diff[0][3]/6;
            ya4=p*(p-1)*(p-2)*(p-3)*diff[0][4]/24;
            ya=ay[0]+ya1+ya2+ya3+ya4;
            printf("%f",ya);
            getcha();



}

Friday, 2 May 2014

Quiz Game in C

#include<stdio.h>
#include<conio.h>
#include<dos.h>
void valid()
{
printf("\n \n Correct Answer\n");
}
void invalid()
{
printf("\n \n Wrong Answer\n");
}
void wel()
{
printf("\n\n \t\t\tWelcome to the C quiz \n");
}
void note()
{
printf("\n\n\nBASIC RULES OF GAME:-");
printf("\n\n\n\t --> The quiz has 5 very basic questions of the C Programming Language.\n");
printf("\t --> Programe can accept only  digits not a character.\n");
printf("\t --> You get '+3' point for every correct answer and '-1' for every\n \t     incorrect answer.\n");
printf("\t --> you have to choose only one option from '1,2,3,4'. \n");
printf("\n\n\n\n\n");
}
void main()
{

int score=0;
int answer;
char array[10];
int x=400, y =400;
clrscr();
wel();
printf("\n\n \t\t\tEnter your name: ");
gets(array);
note();
printf("\t \t Q1) Why is it called 'C'& not 'D' ?\n");
printf("\n\n \t \t[1]C stands for code\n\n \t \t[2]The inventors name started with a C\n\n \t \t[3]It developed after a language called 'B'\n\n \t \t[4]Why should I care?\n");
scanf("%d", &answer);
if(answer==1||answer==2||answer==3||answer==4)
 {
  if(answer==3)
  {
  valid();
  score=score+3;
  }
 else
 {
 invalid();
 score=score-1;
 }
}
else
 {
 printf( "invalid\n" );
 }
delay(2000);
clrscr();
wel();
note();
printf("\n\n \t \tQ2) It was developed at?\n");
printf("\n \t \t [1]IBMt\n\n \t \t [2]Bell Labsn\n\n \t \t [3]MITt\n\n \t \t [4]Microsoft(?)\n");
scanf("%d", &answer);
if(answer==1||answer==2||answer==3||answer==4)
{
if(answer==2)
{
valid();
score=score+3;
}
else
{
invalid();
score=score-1;
}
}
else
{
printf( "invalid\n" );
}
delay(2000);
clrscr();
wel();
note();

printf("\n\n \t \tQ3) Which of these is not a C keyword as per ANSI C ?\n");
printf("\n \t \t [1]externt\n\n \t \t [2]volatilen\n\n \t \t [3]entert\n\n \t \t [4]break\n");
scanf("%d", &answer);
if(answer==1||answer==2||answer==3||answer==4)
{
if(answer==3)
{
valid();
score=score+3;
}
else
{
invalid();
score=score-1;
}
}
else
{
printf( "invalid\n" );
}
delay(2000);
clrscr();
wel();
note();

printf("\n\n \t \tQ4) What is ANSI, btw? ?\n");
printf("\n \t \t [1]Area of Natural and Scientific Interest\n\n \t \t [2]American National Standards Institute\n\n \t \t [3]American National Standardization Institute\n\n \t \t [4]American National Society Of Intellectuals\n");
scanf("%d", &answer);
if(answer==1||answer==2||answer==3||answer==4)
{
if(answer==2)
{
valid();
score=score+3;
}
else
{
invalid();
score=score-1;
}}
else
{
printf( "invalid\n" );
}
delay(2000);
clrscr();
wel();
note();

printf("\n\n \t \tQ5)Which of these concepts is NOT supported by C ?\n");
printf("\n \t \t [1]Pointers\n\n \t \t [2]Functions\n\n \t \t [3]Strings\n\n \t \t [4]Namespaces\n");
scanf("%d", &answer);
if(answer==1||answer==2||answer==3||answer==4)
{
if(answer==4)
{
valid();
score=score+3;
}
else
{
invalid();
score=score-1;
}
}
else
{
printf( "invalid\n" );
}
delay(2000);
clrscr();
wel();

textcolor ( 1 );
cprintf("\n\n\n\n\n\n\n\n              Thank You for taking the Quiz.");
puts(array);
printf("\n\n\n\n\t \t \tTotal Score is:- %d out of 15", score);
getch();
}