链表的普通应用

#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
    int ID;
	float salary;
	struct Node* next;
}Employee;
Employee *Create_T(int n)  //尾插法
{
   Employee *Head,*cur,*pNew;
   int i;
   Head=cur=NULL;     //很重要,一定要有结束的指令
   for (i=0;i<n;i++)
   {
      pNew=(Employee*)malloc(sizeof(Employee));
	  printf("输入第%d个员工的信息:\n",i+1);
	  scanf("%d%f",&pNew->ID,&pNew->salary);
	  if (i==0)
		  Head=cur=pNew;
	  else
	  {
		  cur->next=pNew;
		  cur=pNew;
	  }
   }
   cur->next=NULL;
   return Head;
}
Employee *Create_H(int n)   //头插法
{
    Employee *Head,*pNew;
	int i;
	Head=(Employee*)malloc(sizeof(Employee));
	 printf("输入第一个员工的信息:\n");
	  scanf("%d%f",&pNew->ID,&pNew->salary);
	  Head->next=NULL;
      for (i=2;i<=n;i++)
      {
	    pNew=(Employee*)malloc(sizeof(Employee));
	printf("输入第%个员工的信息:\n",i);
	  scanf("%d%f",&pNew->ID,&pNew->salary);
	 pNew->next=Head->next;
	 Head->next=pNew;
	  }
	  return Head;
}
void Print(Employee *Head)
{
   Employee *p;
   p=Head;
   printf("\n请输出链表中的相关信息:\n");
   while (p)
   {
     printf("ID:%d\tsalary:%.2f\n",p->ID,p->salary);
   p=p->next;
   }
}
Employee *Search_Node(Employee *Head,int id)
{
       Employee *p=Head;
	   while (p&&p->ID!=id)
	      p=p->next;
        return p;
}
Employee *Insert_Node(Employee *Head,int i,int id,float sala)
{
    Employee *p,*s;
	int j;
    s=(Employee*)malloc(sizeof(Employee));
	s->ID=id;
     s->salary=sala;
	 if (i==1)
	 {
	   s->next=Head;
	   Head=s;
	 }
	 else
	 {
	     j=1;
		 p=Head;
		 while (p&&j<i-1)
		 {
		   p=p->next;
		   j++;
		 }
		 if (!p||j>i-1)
		 {
		    printf("i值非法\n");
			return NULL;
		 }
		 s->next=p->next;
		 p->next=s;
	 }
	 return Head;
}
Employee *Delete_Node(Employee *Head,int i)
{
    Employee *p,*q;
	int j;
	if (i==1)
	{
	q=Head;
	Head=Head->next;
	free(q);
	}
    else
	{
	  j=1;
	  p=Head;
	  while (p&&j<i-1)
		 {
		   p=p->next;
		   j++;
		 }
		 if (!p||j>i-1)
		 {
		    printf("i值非法\n");
			return NULL;
		 }
		 q=p->next;
		 p->next=q->next;
	     free(q);
	}
       return Head;
}
int main()
{
   Employee *Head,*p;
   int i,id,n;
   float sala;
   int c;
   while (scanf("%d",&n),n)
   {
   printf("输入%d个员工的信息,建立链表;\n",n);
   Head=Create_T(n);
   Print(Head);
   printf("\n输入要查找员工的工号:");
   scanf("%d",&id);
   p=Search_Node(Head,id);
   if (p)
     printf("工号为%d的员工的工资是:%.2f\n",id,p->salary);
   else
	   printf("该表中无此员工.\n");
   printf("\n输入要插入结点的位置;");
   scanf("%d",&i);
   printf("输入员工的信息:\n");
   scanf("%d%f",&id,&sala);
   Head=Insert_Node(Head,i,id,sala);
   Print(Head);
   printf("\n输入要删除的结点的位置:");
   scanf("%d",&i);
   Head=Delete_Node(Head,i);
   Print(Head);
   }
   return 0;
}    

                                                                                                                                                                                                                                                                                                                                                  
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
    int ID;
	float salary;
	struct Node* next;
}Employee;
Employee *Create_T()  //尾插法
{
   Employee *Head,*cur,*pNew;
   int i,n;
   Head=cur=NULL;     //很重要,一定要有结束的指令
   scanf("%d",&n);
     printf("输入%d个员工的信息,建立链表;\n",n);
   for (i=0;i<n;i++)
   {
      pNew=(Employee*)malloc(sizeof(Employee));
	  printf("输入第%d个员工的信息:\n",i+1);
	  scanf("%d%f",&pNew->ID,&pNew->salary);
	  if (i==0)
		  Head=cur=pNew;
	  else
	  {
		  cur->next=pNew;
		  cur=pNew;
	  }
   }
   cur->next=NULL;
   return Head;
}
Employee *Create_H(int n)   //头插法
{
    Employee *Head,*pNew;
	int i;
	Head=(Employee*)malloc(sizeof(Employee));
	 printf("输入第一个员工的信息:\n");
	  scanf("%d%f",&pNew->ID,&pNew->salary);
	  Head->next=NULL;
      for (i=2;i<=n;i++)
      {
	    pNew=(Employee*)malloc(sizeof(Employee));
	printf("输入第%个员工的信息:\n",i);
	  scanf("%d%f",&pNew->ID,&pNew->salary);
	 pNew->next=Head->next;
	 Head->next=pNew;
	  }
	  return Head;
}
void Print(Employee *Head)
{
   Employee *p;
   p=Head;
   printf("\n请输出链表中的相关信息:\n");
   while (p)
   {
     printf("ID:%d\tsalary:%.2f\n",p->ID,p->salary);
   p=p->next;
   }
}
int Search_Node(Employee *Head)
{     
	int id;
       printf("\n输入要查找员工的工号:");
   scanf("%d",&id);
       Employee *p=Head;
	   while (p&&p->ID!=id)
	      p=p->next;
	   if (p)
     printf("工号为%d的员工的工资是:%.2f\n",id,p->salary);
   else
	   printf("该表中无此员工.\n");
        return 0;
}
Employee *Insert_Node(Employee *Head)
{
    Employee *p,*s;
	int i,j,id;
	float sala;
	printf("\n输入要插入结点的位置;");
   scanf("%d",&i);
   printf("输入员工的信息:\n");
   scanf("%d%f",&id,&sala);
    s=(Employee*)malloc(sizeof(Employee));
	s->ID=id;
     s->salary=sala;
	 if (i==1)
	 {
	   s->next=Head;
	   Head=s;
	 }
	 else
	 {
	     j=1;
		 p=Head;
		 while (p&&j<i-1)
		 {
		   p=p->next;
		   j++;
		 }
		 if (!p||j>i-1)
		 {
		    printf("i值非法\n");
			return NULL;
		 }
		 s->next=p->next;
		 p->next=s;
	 }
	 return Head;
}
Employee *Delete_Node(Employee *Head)
{
    Employee *p,*q;
	int i,j;
	 printf("\n输入要删除的结点的位置:");
   scanf("%d",&i);
	if (i==1)
	{
	q=Head;
	Head=Head->next;
	free(q);
	}
    else
	{
	  j=1;
	  p=Head;
	  while (p&&j<i-1)
		 {
		   p=p->next;
		   j++;
		 }
		 if (!p||j>i-1)
		 {
		    printf("i值非法\n");
			return NULL;
		 }
		 q=p->next;
		 p->next=q->next;
	     free(q);
	}
       return Head;
}
int main()
{
   Employee *Head;
   int c;
   while (1)
   {
	  printf("\t输入“1”建立链表\n");
	  printf("\t输入“2”遍历链表\n");
	  printf("\t输入“3”查询结点\n");
	  printf("\t输入“4”插入结点\n");
	  printf("\t输入“5”删除结点\n");
	  printf("\t输入“6”结束\n");
	   scanf("%d",&c);
	   switch(c)
	   { 
	   case 1:Head=Create_T();break;
	   case 2:Print(Head);break;
	   case 3:Search_Node(Head);break;
	   case 4:Insert_Node(Head);break;
	   case 5:Delete_Node(Head);break;
	   case 6:return 0;
	   }
   }
   return 0;
}

你可能感兴趣的:(链表的普通应用)