/*
Implementation of Link List with basic operations such as insert, delete, search and display.
Every node in the Link list contains data about a Railway passenger and search is based on PNR value.
*/
#include<stdio.h>
#include<stdlib.h>
/*Defining Macros*/
#define UNSUCCESSFUL 1
#define SUCCESSFUL 0
/*Defining the Node structure of the link list*/
typedef struct p_details{
int pnr_no;
char p_name[15];
int p_age;
struct p_details* next;
} p_details;
/*Function to Check, Whether an operation is sucessfull or Not*/
void Error_Check(int code)
{
if(code == UNSUCCESSFUL)
{
printf("The operation was unsuccessful!\n");
}
else
printf("The operation was successful!\n");
}
/*Function to display the details of a specific node in the link list */
void Display(p_details* pNode)
{
printf("\tPassenger Information: \n \tPNR no: %d\n \t Passenger Name: %s\n \t Passenger Age: %d\n\n",pNode->pnr_no, pNode->p_name,pNode->p_age);
}
/*Function to Enter details in a Newly created Node*/
void Enter_Node_Details(p_details* pNode)
{
char c;
printf("\n");
printf("Enter PNR No:\n");
scanf("%d",&pNode->pnr_no);
printf("Enter Passenger Name:\n");
scanf("%s",pNode->p_name);
printf("Enter Passenger Age:\n");
scanf("%d",&pNode->p_age);
pNode->next = NULL;
printf("\n");
}
/*Function to display the details of all nodes in the link list */
void Display_All(p_details* pStart)
{
p_details* tmp = pStart;
if(tmp == NULL)
{
printf("List is empty!\n");return;
}
while(1)
{
if(tmp == NULL) break;
Display(tmp);
tmp = tmp->next;
}
}
/*Function to Insert a Node at the beginning of the link list */
int Insert_At_Begining(p_details** pStart)
{
p_details* tmp = NULL;
tmp = (p_details*)malloc(sizeof(p_details));
Enter_Node_Details(tmp);
if(pStart == NULL)
{
*pStart = tmp;
return SUCCESSFUL;
}
else
{
tmp->next = *pStart;
*pStart = tmp;
return SUCCESSFUL;
}
return SUCCESSFUL;
}
/*Function to Insert a Node at the end of the link list */
int Insert_At_End(p_details** pStart)
{
p_details* tmp = NULL, *tmp2 = *pStart;
tmp = (p_details*)malloc(sizeof(p_details));
if(tmp == NULL)
{
return UNSUCCESSFUL;
}
Enter_Node_Details(tmp);
if(*pStart == NULL)
{
*pStart = tmp; return SUCCESSFUL;
}
while(1)
{
if(tmp2->next == NULL) break;
tmp2 = tmp2->next;
}
tmp2->next = tmp;
return SUCCESSFUL;
}
/*Function to Delete a Node from the beginning of the link list */
int Delete_At_Begining(p_details** pStart)
{
p_details* tmp = *(pStart);
if(tmp == NULL)
{
printf("The list is empty!\n");
return UNSUCCESSFUL;
}
*(pStart) = (*pStart)->next;
Display(tmp);
free(tmp);
return SUCCESSFUL;
}
/*Function to Delete a Node from the end of the link list */
int Delete_At_End(p_details** pStart)
{
p_details* last = *pStart;
p_details* last2 = NULL;
if(*pStart == NULL)
{
printf("List is empty!\n");
return UNSUCCESSFUL;
}
if(last->next == NULL)
{
Display(last);
free(last);
*pStart = NULL;
return SUCCESSFUL;
}
while(1)
{
if(last->next == NULL) break;
last2 = last;
last = last->next;
}
last2->next = NULL;
Display(last);
free(last);
return SUCCESSFUL;
}
/*Function to Search a Node with a given key value */
int Search(p_details* pStart,int what)
{
p_details* tmp = pStart;
p_details* node = NULL;
if(pStart == NULL) return UNSUCCESSFUL;
while(1)
{
if(tmp == NULL) break;
if(tmp->pnr_no == what)
{
node = tmp;
break;
}
tmp = tmp->next;
}
if(node != NULL)
{
Display(node);
return SUCCESSFUL;
}
return UNSUCCESSFUL;
}
/*Starting of the Main Function*/
int main()
{
p_details* start = NULL;
int c, err;
int what;
/*Loop to receive instructions from the user*/
while(1)
{
printf("Please enter a choice:\n**************************\n");
printf("1) Insert node at beginning.\n2) Insert node at end.\n3) Delete node from beginning \n4) Delete node from end\n5) Perform search\n6) Display whole list\n7)exit\n");
scanf("%d",&c);
switch(c)
{
case 1:
err = Insert_At_Begining(&start);
Error_Check(err);
break;
case 2:
err = Insert_At_End(&start);
Error_Check(err);
break;
case 3:
err = Delete_At_Begining(&start);
Error_Check(err);
break;
case 4:
err = Delete_At_End(&start);
Error_Check(err);
break;
case 5:
{
printf("Enter the PNR No to search:\n");
scanf("%d",&what);
err = Search(start,what);
Error_Check(err);
}
break;
case 6:
Display_All(start);
break;
case 7:
return 0;
break;
}
}
return 0;
}
/*End of the Program*/
Implementation of Link List with basic operations such as insert, delete, search and display.
Every node in the Link list contains data about a Railway passenger and search is based on PNR value.
*/
#include<stdio.h>
#include<stdlib.h>
/*Defining Macros*/
#define UNSUCCESSFUL 1
#define SUCCESSFUL 0
/*Defining the Node structure of the link list*/
typedef struct p_details{
int pnr_no;
char p_name[15];
int p_age;
struct p_details* next;
} p_details;
/*Function to Check, Whether an operation is sucessfull or Not*/
void Error_Check(int code)
{
if(code == UNSUCCESSFUL)
{
printf("The operation was unsuccessful!\n");
}
else
printf("The operation was successful!\n");
}
/*Function to display the details of a specific node in the link list */
void Display(p_details* pNode)
{
printf("\tPassenger Information: \n \tPNR no: %d\n \t Passenger Name: %s\n \t Passenger Age: %d\n\n",pNode->pnr_no, pNode->p_name,pNode->p_age);
}
/*Function to Enter details in a Newly created Node*/
void Enter_Node_Details(p_details* pNode)
{
char c;
printf("\n");
printf("Enter PNR No:\n");
scanf("%d",&pNode->pnr_no);
printf("Enter Passenger Name:\n");
scanf("%s",pNode->p_name);
printf("Enter Passenger Age:\n");
scanf("%d",&pNode->p_age);
pNode->next = NULL;
printf("\n");
}
/*Function to display the details of all nodes in the link list */
void Display_All(p_details* pStart)
{
p_details* tmp = pStart;
if(tmp == NULL)
{
printf("List is empty!\n");return;
}
while(1)
{
if(tmp == NULL) break;
Display(tmp);
tmp = tmp->next;
}
}
/*Function to Insert a Node at the beginning of the link list */
int Insert_At_Begining(p_details** pStart)
{
p_details* tmp = NULL;
tmp = (p_details*)malloc(sizeof(p_details));
Enter_Node_Details(tmp);
if(pStart == NULL)
{
*pStart = tmp;
return SUCCESSFUL;
}
else
{
tmp->next = *pStart;
*pStart = tmp;
return SUCCESSFUL;
}
return SUCCESSFUL;
}
/*Function to Insert a Node at the end of the link list */
int Insert_At_End(p_details** pStart)
{
p_details* tmp = NULL, *tmp2 = *pStart;
tmp = (p_details*)malloc(sizeof(p_details));
if(tmp == NULL)
{
return UNSUCCESSFUL;
}
Enter_Node_Details(tmp);
if(*pStart == NULL)
{
*pStart = tmp; return SUCCESSFUL;
}
while(1)
{
if(tmp2->next == NULL) break;
tmp2 = tmp2->next;
}
tmp2->next = tmp;
return SUCCESSFUL;
}
/*Function to Delete a Node from the beginning of the link list */
int Delete_At_Begining(p_details** pStart)
{
p_details* tmp = *(pStart);
if(tmp == NULL)
{
printf("The list is empty!\n");
return UNSUCCESSFUL;
}
*(pStart) = (*pStart)->next;
Display(tmp);
free(tmp);
return SUCCESSFUL;
}
/*Function to Delete a Node from the end of the link list */
int Delete_At_End(p_details** pStart)
{
p_details* last = *pStart;
p_details* last2 = NULL;
if(*pStart == NULL)
{
printf("List is empty!\n");
return UNSUCCESSFUL;
}
if(last->next == NULL)
{
Display(last);
free(last);
*pStart = NULL;
return SUCCESSFUL;
}
while(1)
{
if(last->next == NULL) break;
last2 = last;
last = last->next;
}
last2->next = NULL;
Display(last);
free(last);
return SUCCESSFUL;
}
/*Function to Search a Node with a given key value */
int Search(p_details* pStart,int what)
{
p_details* tmp = pStart;
p_details* node = NULL;
if(pStart == NULL) return UNSUCCESSFUL;
while(1)
{
if(tmp == NULL) break;
if(tmp->pnr_no == what)
{
node = tmp;
break;
}
tmp = tmp->next;
}
if(node != NULL)
{
Display(node);
return SUCCESSFUL;
}
return UNSUCCESSFUL;
}
/*Starting of the Main Function*/
int main()
{
p_details* start = NULL;
int c, err;
int what;
/*Loop to receive instructions from the user*/
while(1)
{
printf("Please enter a choice:\n**************************\n");
printf("1) Insert node at beginning.\n2) Insert node at end.\n3) Delete node from beginning \n4) Delete node from end\n5) Perform search\n6) Display whole list\n7)exit\n");
scanf("%d",&c);
switch(c)
{
case 1:
err = Insert_At_Begining(&start);
Error_Check(err);
break;
case 2:
err = Insert_At_End(&start);
Error_Check(err);
break;
case 3:
err = Delete_At_Begining(&start);
Error_Check(err);
break;
case 4:
err = Delete_At_End(&start);
Error_Check(err);
break;
case 5:
{
printf("Enter the PNR No to search:\n");
scanf("%d",&what);
err = Search(start,what);
Error_Check(err);
}
break;
case 6:
Display_All(start);
break;
case 7:
return 0;
break;
}
}
return 0;
}
/*End of the Program*/
No comments:
Post a Comment