I am trying to make a linked list using only structs. Thing is, I'm really bad at programming. And therefore I make ALOT of mistakes. If anyone has the patience to go through this and point out the mistakes and how to correct them, please do. I need to do this by morning :/
Plus the code isn't complete I'm still trying to get the AddNodes part working.
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
usingnamespace std;
/* The menu options are defined here */
#define ADD 1
#define DELETE 2
#define MODIFY 3
#define SEARCH 4
#define DISPLAY 5
#define EXIT 6 // if you add some thing after this make sure to modify the condition
// of the do while loop.
/************************************/
/* The constant fileds */
constint LEN_NAME =10;
constint LEN_EMAIL =20;
constint LEN_TELNO =15;
/*Enumerated types for the status of the students */
enum StatusT {REGISTERED, ENROLLED, GRADUATED, CLOSED, DROPEDOUT };
// This Corresponds to above enum to convert the values into string
char *StatusStr[] = {"Registered", "Enrolled", "Graduated", "Closed", "Dropedout"};
/* The declaration of the student recrod type node. You can declare
* as many variable of this type as you like. Each one of them is
* going to have the same fields/atributes but of their own.
*/
struct NodeT {
int RollNo;
char Name[LEN_NAME];
char Section;
char Email[LEN_EMAIL];
char TelNo[LEN_TELNO];
StatusT Status;
NodeT *Next;
};
/* This is a global variable
* Should always points to the first node of list
*/
NodeT *Head = NULL;
/* Prototypes of the functions used in the C++ program file.
* If you add a new function in your program do not forget to
* put its prototype here. Any prototype declaration in the program file
* will heart your grade.
*/
void AddNewNode();
void AddFirstNode(NodeT*);
void AddLastNode(NodeT*);
void DeleteNode();
void ModifyNode();
void DisplayNode(NodeT*);
void DisplayCompleteList();
bool AddMiddleNode(NodeT*);
NodeT *GetNewNode();
NodeT *SearchNode();
/***************************** MAIN **************************************/
int main(){
int choice;
while(1){
do {
cout << "\n\n\n\n\n";
cout << "============================" << endl;
cout << " MAIN MENU " << endl;
cout << "============================" << endl;
cout << " "<< ADD <<": Add a new record" << endl;
cout << " "<< DELETE <<": Delete a record" << endl;
cout << " "<< MODIFY <<": Modify a record" << endl;
cout << " "<< SEARCH <<": Search a record" << endl;
cout << " "<< DISPLAY<<": Display" << endl;
cout << " "<< EXIT <<": Exit" << endl;
cout << "============================" << endl;
cout << " Your choice :";
cin >> choice;
} while ((choice < ADD) || (choice > EXIT));
switch (choice){
case ADD : if(Head = NULL)
GetNewNode();
else
AddNewNode();
/* Check if it is very first node */
/* If yes creat and add */
/* if no then call AddNewNode() */
break;
case DELETE : /* Call the Appropriate functions */
break;
case MODIFY : /* Call the appropriate functions */
break;
case SEARCH : /* Search and dispalay the record of a student */
break;
case DISPLAY: /* shoudl display the complete list */
break;
case EXIT : cout<<"Thankyou for using this program...Good day:"<<endl;
system("pause");
return 0;
} // switch(choice)
} // while(1)
system("pause");
} //main()
/*********************************** GetNewNode()***********************************/
NodeT *GetNewNode(){
NodeT *NewNode = NULL;
NewNode = new NodeT;
NewNode->Next = NULL;
NewNode->Name;
NewNode->RollNo;
NewNode->Email;
NewNode->Section;
NewNode->TelNo;
NewNode->Status;
/* You must allocate memory on the heap for the new node
* Set different fields by getting input from the user
* Return the pointer of this node to the calling function
*
* i.e. this function must do step I and step II
*/
return NULL;
}
/*********************************** AddNewNode()***********************************/
void AddNewNode(){
NodeT *NewNode;
NodeT *Current;
Current = Head;
if(NewNode->RollNo < Head->RollNo)
AddFirstNode(NewNode);
elseif(NewNode->Next= NULL)
AddLastNode(NewNode);
else{
while ((Current->Next != NULL ) && (Current->Next->RollNo < NewNode->RollNo)) {
Current= Current->Next;
AddMiddleNode(NewNode);
}
}
/* Do the required declarations
*
*
* Make sure step I and step II have been done
*
*
* Now check the first possibility: i.e. adding in the front of list.
* else check for second possibility: i.e adding at the end of list.
* else it is to be added in the middle.
*
*/
}
/************************************ AddFirstNode() **************************************/
void AddFirstNode(NodeT *NewNode){
NewNode->Next = Head;
Head = NewNode;
NewNode = NULL;
}
/***************************** AddMiddleNode() *********************************/
bool AddMiddleNode(NodeT *NewNode){
NodeT *Current;
Current = Head;
NewNode->Next = Current->Next;
Current->Next = NewNode;
NewNode = Current = NULL;
returntrue; // This is a dumy statement to resolve the compiler error
}
/********************************* AddLastNode()******************************************/
void AddLastNode(NodeT *NewNode){
NodeT *Current;
Current->Next = NewNode;
NewNode = NULL;
}
/********************************* DeleteNode()******************************************/
void DeleteNode(){
/*
* Ask the user which node s/he wants to delete
* search the required node in the list
* if the node is found then remove it form the list
* free the memory reserved for the node by using delete operator.
*
* Make sure to handel differnt possibilites that may arrise due to
* deletion of the node.
*
*/
}
/*************************************** ModifyNode() ***********************************/
void ModifyNode(){
/*
*
*
*
*
*
*
*/
}
/*********************************** SearchNode()***************************************/
NodeT *SearchNode(){
/*
* Write the code to search a node in the list.
* if it is found
* return its pointer
* if not found
* return null
*
*/
return NULL; // Very dangerous. Just to remove the compiler error
}
/******************************* DisplayNode() *******************************************/
void DisplayNode(NodeT *Record){
cout << endl;
cout << "Name : " << Record->Name << endl;
cout << "Roll no : " << Record->RollNo << endl;
cout << "Section : " << Record->Section << endl;
cout << "Email : " << Record->Email << endl;
cout << "Telephone #: " << Record->TelNo << endl;
// cout << "Status : " << ; // do it your self
}
/******************************* DisplayCompleteList()***********************************/
void DisplayCompleteList(){
NodeT *Current;
Current = Head;
while(Current!=NULL){
DisplayNode(Current);
Current=Current->Next;
}
if(Current==NULL){
cout << "List is empty" << endl;
}
}
Sorry for the long code. Any help would be appreciated