CREATE INSERTING A NEW NODE IN A SORTED LIST
This is program to insert a new node into an already sorted list, we compare the data value of the node to be inserted with the data values of the nodes in the list starting from the first node. This is continued until we get a pointer to the node that appears immediately before the node in the list whose data value is greater than the data value of the node to be inserted.
- This is flowchart of this project:
- This is source code:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *link;
};
struct node *insert(struct node *, int);
struct node *sinsert(struct node*, int );
void printlist ( struct node * );
struct node *sortlist(struct node *);
struct node *insert(struct node *p, int n)
{
struct node *temp;
if(p==NULL)
{
p=(struct node *)malloc(sizeof(struct node));
if(p==NULL)
{
printf("Error\n");
exit(0);
}
p-> data =
n;
p-> link = NULL;
}
else
{
temp = p;
while (temp-> link!= NULL)
temp = temp-> link;
temp-> link = (struct node *)malloc(sizeof(struct node));
if(temp -> link == NULL)
{
printf("Error\n");
exit(0);
}
temp = temp-> link;
temp-> data = n;
temp-> link = NULL;
}
return (p);
}
void printlist ( struct node *p )
{
printf("The data values in the list are\n");
while (p!= NULL)
{
printf("%d\t",p-> data);
p = p-> link;
}
}
/* a function to sort a list */
struct node *sortlist(struct node *p)
{
struct node *temp1,*temp2,*min,*prev,*q;
q = NULL;
while(p != NULL)
{
prev = NULL;
min = temp1 = p;
temp2 = p -> link;
while ( temp2 != NULL )
{
if(min -> data > temp2 -> data)
{
min = temp2;
prev = temp1;
}
temp1 = temp2;
temp2 = temp2-> link;
}
if(prev == NULL)
p = min -> link;
else
prev -> link = min -> link;
min -> link = NULL;
if( q == NULL)
q = min; /* moves the node with lowest data value in the list
pointed to by p to the list
pointed to by q as a first node*/
else
{
temp1 = q;
/* traverses the list pointed to by q to get pointer to its
last node */
while( temp1 -> link != NULL)
temp1 = temp1 -> link;
temp1 -> link = min; /* moves the node with lowest data value
in the list pointed to
by p to the list pointed to by q at the end of list pointed by
q*/
}
}
return (q);
}
/* a function to insert a node with data value n in a sorted list
pointed to by p*/
struct node *sinsert(struct node *p, int n)
{
struct node *curr, *prev;
curr =p;
prev = NULL;
while(curr ->data < n)
{
prev = curr;
curr = curr->link;
}
if ( prev == NULL) /* the element is to be inserted at the start of
the list because
it is less than the data value of the first node*/
{
curr = (struct node *) malloc(sizeof(struct node));
if( curr == NULL)
{
printf("error cannot allocate\n");
exit(0);
}
curr->data = n;
curr->link = p;
p = curr;
}
else
{
curr->data = n;
curr->link = prev->link;
prev->link = curr;
}
return(p);
}
void main()
{
int n;
int x;
struct node *start = NULL ;
printf("Enter the nodes to be created \n");
scanf("%d",&n);
while ( n-- > 0 )
{
printf( "Enter the data values to be placed in a node\n");
scanf("%d",&x);
start = insert ( start,x);
}
printf("The created list is\n");
printlist ( start );
start = sortlist(start);
printf("The sorted list is\n");
printlist ( start );
printf("Enter the value to be inserted\n");
scanf("%d",&n);
start = sinsert(start,n);
printf("The list after insertion is\n");
printlist ( start );
system("pause");
}
---------------------------------------------------------------------------------------------------------------
- This is output of this project:
- Video link youtube:
http://www.youtube.com/watch?v=27ixdCN1BHU&feature=youtu.be
Create data structure of staff in C programming
This is Data structure of staff in C programming. This project will show you about information of staff but if we input number 2 you can input information of staff only 2 staff but if you input number 10 you can input information of staff 10 staff too but this project will show which staff that have age start from 40 years old .
- This is flowchart of the project:
- This is source code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct date
{ int day;
char month[10];
int year;
};
struct staff
{ char name[30];
int age;
struct date datein;
float wage;
};
main( )
{
int i,n,count;
float x;
struct staff s[51];
LB:
system("cls"); //clrscr();
printf("\n Input n(0<n<50):");
scanf("%d",&n);
if (n<=0 || n>=50) goto LB;
printf("Input data to the staff\n");
for(i=0; i<n; i++)
{
printf("%d\t Name=",i+1); fflush(stdin); gets(s[i].name);
printf("\tAge="); scanf("%d",&s[i].age);
printf("\tDay in=");
scanf("%d",&s[i].datein.day);
printf("\tMonth in="); fflush(stdin);
gets(s[i].datein.month);
printf("\tYear in=");
scanf("%d",&s[i].datein.year);
printf("\tWage=");
scanf("%d",&x);
s[i].wage=x;
}
system("cls"); //clrscr();
printf("Staff must be over 40 years old\n");
count=0;
for (i=0; i<n;i++)
if(s[i].age>=40)
{ printf("\n %d Name:%s Age: %d", ++count, s[i].name, s[i].age);
printf("\n Date in:%d -%s -%d", s[i].datein.day, s[i].datein.month, s[i].datein.year);
printf("\n Wage: %0.2f", s[i].wage);
}
getch();
return(0);
}
#include<conio.h>
#include<stdlib.h>
struct date
{ int day;
char month[10];
int year;
};
struct staff
{ char name[30];
int age;
struct date datein;
float wage;
};
main( )
{
int i,n,count;
float x;
struct staff s[51];
LB:
system("cls"); //clrscr();
printf("\n Input n(0<n<50):");
scanf("%d",&n);
if (n<=0 || n>=50) goto LB;
printf("Input data to the staff\n");
for(i=0; i<n; i++)
{
printf("%d\t Name=",i+1); fflush(stdin); gets(s[i].name);
printf("\tAge="); scanf("%d",&s[i].age);
printf("\tDay in=");
scanf("%d",&s[i].datein.day);
printf("\tMonth in="); fflush(stdin);
gets(s[i].datein.month);
printf("\tYear in=");
scanf("%d",&s[i].datein.year);
printf("\tWage=");
scanf("%d",&x);
s[i].wage=x;
}
system("cls"); //clrscr();
printf("Staff must be over 40 years old\n");
count=0;
for (i=0; i<n;i++)
if(s[i].age>=40)
{ printf("\n %d Name:%s Age: %d", ++count, s[i].name, s[i].age);
printf("\n Date in:%d -%s -%d", s[i].datein.day, s[i].datein.month, s[i].datein.year);
printf("\n Wage: %0.2f", s[i].wage);
}
getch();
return(0);
}
---------------------------------------------------------------------------------------------------------------
- Output program
Calendar in C Programming
This is Calendar project make in C Programming" Calendar project is an application use C Programming. This program speak about Calendar when you input year it will show calendar of the year.
Example : 2013.
- This is flowchart of this application:
- This is source code:
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
char *months[]=
{
" ",
"\n\n\nJanuary",
"\n\n\nFebruary",
"\n\n\nMarch",
"\n\n\nApril",
"\n\n\nMay",
"\n\n\nJune",
"\n\n\nJuly",
"\n\n\nAugust",
"\n\n\nSeptember",
"\n\n\nOctober",
"\n\n\nNovember",
"\n\n\nDecember"
};
int inputyear(void)
{
int year;
printf("Please enter a year (example: 1999) : ");
scanf("%d", &year);
return year;
}
int determinedaycode(int year)
{
int daycode;
int d1, d2, d3;
d1 = (year - 1.)/ 4.0;
d2 = (year - 1.)/ 100.;
d3 = (year - 1.)/ 400.;
daycode = (year + d1 - d2 + d3) % 7;
return daycode;
}
int determineleapyear(int year)
{
if(year % 4 == FALSE && year % 100 != FALSE || year % 400 == FALSE)
{
days_in_month[2] = 29;
return TRUE;
}
else
{
days_in_month[2] = 28;
return FALSE;
}
}
void calendar(int year, int daycode)
{
int month, day;
for ( month = 1; month <= 12; month++ )
{
printf("%s", months[month]);
printf("\n\nSun Mon Tue Wed Thu Fri Sat\n" );
// Correct the position for the first date
for ( day = 1; day <= 1 + daycode * 5; day++ )
{
printf(" ");
}
// Print all the dates for one month
for ( day = 1; day <= days_in_month[month]; day++ )
{
printf("%2d", day );
// Is day before Sat? Else start next line Sun.
if ( ( day + daycode ) % 7 > 0 )
printf(" " );
else
printf("\n " );
}
// Set position for next month
daycode = ( daycode + days_in_month[month] ) % 7;
}
}
int main(void)
{
int year, daycode, leapyear;
year = inputyear();
daycode = determinedaycode(year);
determineleapyear(year);
calendar(year, daycode);
printf("\n");
system("pause");
return 0;
}
------------------------------------------------------------------------
- Output program
- This is my video.
http://www.youtube.com/watch?v=ZmhZeD8Q4qs&feature=youtu.be
http://www.youtube.com/watch?v=ZmhZeD8Q4qs&feature=youtu.be
No comments:
Post a Comment