Hello I am just trying to get a little nudge in the right direction I cant make
ID: 3656436 • Letter: H
Question
Hello I am just trying to get a little nudge in the right direction
I cant make it into the lab due to work.
/*
Complete insertStudents and deleteStudents functions in the skeleton program.
The output of the program should be like:
sid=001,name=John
sid=002,name=Joan
sid=003,name=Jack
sid=001,name=John
sid=003,name=Jack
*/
#include "ex6.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
Snodeptr students;
//Create the dummy node
makenullStudents(&students);
struct studentType student1,student2,student3;
strcpy(student1.sid,"001");
strcpy(student1.sname,"John");
strcpy(student2.sid,"002");
strcpy(student2.sname,"Joan");
strcpy(student3.sid,"003");
strcpy(student3.sname,"Jack");
insertStudents(student1,students);
insertStudents(student2,students);
insertStudents(student3,students);
printStudents(students);
deleteStudents(student2,students);
printStudents(students);
return 0;
}
Snodeptr sAlloc(void) {
return (Snodeptr) malloc(sizeof(struct snode));
}
void makenullStudents(Snodeptr *students) {
(*students)=sAlloc();
}
void insertStudents(struct studentType x, Snodeptr students) { --- I am tryingto understand what ineed to put into step make it work---can you provide some helpful hints--what i have in here is not working
char line[80];
printf("Enter Student Id: ");
getline(line,sizeof(line));
scanf(line,"%s",x.sid);
printf("Type in Student Name: ");
getline(line,sizeof(line));
scanf(line,"%s",x.sname);
if (insertStudents(x,students) == 0)
printf("Student s has been inserted", x.sid);
else
printf("Student s already exists", x.sid);
}
void deleteStudents(struct studentType x, Snodeptr students) { --- I am trying to understand what i need to put into step make it work---can you provide some helpful hints----what i have in here is not working
char line[80];
printf("Enter sid to be deleted: ");
getline(line,sizeof(line));
sscanf(line,"%s",x.sid);
if (deleteStudents(x,students,courses) == 0)
printf("Student s has been deleted", x.sid);
else
printf("Student s does not exist", x.sid);
}
void printStudents(Snodeptr students) {
int count=0;
Snodeptr p=students;
char ch;
while((p->next)!=NULL){
printf("sid=%s,name=%s ",p->next->student.sid,p->next->student.sname);
p=p->next;
}
}
Explanation / Answer
#include #include struct node { int data; struct node *next; }*head; void append(int num) { struct node *temp,*right; temp= (struct node *)malloc(sizeof(struct node)); temp->data=num; right=(struct node *)head; while(right->next != NULL) right=right->next; right->next =temp; right=temp; right->next=NULL; } void add( int num ) { struct node *temp; temp=(struct node *)malloc(sizeof(struct node)); temp->data=num; if (head== NULL) { head=temp; head->next=NULL; } else { temp->next=head; head=temp; } } void addafter(int num, int loc) { int i; struct node *temp,*left,*right; right=head; for(i=1;inext; } temp=(struct node *)malloc(sizeof(struct node)); temp->data=num; left->next=temp; left=temp; left->next=right; return; } void insert(int num) { int c=0; struct node *temp; temp=head; if(temp==NULL) { add(num); } else { while(temp!=NULL) { if(temp->datanext; } if(c==0) add(num); else if(cdata==num) { if(temp==head) { head=temp->next; free(temp); return 1; } else { prev->next=temp->next; free(temp); return 1; } } else { prev=temp; temp= temp->next; } } return 0; } void display(struct node *r) { r=head; if(r==NULL) { return; } while(r!=NULL) { printf("%d ",r->data); r=r->next; } printf(" "); } int count() { struct node *n; int c=0; n=head; while(n!=NULL) { n=n->next; c++; } return c; } int main() { int i,num; struct node *n; head=NULL; while(1) { printf(" List Operations "); printf("=============== "); printf("1.Insert "); printf("2.Display "); printf("3.Size "); printf("4.Delete "); printf("5.Exit "); printf("Enter your choice : "); if(scanf("%d",&i)Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.