单链表函数

/*******************************************************************/

//这个是一个单链表的函数,里面有链表的建立,删除,链表的查找,打印

//节点的插入,节点的删除,单个节点的打印

//节点的删除需要两个参数,一个是检索节点,还有一个就是检索节点前一个节点

/*******************************************************************/

 

 

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
/*****************************************************************/
//struct student
/*****************************************************************/
struct student
{
 int number;
 char name[10];
 char address[20];
 float math;
 struct student * next;      
};

typedef struct student * STU;
/*****************************************************************/
//creat function
/*****************************************************************/
STU creat()
{
  STU ptr;
  ptr=(STU)malloc(sizeof(struct student));
  if(ptr==NULL){printf("Out of memory!"),exit(1);}
  return ptr;
}
/*****************************************************************/
//addnode function
/*****************************************************************/
STU addnode(STU head)
{
STU node;
node=creat();
printf("Pls input student number:");scanf("%d",&node->number);while(getchar()!='/n')continue;
printf("Pls input student name:");scanf("%s",&node->name);while(getchar()!='/n')continue;
printf("Pls input student address:");scanf("%s",node->address);
printf("Pls input student math:");scanf("%f",&node->math);
node->next=head;
head=node;  
return head;
}

/*****************************************************************/
//display funciton
/*****************************************************************/
STU display(STU head)
{STU temp;
temp=head;
printf("Number      name      address      math/n");
while(temp!=NULL)   
{
printf("%d,%s,%s,%.2f/n",temp->number,temp->name,temp->address,temp->math);
temp=temp->next;
}
}

/*****************************************************************/
//displaynode funciton
/*****************************************************************/
STU displaynode(STU temp)
{
printf("Number      name      address      math/n");
printf("%d,%s,%s,%.2f/n",temp->number,temp->name,temp->address,temp->math);

}
/*****************************************************************/
//deletelist function
/*******************************************************************/
STU deletelist(STU head)
{
 STU temp;
 temp=head;
 while(temp!=NULL)
 {
  head=temp->next;
  free(temp);
   temp=head;          
 }     
  return head; 
}
/*****************************************************************/
//findnode function
/*******************************************************************/
STU findnode(STU head,int num)
{
 STU temp;
 temp=head;
 if(temp==NULL){printf("The linkedlist is empty!");return NULL;}
 while((temp->number!=num)&&temp!=NULL){ temp=temp->next;}  
 if(temp->number==num) return temp;
 if(temp==NULL){printf("The num is not in linkedlist!");return NULL;}
}

 

 

/*****************************************************************/
//delete function

//case 1 the linkedlist is empty and the element is not in the linkedlist

//case the head is required node ,the tail is the required, the midd
/*******************************************************************/
STU deletenode(STU head,int num)
{
 STU temp,pretemp;
 temp=head;
 if(temp==NULL){printf("The linkedlist is empty!");return NULL;}
 while((temp->number!=num)&&temp!=NULL){ ;pretemp=temp;temp=temp->next;}   
if(temp==NULL){printf("The num is not in linkedlist!");return NULL;}

 if(temp==head){head=temp->next;free(temp);return head;}
 if(temp->next==NULL){pretemp->next=NULL;return head;}

else Pretemp->next=temp->next;free(temp);return head;
 
}
/*****************************************************************/
//insertnode function
/*****************************************************************/
STU addnode(STU head,int num)
{
STU node,temp;
node=creat();
printf("Pls input student number:");scanf("%d",&node->number);while(getchar()!='/n')continue;
printf("Pls input student name:");scanf("%s",&node->name);while(getchar()!='/n')continue;
printf("Pls input student address:");scanf("%s",node->address);
printf("Pls input student math:");scanf("%f",&node->math);
temp=findnode(head,num);
if(temp==NULL){printf("The linkedlist  empty or The num is error !");head=node,head->next=NULL;return head;}
if(temp->next==NULL){temp->next=node;node->next=NULL;return head;}
else {temp->next=node;node->next=temp->next->next;return head;}
}

 

/*****************************************************************/

//Main funciton
/****************************************************************/
int main(void)
{
    STU head;
    int i;
    head=NULL;
   
    head=addnode(head);
    head=addnode(head);
    display(head);
   
    printf("Pls input your want to find data:");
    scanf("%d",&i);
    displaynode(findnode(head,i));
   
    printf("Pls input your want to insert data:");
    scanf("%d",&i);
    insertnode(head,i);
    display(head);
   
    printf("Pls input your want to delete data:");
    scanf("%d",&i);
    deletenode(head,i);
    display(head);
   
    if(deletelist(head)!=NULL)printf("The list not delete!");
    while(1);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值