马上要做程序设计期末作业 (⋟﹏⋞),恰好在实验手册上看到单向链表,顺便学习下,如有错误欢迎指正。
节点结构体定义
struct node
{
char name[20];
int age;
double salary;
struct node *next;
};
链表的建立及增加
struct node *creat(struct node *head,struct node data){
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
data.next=NULL;
*p=data;
if(head!=NULL)p->next=head;
return p;
}
链表数据输出
void print(struct node *head){
if(head==NULL){
printf("No storage information is available\n");
return;
}
printf("name\tage\tsalary\n");
while(head!=NULL){
printf("%s\t%d\t%.2lf\n",head->name,head->age,head->salary);
head=head->next;
}
}
链表数据查找 这里举例名字
struct node *find(struct node *head,char f_name[20]){
if(head==NULL){
printf("This information is not included in the data\n");
return NULL;
}
while(strcmp(head->name,f_name)&&head->next!=NULL)
head=head->next;
if(!strcmp(head->name,f_name)){
printf("%s\t%d\t%.2lf\n",head->name,head->age,head->salary);
return head;
}
printf("This information is not included in the data\n");
return NULL;
}
链表数据删除 这里举例名字
struct node *del(struct node *head,char d_name[20]){
struct node *p,*q=NULL;
p=head;
while(p->next!=NULL&&strcmp(p->name,d_name)){
q=p;
p=p->next;
}
if(!strcmp(p->name,d_name)){
if(q==NULL)head=p->next;
else q->next=p->next;
free(p);
printf("The data is deleted\n");
}
else printf("The data not been found\n");
print(head);
return head;
}
链表数据修改
void modification(struct node *head,char m_name[20]){
struct node *q;
q=find(head,m_name);
if(q!=NULL){
printf("Please enter\nName\tAge\tSalary\n");
scanf("%s%d%lf",q->name,&q->age,&q->salary);
print(head);
}
}
完整代码
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node
{
char name[20];
int age;
double salary;
struct node *next;
};
//链表的建立及增加
struct node *creat(struct node *head,struct node data){
struct node *p;
p=(struct node*)malloc(sizeof(struct node));
data.next=NULL;
*p=data;
if(head!=NULL)p->next=head;
return p;
}
//链表数据输出
void print(struct node *head){
if(head==NULL){
printf("No storage information is available\n");
return;
}
printf("name\tage\tsalary\n");
while(head!=NULL){
printf("%s\t%d\t%.2lf\n",head->name,head->age,head->salary);
head=head->next;
}
}
//链表数据查找 这里举例名字
struct node *find(struct node *head,char f_name[20]){
if(head==NULL){
printf("This information is not included in the data\n");
return NULL;
}
while(strcmp(head->name,f_name)&&head->next!=NULL)
head=head->next;
if(!strcmp(head->name,f_name)){
printf("%s\t%d\t%.2lf\n",head->name,head->age,head->salary);
return head;
}
printf("This information is not included in the data\n");
return NULL;
}
//链表数据删除 这里举例名字
struct node *del(struct node *head,char d_name[20]){
struct node *p,*q=NULL;
p=head;
while(p->next!=NULL&&strcmp(p->name,d_name)){
q=p;
p=p->next;
}
if(!strcmp(p->name,d_name)){
if(q==NULL)head=p->next;
else q->next=p->next;
free(p);
printf("The data is deleted\n");
}
else printf("The data not been found\n");
print(head);
return head;
}
//链表数据修改
void modification(struct node *head,char m_name[20]){
struct node *q;
q=find(head,m_name);
if(q!=NULL){
printf("Please enter\nName\tAge\tSalary\n");
scanf("%s%d%lf",q->name,&q->age,&q->salary);
print(head);
}
}
int main(){
int i,n;
struct node *head=NULL,data;
char f_name[20],d_name[20],m_name[20];
printf("Please enter the number of data to be added\n");
scanf("%d",&n);
printf("Please enter\nName\tAge\tSalary\n");
for(i=0;i<n;i++){
scanf("%s%d%lf",data.name,&data.age,&data.salary);
head=creat(head,data);
}
print(head);
printf("Please enter the name you want to find\n");
scanf("%s",f_name);
find(head,f_name);
printf("Please enter the name of the employee you want to delete\n");
scanf("%s",d_name);
head=del(head,d_name);
printf("Please enter the name of the information you want to modify\n");
scanf("%s",m_name);
modification(head,m_name);
return 0;
}