C语言链表的插入和删除、建立

choiceTXT.h

//执行要重复的代码,选择执行的醒目并将结果付给choice
#include"stdio.h"

int choiceTXT()
{
 int choice;
 
 printf("/n/t请选择你要执行的选项:");
 printf("/n===============================/n/n");
 printf("/t1.创建/n");
 printf("/t2.浏览/n");
 printf("/t3.插入/n");
 printf("/t4.删除/n");
 printf("/t5.退出/n");
 printf("/n===============================/n");
 printf("/t你要的操作(1-5):");
 scanf("%d",&choice);
 return choice;
}

int choice()
{
 int choice = choiceTXT();

 while(1)
 {
 
  if((choice < 1) || (choice >5))
  {
   printf("/n/t你的选择无效!!重新选择。");
   choice = choiceTXT();
  }
  else
  {
   return choice;
  }
 }
}

 

creat.c

 

#include"stdio.h"
#include"stdlib.h"
#include"define.h"

extern count;

struct stuList *creat()
{
 struct stuList *head,*pf,*pc;//pc:当前输入的记录
                              //pf:前一条记录
 int yn=1;
 
 while(yn)
 {
  pc = (struct stuList *)malloc(sizeof( struct stuList));
  
  if(pc==NULL)
  {
   printf("/n/tCREAT ERRO!!/n ");
   return NULL;
  }
  printf("/t请输入姓名:");
  fflush(stdin);
  scanf("%s",pc->stuName);
  printf("/t请输入学号:");
  fflush(stdin);
  scanf("%d",&pc->stuId);

  printf("/t请输入成绩:");
  fflush(stdin);
  scanf("%d",&pc->stSoult);
  
  if(count==0)
   head = pf = pc;
  else pf->next = pc;//前一条纪律的next指向当前记录
  count++;
  pc->next = NULL;
  pf = pc;
  
  printf("/t是否还要输入:");
  scanf("%d",&yn);

  if(yn == 0 )
   return head;
 }
  
}

 

define.h

 

// 定义用于链表的结构
struct stuList
{
 char stuName[10];
 int  stuId;
 int  stSoult;
 struct stuList *next;
};

 

dele.c

 

#include"stdio.h"
#include"define.h"
#include"stdlib.h"

struct stuList *dele(struct stuList *head)
{
 struct stuList *back,*pf;
 int  numId;

 if(head==NULL)
 {
  printf("/t NO DELETE BECOUSE OR LIST IS NULL!!");
  return NULL;
 }

 printf("/t请你输入你要删除的的学生学号:");
 scanf("%d",&numId);

 back = head;
 if(head->stuId == numId)
 {
  back = back->next;
  free(head);
  return back;
 }

    pf = back;
 back=back->next;
 while(back != NULL)
 {
  if(back->stuId == numId)
  {
   pf->next=back->next;
   free(back);
   return head;
  }
  pf = back;
  back=back->next;
 }
 printf("/n/t没有你要删除的学生:/n");
 return head;
}

 

display.c

#include"stdio.h"
#include"define.h"

void disp(struct stuList *head)
{
 struct stuList *pc;
 pc = head;
 printf("/t姓名/t学号/t成绩/n");
 while(pc != NULL)
 {
  printf("/t%s/t%d/t%d/n",pc->stuName,pc->stuId,pc->stSoult);
  pc=pc->next;

 }
}

 

insert.c

 

#include"stdio.h"
#include"stdlib.h"
#include"define.h"

extern count;

struct stuList * insert(struct stuList *head)
{
 struct stuList *pc,*pi,*back;
 int numId;
 
 pi = (struct stuList *)malloc(sizeof(struct stuList));
 printf("/t要插入学生的姓名:");
 fflush(stdin);
 scanf("%s",pi->stuName);

 printf("/t要插入学生的学号:");
 scanf("%d",&pi->stuId);

 printf("/t请输入成绩:");
 fflush(stdin);
 scanf("%d",&pi->stSoult);

 if(head==NULL)
 {  
  head=pi;
  head->next=NULL;
  count++;
  return head;
 }
 printf("/n/t请输入要插入的位置:");
 scanf("%d",&numId);
 
 pc = head;
 while (pc !=NULL)
 {
  if(pc->stuId==numId)
  {
   pi->next = pc->next;
   pc->next = pi;
   count++;
   return head;
  }
  back=pc;
  pc=pc->next;
 }
 
 pi->next=NULL;
 back->next=pi;
  
 count++;
 return head;
}

 

 

LinkList.c

 

#include"stdio.h"
#include"choiceTXT.h"
#include"define.h"

int count=0;
struct stuList *creat();
void disp(struct stuList *head);
struct stuList *insert(struct stuList *head);
struct stuList *dele(struct stuList *head);

void main()
{
 struct stuList *head;
 int    chose;

 head=NULL;//初始化指针head;
  
 while(1)
 {
  chose=choice();
  switch(chose)
  {
  case 1:
   head=creat();
   break;
  case 2:
   if(head==NULL)
   {
    printf("/tLIST IS NULL!!/n");
    break;
   }else
   {
    disp(head);
    break;
   }
  case 3:
   head=insert(head);
   break;
  case 4:
   head = dele(head);
   break;
  case 5:
   return;
  }
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值