#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
//Define a structure
struct Student{
char name[20];
int number;
struct Student *next;
};
int main() {
//Function declaration
struct Student * createLink();
void printLink(struct Student * pHead);
struct Student * insertLink(struct Student * pHead);
void deleteLink(struct Student * pHead);
void menu(struct Student * pHead);
//Define a pointer variable that points to the structure, to change the head node of the structure
struct Student * pHead=NULL;
menu(pHead);
}
void menu(struct Student * pHead){
int i;
printf("1. Create a linked list 2. Add a node to the linked list 3. Delete the node of the linked list 4. Print the linked list\n");
printf("Please enter the operation command:");
scanf("%d",&i);
switch (i){
case 1:
pHead=createLink();
printf("Operation succeeded\n");
menu(pHead);
break;
case 2:
pHead=insertLink(pHead);
printf("Operation succeeded\n");
menu(pHead);
break;
case 3:
deleteLink(pHead);
printf("The operation succeeded\n");
menu(pHead);
break;
case 4:
printLink(pHead);
printf("The operation succeeded\n");
menu (pHead);
break;
default:
printf("The input parameter is invalid!\n");
menu(pHead);
break;
}
}
//Create a linked list
struct Student * createLink(){
//Define a pointer variable to the structure
struct Student * pHead=NULL;
//Define two pointers to the structure
struct Student *pOld,*pNew;
//Allocate a piece of memory, then let the two pointers point to it
pHead=pNew=pOld=(struct Student*)malloc(sizeof (struct Student));
printf("Please enter a student's name:");
scanf("%s",&pNew->name);
printf("Please enter a student's student number:");
scanf("% d",&pNew->number);
while(pNew->number!=0){
pOld=pNew; // Point the old pointer to the new pointer
pNew=(struct Student*)malloc(sizeof(struct Student));
printf("Please enter a student's name:");
scanf("%s",&pNew->name);
printf("Please enter a student's name Student number: ");
scanf("%d",&pNew->number);
pOld->next=pNew;
}
pOld->next=NULL;
free(pNew);
return pHead;
}
//Print the elements in the linked list
void printLink(struct Student * pHead){
while(1){
if(pHead!=NULL){
printf("The student's name is: %s, the student's student number is %d\n",pHead->name,pHead ->number);
pHead=pHead->next; //Point the pointer to the next node
}else{
break;
}
}
}
//Insert data into the linked list, pHead represents the head node of the linked list to be inserted
struct Student * insertLink(struct Student * pHead){
//Create a new node
struct Student * pNew,*pTemp;
pNew=(struct Student*) malloc(sizeof(struct Student));
printf("Please enter name:");
scanf("%s",&pNew->name);
printf("Please enter student number:");
scanf("%s", &pNew->number);
pTemp=pHead->next; //The address of the next node of the original pHead node
pHead->next=pNew;
pNew->next=pTemp;
return pHead;
}
//Delete the linked list, pHead is the head node pointer, index is the node to be deleted
void deleteLink(struct Student * pHead){
int index;
int i;
struct Student * pTemp,*pTemp2,*pTemp3;
printf("Please enter the index number of the node to be deleted:");
scanf("%d",&index);
pTemp2=pHead;
//delete the second node
for(i=0;i<index-1;i++ ){
pTemp3=pTemp2;
pTemp=pTemp2->next; //pointer to the second node
pTemp2=pTemp;
}
pTemp3->next=pTemp->next;
free(pTemp);
}
#include<stdlib.h>
#include<conio.h>
//Define a structure
struct Student{
char name[20];
int number;
struct Student *next;
};
int main() {
//Function declaration
struct Student * createLink();
void printLink(struct Student * pHead);
struct Student * insertLink(struct Student * pHead);
void deleteLink(struct Student * pHead);
void menu(struct Student * pHead);
//Define a pointer variable that points to the structure, to change the head node of the structure
struct Student * pHead=NULL;
menu(pHead);
}
void menu(struct Student * pHead){
int i;
printf("1. Create a linked list 2. Add a node to the linked list 3. Delete the node of the linked list 4. Print the linked list\n");
printf("Please enter the operation command:");
scanf("%d",&i);
switch (i){
case 1:
pHead=createLink();
printf("Operation succeeded\n");
menu(pHead);
break;
case 2:
pHead=insertLink(pHead);
printf("Operation succeeded\n");
menu(pHead);
break;
case 3:
deleteLink(pHead);
printf("The operation succeeded\n");
menu(pHead);
break;
case 4:
printLink(pHead);
printf("The operation succeeded\n");
menu (pHead);
break;
default:
printf("The input parameter is invalid!\n");
menu(pHead);
break;
}
}
//Create a linked list
struct Student * createLink(){
//Define a pointer variable to the structure
struct Student * pHead=NULL;
//Define two pointers to the structure
struct Student *pOld,*pNew;
//Allocate a piece of memory, then let the two pointers point to it
pHead=pNew=pOld=(struct Student*)malloc(sizeof (struct Student));
printf("Please enter a student's name:");
scanf("%s",&pNew->name);
printf("Please enter a student's student number:");
scanf("% d",&pNew->number);
while(pNew->number!=0){
pOld=pNew; // Point the old pointer to the new pointer
pNew=(struct Student*)malloc(sizeof(struct Student));
printf("Please enter a student's name:");
scanf("%s",&pNew->name);
printf("Please enter a student's name Student number: ");
scanf("%d",&pNew->number);
pOld->next=pNew;
}
pOld->next=NULL;
free(pNew);
return pHead;
}
//Print the elements in the linked list
void printLink(struct Student * pHead){
while(1){
if(pHead!=NULL){
printf("The student's name is: %s, the student's student number is %d\n",pHead->name,pHead ->number);
pHead=pHead->next; //Point the pointer to the next node
}else{
break;
}
}
}
//Insert data into the linked list, pHead represents the head node of the linked list to be inserted
struct Student * insertLink(struct Student * pHead){
//Create a new node
struct Student * pNew,*pTemp;
pNew=(struct Student*) malloc(sizeof(struct Student));
printf("Please enter name:");
scanf("%s",&pNew->name);
printf("Please enter student number:");
scanf("%s", &pNew->number);
pTemp=pHead->next; //The address of the next node of the original pHead node
pHead->next=pNew;
pNew->next=pTemp;
return pHead;
}
//Delete the linked list, pHead is the head node pointer, index is the node to be deleted
void deleteLink(struct Student * pHead){
int index;
int i;
struct Student * pTemp,*pTemp2,*pTemp3;
printf("Please enter the index number of the node to be deleted:");
scanf("%d",&index);
pTemp2=pHead;
//delete the second node
for(i=0;i<index-1;i++ ){
pTemp3=pTemp2;
pTemp=pTemp2->next; //pointer to the second node
pTemp2=pTemp;
}
pTemp3->next=pTemp->next;
free(pTemp);
}