Beginner here!
I want to add one more function to my program, the peek operation in the stack, but I don't know how to do it. When the user chooses the third option which is the peek operation. I want the program to print the top id number, name, and course.
The output I want,
The topmost ID number is:
The topmost Name is:
The topmost Course is:
Here is my code for other functions.
#include<stdio.h>
#include<malloc.h>
typedef struct info
{
char name[20],courses[50];
int idnum;
struct info *next;
};
struct info *push(struct info *);
struct info *pop(struct info *);
struct info *peek(struct info *);
void display(struct info *);
void printline();
int main()
{
struct info *top=NULL;
int ch;
printline();
printf("\t STUDENT MANGAMENT SYSTEM\n");
printline();
printf(" [1] - PUSH");
printf("\n [2] - POP");
printf("\n [3] - PEEK");
printf("\n [4] - DISPLAY");
printf("\n [0] - EXIT\n");
printline();
do
{
printf("Enter your choice: ");
scanf("%d",&ch);
printline();
switch(ch)
{
case 1:
top=push(top);
break;
case 2:
top=pop(top);
break;
case 3:
break;
case 4:
display(top);
case 0:
printline();
printf("Thank you \nHave a Nice Day!\n");
break;
}
}while(ch!=0);
}
struct info *push(struct info *top)
{
struct info *p;
p=(struct info *)malloc(sizeof(struct info));
if(p==NULL)
{
p -> next = NULL;
top = p;
}
else
{
printf("Enter the Student name: ");
scanf("%s",&p->name);
printf("Enter Student course: ");
scanf("%s",&p->courses);
printf("Enter the ID number of Student: ");
scanf("%d",&p->idnum);
p->next=top;
top=p;
}
return(top);
}
struct info *pop(struct info *top)
{
struct info *p;
if(top==NULL)
{
printf("nothing to pop");
}
else
{
printf("\nThe Student name is: %s",top->name);
printf("\nThe Student course is: %s",top->courses);
printf("\nThe ID Number of the student is: %d",top->idnum);
top=top->next;
}
return(top);
}
struct info *peek(struct info *top){
}
void display(struct info *top)
{
if(top==NULL)
{
printf("NOTHING TO DISPLAY\n");
printline();
}
else
{
while(top!=NULL)
{
printline();
printf("\t STUDENT MANGAMENT SYSTEM\n");
printline();
printf("\nThe student name is: %s",top->name);
printf("\nThe student address is: %s",top->courses);
printf("\nThe marks of the student is: %d \n",top->idnum);
printline();
top=top->next;
}
}
}
void printline(){
int i;
for(i=0; i<50; i++)
printf("-");
printf("\n");
}
Copy the code in pop function but remove the top=top->next;
Related
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
struct Node
{
char firstname[100];
char lastname[100];
char number[100];
char mail[100];
struct Node *next;
}*head;
void insert(char* firstname,char* lastname,char* number,char* mail)
{
struct Node * node=(struct Node *)malloc(sizeof(struct Node));
strcpy(node->firstname, firstname);
strcpy(node->lastname, lastname);
strcpy(node->number, number);
strcpy(node->mail, mail);
node->next=NULL;
if(head==NULL)
{
head=node;
}
else{
node->next=head;
head=node;
}
}
void search(char* firstname)
{
struct Node * temp = head;
while(temp!=NULL){
if(temp->firstname==firstname){
printf("Contact Found");
printf("Firstname:%s\n",temp->firstname);
printf("Lastname:%s\n",temp->lastname);
printf("PhoneNumber:%s\n",temp->number);
printf("Mail Id:%s\n",temp->mail);
return;
}
temp = temp->next;
}
printf("%s is not found in the contact \n",firstname);
}
void update(char* firstname)
{
struct Node * temp=head;
while(temp!=NULL){
if(temp->firstname==firstname){
printf("Contact Found");
printf("Enter the new Phone number for %s\n",temp->firstname);
scanf("%s",temp->number);
printf("Contact Updated Successfully\n");
return;
}
temp=temp->next;
}
printf("%s is not found in the contact \n",firstname);
}
void delete(char* firstname)
{
struct Node * temp1 = head;
struct Node * temp2 = head;
while(temp1!=NULL){
if(temp1->firstname==firstname){
printf("Contact Found for deleting\n");
if(temp1==temp2){
head = head->next;
free(temp1);
}
else{
temp2->next = temp1->next;
free(temp1);
}
printf("Contact deleted Successfully\n");
return;
}
temp2=temp1;
temp1=temp1->next;
}
printf("%s is not found in the contact \n",firstname);
}
void display()
{
struct Node * temp=head;
while(temp!=NULL){
printf("Firstname:%s\n",temp->firstname);
printf("Lastname:%s\n",temp->lastname);
printf("PhoneNumber:%s\n",temp->number);
printf("Mail Id:%s\n",temp->mail);
temp = temp->next;
}
}
int main()
{
head = NULL;
int choice;
char firstname[100];
char lastname[100];
char number[100];
char mail[100];
printf("-------Welcome--------\n ");
printf("1.Insert a Contact\n2.Search a Contact\n3.Delete a Contact\n4.Update a Contact\n5.Display all the Contacts");
do
{
printf("\nEnter Choice: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter Firstname:");
scanf("%s",firstname);
printf("Enter Lastname:");
scanf("%s",lastname);
printf("Enter PhoneNumber:");
scanf("%s",number);
printf("Enter Mail Id:");
scanf("%s",mail);
insert(firstname,lastname,number,mail);
break;
case 2:
printf("Enter Firstname to Search:");
scanf("%s",firstname);
search(firstname);
break;
case 3:
printf("Enter Firstname to Delete:");
scanf("%s",firstname);
delete(firstname);
break;
case 4:
printf("Enter Firstname to Update:");
scanf("%s",firstname);
update(firstname);
break;
case 5:
display();
break;
}
}while (choice != 0);
}
this c program runs without error but search,delete,update funtion is not working...you can refer the img for more details.
tommorrow i have to submit my mini project..so if anyone knows c program please help me
Enter Choice: 2
Enter Firstname to Search:durai
durai is not found in the contact
Enter Choice: 3
Enter Firstname to Delete:durai
durai is not found in the contact
Enter Choice: 4
Enter Firstname to Update:durai
durai is not found in the contact
these are the errors which i'm getting
For example within the function search you are trying to compare two pointers that point to different extents of memory
if(temp->firstname==firstname){
So the condition evaluates to false even if the pointed strings are equal each other. You have to use the standard string function strcmp
For example
if( strcmp( temp->firstname, firstname ) == 0 ){
Pay attention to that all function parameters that have the type char * should be declared as having the type const char *.
Also it is a bad idea when the functions depend on the global variable head. For example in this case you can not create more than one list in a program.
I'm writing this code to store data records of students' first name, last name, scores and zip codes. I have almost everything done but my Print() function doesn't print the first element of node 2(first name of 2nd student) and turns into an infinite loop when I input ore than 2 nodes, what am i doing wrong?
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
void Insert(char first[20], char last[20], float data, char zip[50]);
void delete(int e);
void Print();
double median();
struct node
{
char first_name[20];
char last_name[20];
float score;
char zip_code[50];
struct node *ptr;
};
int n;
struct node* head =NULL;
struct node* tail=NULL ;
void Insert(char first[20], char last[20], float data, char zip[50])
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp->ptr=NULL;
strcpy(temp->first_name,first);
strcpy(temp->last_name,last);
temp->score=data;
strcpy(temp->zip_code,zip);
if(head==NULL)
{
head=temp;
tail=temp;
return;
}
tail->ptr=temp;
tail=temp;
free(temp);
temp=NULL;
}
void delete(int e)
{
int i;
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp=head;
if(e==1)
{
head=temp->ptr;
free(temp);
return;
}
else if(e==n)
{
while(temp->ptr->ptr!=NULL)
{
temp=temp->ptr;
}
temp->ptr=NULL;
free(temp->ptr);
return;
}
for(i=0; i<(e-2); ++i)
temp=temp->ptr;
struct node *temp1=temp->ptr;
temp->ptr=temp1->ptr;
free(temp1);
}
void Print()
{
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp=head;
printf("Data entered is below: \n");
while(temp!=NULL)
{
printf("First Name: %s, Last Name: %s, Score: %.2f, Zip Code: %s",temp->first_name,temp->last_name,temp->score,temp->zip_code);
temp=temp->ptr;
printf("\n");
}
printf("\n\n\n");
}
double median()
{
double median,tmp;;
double *ex=(double*)malloc(sizeof(double));/*head*/
double *exe=(double*)malloc(sizeof(double));/*dynamic*/
ex=exe;
int i=1,term,j;
struct node *temp=(struct node*)malloc(sizeof(struct node));
temp=head;
while(i<=n)
{
temp->ptr;
*exe=temp->score;
exe++;
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if( *(ex+i) > *(ex+j))
{
tmp = *(ex+i);
*(ex+i) = *(ex+j);
*(ex+j) = tmp;
}
}
}
if(n%2==0)
{
/*even;median=n/2-1*/
term=(n/2)-1;
median= (*(ex+(term-1)));
return median;
}
/*odd; median=n-1/2*/
term=(n-1)/2;
median= (*(ex+(term-1)));
return median;
}
int main()
{
char name1[20],name2[20], code[50];
float x;
int i,option,index;
printf("Enter the number of nodes: ");
scanf("%d",&n);
printf("Please enter the records of students in the following format(click enter for new students)\n");
printf("First_Name Last_Name Score Zip_Code\n");
for(i=1; i<=n; ++i)
{
scanf(" %s",name1);
scanf(" %s",name2);
scanf(" %f",&x);
scanf(" %s",code);
Insert(name1,name2,x,code);
}
printf("\n");
while(1)
{
printf("Choose one of the following options: \n");
printf("Print records (press 1)\nAdd a new record (press 2)\nDelete record(s) (press 3)\nSearch by zip code (press 4)\nSearch by score range (press 5)\nFind median score (press 6)\nExit the program (press 0)\n");
scanf("%d",&option);
switch(option)
{
case 0:
{
/*Exit Program*/
exit(0);
break;
}
case 1:
{
/*print*/
Print();
break;
}
case 2:
{
/*insert*/
getchar();
printf("Enter the new record in the following format: \nFirst_Name Last_Name Score Zip_Code\n");
scanf("%s",name1);
scanf("%s",name2);
scanf("%f",&x);
scanf("%s",code);
getchar();
Insert(name1,name2,x,code);
break;
}
case 3:
{
/*delete*/
printf("Enter the node/record to be deleted: ");
scanf("%d",&index);
delete(index);
printf("The deletion of record %d has been succesfully completed!\n\n",index);
break;
}
case 4:
{
/*search by zip*/
break;
}
case 5:
{
/*search by score*/
break;
}
case 6:
{
/*find median*/
printf("Median score for the entered records is: %f",median());
break;
}
}/*switch*/
}/*while*/
return 0;
}
The Insert function is holding a reference to freed data:
tail=temp;
free(temp);
Attempting to use the storage referenced by tail after it's been freed is an error.
This program is supposed to manipulate a student list. Every time I try to add more than one student I get a segmentation fault error. Also, when I try to print the list recursively I get a segmentation fault error. I think it has to do with how I am saving to the structure and/or calling it. I am very new to programming so I'm sure it is something simple. Any ideas?
//Header file declarations.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Structure defintion.
struct student {
int ID;
char name[40];
struct student *next;
};
//Type definition.
typedef struct student Student;
//Function prototypes.
int getChoice();
Student *addToList(Student *List);
void printList(Student *List);
void printListRR(Student *List);
void searchList(Student *List);
/*main function
Objective: This function provides runs a function call based on an option selected the user in another function.
Input: This function recieves no input from the user directly but it is passed their menu selection.
Output: The function outputs error messages and a closing salutation to the user. It returns 0.
*/
int main(void) {
int choice = 0;
Student *SLIST = NULL;
//Call getChoice to get user's selection
choice = getChoice();
//Switch-case for the possible menu selections
while(choice >= 0) {
switch(choice) {
case 0 : printf("Bye...\n"); exit(0);
case 1 : SLIST = addToList(SLIST); break;
case 2 : printList(SLIST); break;
case 3 : printListRR(SLIST); break;
case 4 : searchList(SLIST); break;
default: printf("That is not a valid choice\n");
}
choice = getChoice();
}
if(SLIST) free(SLIST);
return 0;
}
int getChoice() {
int choice = 0;
printf("\n****** MENU ******\n");
printf("1. Add new student to list.\n");
printf("2. Print the student list, beginning to end.\n");
printf("3. Recursively print the student list from the end.\n");
printf("4. Search the list for a student.\n");
printf("0. Quit.\n");
printf("\nEnter your choice: ");
scanf("%d", &choice);
return choice;
}
Student *addToList(Student *List){
Student *studentPtr = (Student *) malloc(sizeof(Student));
printf("Student ID: ");
scanf("%d", &(studentPtr->ID));
printf("Student Name: ");
scanf(" %[^\n]", studentPtr->name);
if(List == NULL){
return studentPtr;
}
Student *nextStudent = List;
while (nextStudent->next != NULL){
nextStudent = nextStudent->next;
}
nextStudent->next = studentPtr;
return List;
}
void printList(Student *List){
while(List != NULL){
printf("%d %s\n", List->ID, List->name);
List = List->next;
}
}
void printListRR(Student *List){
if(List == NULL){
return;
}
printListRR(List->next);
}
void searchList(Student *List){
int idSearch;
printf("Enter student ID to search for: ");
scanf("%d", &idSearch);
while(List != NULL){
if(List->ID == idSearch){
printf("%d %s\n", List->ID, List->name);
return;
}
List = List->next;
}
printf("ID %d not found", idSearch);
}
Try initialising studentPtr->next to NULL in addToList()?
In my program,I plan to filter choices for the users to get a specific piece of data for the inventory.
For instance,at start of the program, there will be menu that gives user option to add or delete data and then for instance if they say insert then I will give them option to select a car company(i.e. like Toyota or Honda) and once they select that company I will give them choice of all Toyota models they can add in the inventory.This will happen so I can narrow down and do a selected operation on the information I have been given.
The thing is I don't know how to filter out the models for a specific company.I have created different arrays containing different models but I don't know how to display user option of models for that company.
Here is my code..
#include <stdio.h>
#include <stdlib.h>
#define MAX_WORD_LENGTH 20
#define MAX_SIZE 100
typedef struct cardata{
char carname[MAX_WORD_LENGTH];
char carmodel[MAX_WORD_LENGTH];
char caryear[MAX_WORD_LENGTH];
char cartype[MAX_WORD_LENGTH];
int quantity;
}CarData;
struct node{
CarData data;
struct node *next;
struct node *prev;
}*start=NULL;
const char *companyList[10] = {"Toyota", "Honda","Hyundai","Nissan","Mitsubishi","VoksWagon","Acura","Ford","Dodge","GMC"};
const char *companyModels[10] = {toyotaModels,hondamodels,hyundaimodels,nissanmodels,mitsubishimodels,vokswagonmodels,acuramodels,fordmodels,dodgemodels,gmcmodels};
const char *toyotaModels[10]={"Corolla","Camery"}
const char *hondaModels[10]={"Civic","Accord"};
void insert_first(){
struct node *ptr;
char carname[MAX_WORD_LENGTH];
char carmodel[MAX_WORD_LENGTH];
char caryear[MAX_WORD_LENGTH];
char cartype[MAX_WORD_LENGTH];
int carQuantity;
int ch;
printf("\n\n\n1.Toyota \n2.Honda \n3.Hyundai \n4.Nissan \n5.Mitsubishi \n6.Volksvagon \n7.Acura \n8.Ford \n9.Dodge \n10.GMC\n");
printf("\nPress a number to select corresponding car(i.e. 1 for toyota, 2 for honda): ");
scanf("%d", &ch);
strcpy(carname,companyList[ch-1]);
printf("\n\nEnter the car model: ");
scanf("%s", carmodel);
printf("\n\nEnter the car year: ");
scanf("%s", caryear);
printf("\n\nEnter the car type: ");
scanf("%s", cartype);
printf("\n\nEnter the quantity of models: ");
scanf("%d", &carQuantity);
if(start==NULL){
start=(struct node *)malloc(sizeof(struct node));
strcpy(start->data.carname,carname);
strcpy(start->data.carmodel,carmodel);
strcpy(start->data.caryear,caryear);
strcpy(start->data.cartype,cartype);
start->data.quantity=carQuantity;
start->prev=NULL;
start->next=NULL;
}else{
ptr=start;
start=(struct node *)malloc(sizeof(struct node));
strcpy(start->data.carname,carname);
strcpy(start->data.carmodel,carmodel);
strcpy(start->data.caryear,caryear);
strcpy(start->data.cartype,cartype);
start->data.quantity=carQuantity;
start->next=ptr;
}
}
void delete_first(){
struct node *ptr;
char carname[MAX_WORD_LENGTH];
char carmodel[MAX_WORD_LENGTH];
char caryear[MAX_WORD_LENGTH];
char cartype[MAX_WORD_LENGTH];
char modelNumber[MAX_WORD_LENGTH];
int carQuantity;
if(start==NULL){
printf("\n\nLinked list is empty.\n");
}else{
ptr=start;
printf("\nThe car for which the entry is removed is %s \n",ptr->data.carname);
strcpy(start->data.carname,carname);
strcpy(start->data.carmodel,carmodel);
strcpy(start->data.caryear,caryear);
strcpy(start->data.cartype,cartype);
start->data.quantity=carQuantity;
start=start->next;
free(ptr);
}
}
void display()
{
struct node *ptr=start;
int i=1;
if(ptr == NULL){
printf("\nLinklist is empty.\n");
}else{
printf("\nSr. No Make Model Year Type Quantity\n");
while(ptr != NULL){
printf("\n%d.\t%s %s %s %s %d\n", i,ptr->data.carname,ptr->data.carmodel,ptr->data.caryear,ptr->data.cartype,ptr->data.quantity);
ptr = ptr->next;
i++;
}
}
}
int main(void)
{
int ch;
do
{
printf("\n\n\n1. Insert \n2. Delete \n3. Display \n4. Exit\n");
printf("\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert_first();
break;
case 2:
delete_first();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\n\nInvalid choice. Please try again. \n");
}
} while(1);
return 0;
}
OP: "don't know how to display user option of models."
You need to dynamically form the question from the selected car company. Suggest not using fixed list sizes (drop the 10) and append a NULL to indicated end of list. Improve error handling by checking scanf result and argument range.
const char **CompanyModels[] = { toyotaModels, hondaModels, 0 /* List is 2 long */};
int ModelPrompt(const char **CompanyModels) {
printf("\n\n\n");
int i;
for (i=0; CompanyModels[i], i++) {
printf("%d.%s \n", d+1, CompanyModels[i]);
}
return i;
}
void insert_first(){
...
// recommend replacing next 2 lines with a CompanyPrompt(companyList);
printf("\n\n\n1.Toyota \n2.Honda \n3.Hyundai \n4.Nissan \n5.Mitsubishi \n6.Volksvagon \n7.Acura \n8.Ford \n9.Dodge \n10.GMC\n");
printf("\nPress a number to select corresponding car(i.e. 1 for toyota, 2 for honda): ");
scanf("%d", &ch);
strcpy(carname,companyList[ch-1]);
int MaxModelIndex = ModelPrompt(CompanyModels[ch]);
int carmodelIndex = -1;
if ((1 != scanf("%d", carmodelIndex)) || (carmodelIndex < 0) || (carmodelIndex > MaxModelIndex)) {
; // handle error
}
strcpy(carmodel, CompanyModels[ch][carmodelIndex]);
// continue with similar code for cartype.
This is the code I have written for singly linked list recently. How can I convert it to doubly linked list? Is this even possible? I just started doing linked list recently, but what our lecturer taught is not clear enough.
int delnum,id;
#define CLR system("cls")
#define INFO_SIZE 50
void view();
struct info
{
int cusnum;
char cusname[INFO_SIZE];
char cusdes[INFO_SIZE];
struct info *next;
struct info *prev;
}*addcase,*temp,*tempdisplay, *start,*last;
void menu()
{
void addToStart();
void addToEnd();
void printList();
void removeNodeAt();
int choice;
bool valid = true;
CLR;
printf("\n\t Basic Linked list menu");
printf("\n\t 1.Add a new node to the end");
printf("\n\t 2.Add a new node to the beginning");
printf("\n\t 3.Print out the entire list");
printf("\n\t 4.Remove a node from the list");
printf("\n\t 5.Back to main menu");
printf("\n\t 6.Quit the program\n\n");
do
{
printf ("\n\t Enter your choice: ");
scanf("%d",&choice);fflush (stdin);
switch (choice)
{
case 1:
addToEnd();
break;
case 2:
addToStart();
break;
case 3:
printList ();
break;
case 4:
removeNodeAt ();
break;
case 5:
main();
break;
case 6:
exit(0);
default :
printf("\n\t Invalid Input \n\n");
valid = false;
break;
}
}while(!valid);
}
void addToStart()
{
addcase=(struct info*) malloc(sizeof(struct info));
char buffer[INFO_SIZE];
bool isInt(char[]);
bool isValidName(char[]);
CLR;
printf("\n\tAdd from begining\n");
printf("\n\t What is your name (use symbol to represent spacing):\n\t ");
gets(buffer);fflush(stdin);
while(!isValidName(buffer))
{
printf("\n\t Invalid Input \n");
printf("\n\t What is your name (use symbol to represent spacing):\n\t ");
gets(buffer);fflush(stdin);
}
sscanf(buffer,"%s", &addcase->cusname);
printf("\n\t What is your customer number: ");
gets(buffer);fflush(stdin);
while(!isInt(buffer))
{
printf("\n\t Invalid Input \n");
printf("\n\t What is your customer number: ");
gets(buffer);fflush(stdin);
}
sscanf(buffer," %d",&addcase->cusnum);
printf("\n\t What is your description: ");
gets(buffer);fflush(stdin);
while(!isValidName(buffer))
{
printf("\n\t Invalid Input\n");
printf("\n\t What is your description: ");
gets(buffer);fflush(stdin);
}
sscanf(buffer,"%s", &addcase->cusdes);
addcase->next= NULL;
if(start== NULL)
{
start=addcase;
}
else
{
addcase->next =start;
start= addcase;
}
printf("\n\n\tRecord was successfully saved !!!\n");
printf("\n\t");
system("pause");
menu();
}
void addToEnd()
{
addcase =(struct info*) malloc(sizeof(struct info));
char buffer[INFO_SIZE];
bool isInt(char[]);
bool isValidName(char[]);
CLR;
printf("\n\tAdd from end\n");
printf("\n\t What is your name (use symbol to represent spacing):\n\t ");
gets(buffer);fflush(stdin);
while(!isValidName(buffer))
{
printf("\n\t Invalid Input\n");
printf("\n\t What is your name (use symbol to represent spacing):\n\t ");
gets(buffer);fflush(stdin);
}
sscanf(buffer,"%s",&addcase->cusname);
printf("\n\t What is your customer number: ");
gets(buffer);fflush(stdin);
while(!isInt(buffer))
{
printf("\n\t Invalid Input \n");
printf("\n\t What is your customer number: ");
gets(buffer);fflush(stdin);
}
sscanf(buffer," %d",&addcase->cusnum);
printf("\n\t What is your description: ");
gets(buffer);fflush(stdin);
while(!isValidName(buffer))
{
printf("\n\t Invalid input \n");
printf("\n\t What is your description: ");
gets(buffer);fflush(stdin);
}
sscanf(buffer,"%s",&addcase->cusdes);
addcase->next=NULL;
if(start== NULL){
start=addcase;
}else {
temp=start;
while(temp->next!= NULL){
temp= temp->next;
}
temp->next=addcase;
}
printf("\n\n\tRecord was successfully saved !!!\n");
printf("\n\t");
system("pause");
menu();
}
void printList()
{
CLR;
printf("\n\tRecord List");
if(start==NULL)
printf("\n\n\t No record available");
else
{
tempdisplay=start;
while (tempdisplay != NULL)
{
printf("\n\n\t %d \t%s \t%s\n",tempdisplay->cusnum,tempdisplay->cusname,tempdisplay->cusdes);
tempdisplay=tempdisplay->next;
}
}printf("\n\t");
system("pause");
menu();
}
void removeNodeAt()
{
char buffer[INFO_SIZE];
bool isInt(char[]);
CLR;
if(start==NULL)
printf("\n\n\t No record available to remove"),
printf("\n\n\t"),
system("pause"),
menu();
else
{
tempdisplay=start;
printf("\n\tDeletation\n");
printf("\n\t Number\t Name\t Desscription\n");
while (tempdisplay != NULL)
{
printf("\n\t %d\t%s\t\t%s",tempdisplay->cusnum,tempdisplay->cusname,tempdisplay->cusdes);
tempdisplay=tempdisplay->next;
}
}
printf("\n\n\t Input specific Customer Number to delete:");
gets(buffer);fflush(stdin);
while(!isInt(buffer))
{
printf("\n\t Invalid Input \n");
printf("\n\n\t Input specific Customer Number to delete:");
gets(buffer);fflush(stdin);
}
sscanf(buffer," %d",&delnum);
if(delnum==start->cusnum)
start=start->next;
else if(&delnum!=&temp->cusnum)
{
printf("\n\t No record specific to this number\n");
Sleep(1000);
removeNodeAt();
}
else{
temp=start;
last->next=last->next->next;
}
printf("\n\n\t Record was successfully deleted !!!\n");
printf("\n\t");
system("pause");
menu();
}
I had written this program for entering student information in the doubly linked list long back . Refer the below program and edit your program on the same lines . Your program just creates a singly linked list and provides options to add , remove elements and print elements from the list.
#include<stdio.h>
#include<malloc.h>
#include<string.h>
struct student
{
char name[10];
int m1;
int m2;
struct student *llink;
struct student *rlink;
};
struct student * addelement(struct student *,char name[],int,int,int,int);
void display( struct student *,char name[]);
int main()
{
int num,i,m1,m2;
struct student *head;
head=NULL;
char name[10];
printf("\n Enter the number of students data to be entered :");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
printf("Enter the name of the student :");
scanf("%s",name);
printf("\n Enter the marks in maths :");
scanf("%d",&m1);
printf("\n Enter the marks in science : ");
scanf("%d",&m2);
head=addelement(head,name,m1,m2,i,num);
}
printf("\n Enter the student name to be searched : ");
scanf("%s",name);
display(head,name);
}
struct student * addelement(struct student *head,char name[10],int m1,int m2,int i,int num){
struct student *newnode;
newnode=(struct student *)malloc(sizeof(struct student));
strcpy(newnode->name,name);
newnode->m1=m1;
newnode->m2=m2;
if(i==num)
{
newnode->llink=NULL;
newnode->rlink=head;
head->llink=newnode;
return newnode;
}
else if(head==NULL)
{
newnode->rlink=NULL;
newnode->llink=NULL;
return newnode;
}
else
{
head->llink=newnode;
newnode->rlink=head;
return newnode;
}
}
void display(struct student *p3,char name[10])
{
struct student *temp;
temp=p3;
int flag=0;
if(p3==NULL)
{
printf("\n The list is empty \n");
}
while(p3!=NULL)
{
printf("\n Student Name: %s, Maths Marks: %d,Science Marks: %d",p3->name,p3->m1,p3->m2);
if(p3->m1>=90 && p3->m2>=90)
{
printf("\n Student Name: %s, Maths Marks: %d,Science Marks: %d",p3->name,p3->m1,p3->m2);
p3=p3->rlink;
}
else
{
p3=p3->rlink;
}
}
p3=temp;
while(p3!=NULL)
{
if(strcmp(p3->name,name)==0)
{
flag=1;
break;
}
else
{
p3=p3->rlink;
}
}
if(flag==1)
{
printf("\n The student information exists in the database \n ");
}
else
{
printf("\n The student information does not exist in the database \n");
}
}