#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct dlinklist
{
struct dlinklist *prev; /** Stores address of previous node **/
int masv; /** stores roll number **/
char hovaten[30]; /** stores Name **/
float diemk1;
float diemk2; /** stores Marks **/
struct dlinklist *next; /** stores address of next node **/
};
/** Redefining dlinklist as node **/
typedef struct dlinklist node;
void init(node*); /** Input function **/
void disp(node*); /** Function for displaying node **/
void namesrch(node*); /** Function for searching node by name **/
void main()
{
node *head;
char ch; /* Choice inputing varible */
int opt; /* Option inputing variable*/
static int flag=0; /* Unchanged after iniialization */
head=(node*)malloc(sizeof(node));
head->next=NULL;
head->prev=NULL;
do
{
again:
printf("\nNhap vao yeu cau cua ban:\n");
printf("\n1. Nhap sdu lieu vao danh sach\n");
printf("\n2. Dien thi du lieu\n");
printf("\n3. Tim kiem ket qua\n");
scanf("%d",&opt);
if(flag==0 && opt!=1)
{
printf("\nNo. You must first initialize at least one node\n");
goto again;
}
if(flag==1 && opt==1)
{
printf("\nInitialisation can occur only once.\n");
printf("\nNow you can insert a node\n");
goto again;
}
if(opt==4 && head->next==NULL)
{
printf("\nYou cannot delete the one and only the single node\n");
goto again;
}
if(flag==0 && opt==1)
flag=1;
switch(opt)
{
case 1:
init(head);
break;
case 2:
disp(head);
break;
case 3:
namesrch(head);
break;
}
printf("\nBan co muon tiep tuc[y/n]\n");
ch=getche();
}while(ch=='Y' || ch=='y');
printf("\nPress any key to exit\n");
}
void init(node *current)
{
current->prev=NULL;
printf("\nNhap vao SHSV:\n");
scanf("%d",¤t->masv);
printf("\nNhap vao ten sinh vien:\n");
fflush(stdin);
gets(current->hovaten);
printf("\nNhap vao diem Ky 1:\n");
scanf("%f",¤t->diemk1);
current->next=NULL;
printf("\nNhap vao diem Ky 2:\n");
scanf("%f",¤t->diemk2);
current->next=NULL;
}
void namesrch(node *current)
{
char arr[20];
printf("\nNhap vao ten sinh vien can tim: \n");
fflush(stdin);
gets(arr);
while(current->next!=NULL)
{
if(strcmp(current->hovaten,arr)==NULL)
printf("\n%d\t%s\t%f\n",current->masv,current->hovaten,current->diemk1,current->diemk2);
current=current->next;
}
if(current->next==NULL && strcmp(current->hovaten,arr)==NULL)
printf("\n%d\t%s\t%f\n",current->masv,current->hovaten,current->diemk1,current->diemk2);
}
void disp(node *current)
{
while(current!=NULL)
{
printf("\n%d\t%s\t%f",current->masv,current->hovaten,current->diemk1,current->diemk2);
current=current->next;
}
}