This is just a linked list program for those who need help understanding the fundamentals. Put comments if you find some bugs.
The program is about maintaining a student database (their roll number and their age)
I've used Turbo C++ 4.5 as the compiler

:icon_cool:

Edited 7 Years Ago by __avd: Snip email

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

typedef struct student
{
	int roll;
	int age;
	struct student *link;
}std;

std *head;	std *temp;	std *temp2;	std *add;
int i;		int r;		int a;		int n;	int pos;	int fRoll;

void append(int r,int a)
{
	temp2 = (std *)malloc(sizeof(std));
	temp2 -> roll = r;
	temp2 -> age = a;

	temp = head;

	if(temp == NULL)
	{
		head = temp2;
		head -> link = NULL;
	}
	else
	{
		while(temp -> link != NULL)
		{
			temp = temp -> link;
		}
		temp -> link = temp2;
		temp2 -> link = NULL;
	}
}

void display(void)
{
	temp = head;
	while(temp != NULL)
	{
		printf("\tRoll No. : %d\tAge : %d\n", temp -> roll, temp -> age);
		temp = temp -> link;
	}
}

void search(int r)
{
	temp = head;
	while(temp != NULL)
	{
		if(temp -> roll == r)
		{
			printf("\n\tRoll Number Exists!\n");
			printf("\tRoll No. : %d\tAge : %d\n", temp -> roll, temp -> age);
			return;
		}
		temp = temp -> link;
	}
	printf("\n\n\tSpecified Roll Number Does Not Exist!");
}

void addAtBegin(int r,int a)
{
	temp2 = (std *)malloc(sizeof(std));
	temp2 -> roll = r;
	temp2 -> age = a;
	temp2 -> link = head;
	head = temp2;
}

void delRoll(int r)
{
	temp = head -> link;
	temp2 = head;

	if(temp2 -> roll == r)
	{
		head = temp;
		free(temp2);
		return;
	}

	while(temp -> roll != r)
	{
		temp = temp -> link;
		temp2 = temp2 -> link;
	}
	if(temp == NULL)
	{
		printf("\n\tRoll Number Does Not Exist");
		return;
	}

	temp = temp -> link;
	free(temp2 -> link);
	temp2 -> link = temp;
}

void addAtLoc(int pos,int r,int a)
{
	add = (std *)malloc(sizeof(std));
	add -> roll = r;
	add -> age = a;

	if(pos == 1)
	{
		add -> link = head;
		head = add;
		return;
	}

	temp = head;
	for(i = 0;i < pos - 2;i++)
	{
		temp = temp -> link;
		if(temp == NULL)
		{
			printf("\n\tLocation Does Not Exist");
			return;
		}
	}

	temp2 = temp -> link;
	temp -> link = add;
	add -> link = temp2;
}

void addAftRoll(int fRoll,int r,int a)
{
	temp = head;
	while(temp -> roll != fRoll)
	{
		temp = temp -> link;
		if(temp == NULL)
		{
			printf("\n\tRoll Number Does Not Exist");
			return;
		}
	}

	temp2 = temp -> link;

	add = (std *)malloc(sizeof(std));
	add -> roll = r;
	add -> age = a;

	temp -> link = add;
	add -> link = temp2;
}

int main()
{
	do
	{
		clrscr();
		printf("\n\t================");
		printf("\n\tSTUDENT DATABASE");
		printf("\n\t================\n");
		printf("\n\t1 - Apending Information About A Student");
		printf("\n\t2 - Displaying Information Of All Students");
		printf("\n\t3 - Searching Information About A Student");
		printf("\n\t4 - Deleting Information About A Student According To Roll No");
		printf("\n\t5 - Adding Information About A Student At Begining");
		printf("\n\t6 - Adding Information About A Student At Any Location");
		printf("\n\t7 - Adding Information About A Student After A Roll Number");
		printf("\n\t8 - Exit");
		printf("\n\n\tChoice : ");
		fflush(stdin);
		scanf("%d",&n);
		switch(n)
		{
			case 1 :
				clrscr();
				printf("\n\n\tEnter Roll Number : ");
				scanf("%d",&r);
				printf("\n\n\tEnter Age : ");
				scanf("%d",&a);
				append(r,a);
				getch();
				break;
			case 2 :
				clrscr();
				printf("\n\n\tInformation List\n\n");
				display();
				getch();
				break;
			case 3 :
				clrscr();
				printf("\n\n\tEnter The Roll Number You Want To Search : ");
				scanf("%d",&r);
				search(r);
				getch();
				break;
			case 4 :
				clrscr();
				printf("\n\n\tEnter The Roll Number You Want To Delete : ");
				scanf("%d",&r);
				delRoll(r);
				getch();
				break;
			case 5 :
				clrscr();
				printf("\n\n\tEnter Roll Number : ");
				scanf("%d",&r);
				printf("\n\n\tEnter Age : ");
				scanf("%d",&a);
				addAtBegin(r,a);
				getch();
				break;
			case 6 :
				clrscr();
				printf("\n\n\tEnter Location : ");
				scanf("%d",&pos);
				printf("\n\n\tEnter Roll Number : ");
				scanf("%d",&r);
				printf("\n\n\tEnter Age : ");
				scanf("%d",&a);
				addAtLoc(pos,r,a);
				getch();
				break;
			case 7 :
				clrscr();
				printf("\n\n\tEnter Roll Number After Which New Data Will Be Put : ");
				scanf("%d",&fRoll);
				printf("\n\n\tEnter Roll Number : ");
				scanf("%d",&r);
				printf("\n\n\tEnter Age : ");
				scanf("%d",&a);
				addAftRoll(fRoll,r,a);
				getch();
				break;
			case 8 :
				printf("\n\n\tThank You!");
				getch();
				break;
			default :
				printf("\n\n\tThe Specified Option Does Not Exist!");
				getch();
				break;
		}
	}while(n!=8);
	return 0;
}

1) remove conio.h -- its non'standard and not implemented by very many compilers. Code snippets should not use compiler-specific stuff.

2) delete all those global variables on lines 12 and 13. Just declare them inside the functions that use them. And you will probably have to pass the std* head pointer as a parameter to the functions that use it.

3) a few comments would be nice to explain what those functions do. Your entire program contains not even one comment. Subtract 3 points from your grade for that.

If I were going to grade this I would give it a grade of B. There is a lot of good code there, just needs to be cleaned up.

Edited 7 Years Ago by Ancient Dragon: n/a

remove conio.h

I wanted to do that but I need a C equivalent of clrscr()
Otherwise, the output looks bad :'(

delete all those global variables

I've seen that, in my compiler, if i declare a variable globally, the problem of garbage values disappears. If only there was a constructor in C, this problem would have never risen :sad:

a few comments would be nice to explain what those functions do

I thought the names were enough ... but okay, I'll give the comments but will you improve my grades? :P

I'll edit the code later but i will ...

Edited 7 Years Ago by xavier666: n/a

Okay, this is a temporary fix. Love it or leave it :p

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

typedef struct student
{
	int roll;
	int age;
	struct student *link;
}std;

void append(int,int);			//	Creating the database for the 1st time
void display(void);				//	Displays the entire database
void search(int);              	//	Searches for a particular roll number
void delRoll(int);              //	Deletes an entry according to the given roll number
void addAtBegin(int,int);   	//	Inserts an entry in the begining
void addAtLoc(int,int,int);    	//	Inserts an entry at the given location
void addAftRoll(int,int,int);   //	Inserts an entry after a specified roll number
void noOfStudents(void);		//  Finds the total number of students

std *head;	std *temp;	std *temp2;	std *add;

int main()
{
	int n;	int r;	int a;	int pos;	int fRoll;

	do
	{
		clrscr();
		printf("\n\t================");
		printf("\n\tSTUDENT DATABASE");
		printf("\n\t================\n");
		printf("\n\t1 - Apending Information About A Student");
		printf("\n\t2 - Displaying Information Of All Students");
		printf("\n\t3 - Searching Information About A Student");
		printf("\n\t4 - Deleting Information About A Student According To Roll No");
		printf("\n\t5 - Adding Information About A Student At Begining");
		printf("\n\t6 - Adding Information About A Student At Any Location");
		printf("\n\t7 - Adding Information About A Student After A Roll Number");
		printf("\n\t8 - Finds Total Number Of Students");
		printf("\n\t9 - Exit");
		printf("\n\n\tChoice : ");
		fflush(stdin);
		scanf("%d",&n);
		switch(n)
		{
			case 1 :
				clrscr();
				printf("\n\n\tEnter Roll Number : ");
				scanf("%d",&r);
				printf("\n\n\tEnter Age : ");
				scanf("%d",&a);
				append(r,a);
				getch();
				break;
			case 2 :
				clrscr();
				printf("\n\n\tInformation List\n\n");
				display();
				getch();
				break;
			case 3 :
				clrscr();
				printf("\n\n\tEnter The Roll Number You Want To Search : ");
				scanf("%d",&r);
				search(r);
				getch();
				break;
			case 4 :
				clrscr();
				printf("\n\n\tEnter The Roll Number You Want To Delete : ");
				scanf("%d",&r);
				delRoll(r);
				getch();
				break;
			case 5 :
				clrscr();
				printf("\n\n\tEnter Roll Number : ");
				scanf("%d",&r);
				printf("\n\n\tEnter Age : ");
				scanf("%d",&a);
				addAtBegin(r,a);
				getch();
				break;
			case 6 :
				clrscr();
				printf("\n\n\tEnter Location : ");
				scanf("%d",&pos);
				printf("\n\n\tEnter Roll Number : ");
				scanf("%d",&r);
				printf("\n\n\tEnter Age : ");
				scanf("%d",&a);
				addAtLoc(pos,r,a);
				getch();
				break;
			case 7 :
				clrscr();
				printf("\n\n\tEnter Roll Number After Which New Data Will Be Put : ");
				scanf("%d",&fRoll);
				printf("\n\n\tEnter Roll Number : ");
				scanf("%d",&r);
				printf("\n\n\tEnter Age : ");
				scanf("%d",&a);
				addAftRoll(fRoll,r,a);
				getch();
				break;
			case 8 :
				clrscr();
				noOfStudents();
				getch();
				break;
			case 9 :
				printf("\n\n\tThank You!");
				getch();
				break;
			default :
				printf("\n\n\tThe Specified Option Does Not Exist!");
				getch();
				break;
		}
	}while(n!=9);
	return 0;
}

void append(int r,int a)
{
	temp2 = (std *)malloc(sizeof(std));
	temp2 -> roll = r;
	temp2 -> age = a;

	temp = head;

	if(temp == NULL)
	{
		head = temp2;
		head -> link = NULL;
	}
	else
	{
		while(temp -> link != NULL)
		{
			temp = temp -> link;
		}
		temp -> link = temp2;
		temp2 -> link = NULL;
	}
}

void display(void)
{
	temp = head;
	if(temp == NULL)
	{
		printf("\n\n\tEmpty Database!");
		return;
	}

	while(temp != NULL)
	{
		printf("\tRoll No. : %d\tAge : %d\n", temp -> roll, temp -> age);
		temp = temp -> link;
	}
}

void search(int r)
{
	temp = head;
	while(temp != NULL)
	{
		if(temp -> roll == r)
		{
			printf("\n\tRoll Number Exists!\n");
			printf("\tRoll No. : %d\tAge : %d\n", temp -> roll, temp -> age);
			return;
		}
		temp = temp -> link;
	}
	printf("\n\n\tSpecified Roll Number Does Not Exist!");
}

void addAtBegin(int r,int a)
{
	temp2 = (std *)malloc(sizeof(std));
	temp2 -> roll = r;
	temp2 -> age = a;
	temp2 -> link = head;
	head = temp2;
}

void delRoll(int r)
{
	if(head == NULL)
	{
		printf("\n\n\tEmpty Database!");
		return;
	}
	temp = head -> link;
	temp2 = head;

	if(temp2 -> roll == r)
	{
		head = temp;
		free(temp2);
		return;
	}

	while(temp -> roll != r)
	{
		temp = temp -> link;
		temp2 = temp2 -> link;
        if(temp == NULL)
		{
			printf("\n\tRoll Number Does Not Exist");
			return;
		}
	}

	temp = temp -> link;
	free(temp2 -> link);
	temp2 -> link = temp;
}

void addAtLoc(int pos,int r,int a)
{
	int i;
	add = (std *)malloc(sizeof(std));
	add -> roll = r;
	add -> age = a;

	if(pos == 1)
	{
		add -> link = head;
		head = add;
		return;
	}

	temp = head;
	for(i = 0;i < pos - 2;i++)
	{
		temp = temp -> link;
		if(temp == NULL)
		{
			printf("\n\tLocation Does Not Exist");
			return;
		}
	}

	temp2 = temp -> link;
	temp -> link = add;
	add -> link = temp2;
}

void addAftRoll(int fRoll,int r,int a)
{
	temp = head;
	while(temp -> roll != fRoll)
	{
		temp = temp -> link;
		if(temp == NULL)
		{
			printf("\n\tRoll Number Does Not Exist");
			return;
		}
	}

	temp2 = temp -> link;

	add = (std *)malloc(sizeof(std));
	add -> roll = r;
	add -> age = a;

	temp -> link = add;
	add -> link = temp2;
}

void noOfStudents(void)
{
	int l = 0;
	temp = head;
	while(temp!=NULL)
	{
		temp = temp -> link;
		l++;
	}
	printf("\n\n\tTotal Number Of Students : %d", l);
}