链表几种常用操作回顾

============== 链表几种常用操作回顾   ==================

//***********************************
//**      链表各种操作回顾     **  
//***********************************

#include <iostream.h>

//------------------------------------------------------------------------------
//***define eare***

//------------------------------------------------------------------------------
//***varible eare***
struct Student
{
 long number;
 float score;
 Student *next;
};

Student *head;//链首指针  注意:结构体被定义成了指针类型!!

//------------------------------------------------------------------------------
//***function pro.***
Student *Create(void);     //创建链表  注意:在该结构体类型下又定义了一个指针函数!!
void ShowList(Student *head);   //打印链表

Student *Delete(Student *head,long number);      //删除链表中某结点
Student *Insert(Student *head,Student *add_node); //插入链表结点

//------------------------------------------------------------------------------
//***main code***
void main(void)
{
 Student *phead;
 //long del_node;
 Student add_node;
 //先创建并显示init的链表
 
 phead=Create();   //注意上面定义的指针函数的调用方法!!!
 ShowList(phead);
 
 //删除链表指点结点并显示modify的链表
 /*cout<<"Do you want to delete which node? please input the number:";
 cin>>del_node;
 phead=Delete(phead,del_node);
 ShowList(phead);*/

 //插入链表结点并显示modify的链表
 cout<<"please input number&score :"<<endl;
 cin>>add_node.number>>add_node.score;

 phead=Insert(phead,&add_node);
 ShowList(phead);
}

//------------------------------------------------------------------------------
//function: Create() 创建链表
//input: void
//output:
//return: Student *
//other: fzh 2006-1-19
//------------------------------------------------------------------------------
Student *Create(void)
{
 Student *pN;    //创建链表结点的指针
 Student *pEnd;    //链尾指针,用于其后插入结点指针
 pN=new Student;    //新建一个结点,准备插入链表
 cout<<"please input numer and score:/n";
 cin>>pN->number>>pN->score; //给结点赋值
 head=NULL;     //一开始链表为空
 pEnd=pN;

 while(pN->number !=0)
 {
  if(head==NULL)
   head=pN;
  else
   pEnd->next=pN;

  pEnd=pN;    //N点处
  pN=new Student;
  cout<<"please input numer and score:/n";
  cin>>pN->number>>pN->score;
 }
 pEnd->next =NULL;
 delete pN;
 return (head);
}

//------------------------------------------------------------------------------
//function: ShowList() 显示链表
//input: void
//output:
//return: Student *
//other: fzh 2006-1-19
//------------------------------------------------------------------------------

void ShowList(Student *head)
{
 cout<<"*******now the items of list is***********/n";
 while(head)
 {
  cout<<head->number<<","<<head->score <<endl;
  head=head->next;
 }

}

//------------------------------------------------------------------------------
//function: Delete() 删除链表指点结点
//input: void
//output:
//return: Student *
//other: fzh 2006-1-19
//------------------------------------------------------------------------------
Student *Delete(Student *head,long number)
{
 Student *p;
 Student *pFind;        //"哨兵"指针
 if(!head)
 {
  cout<<"/n the link table is Null!";
  return NULL;
 }
 //情况1:要删除的结点是链首
 if(head->number==number)
 {
  p=head;
  head=head->next;
  delete p;
  cout<<number<<"the head of list have been deleted"<<endl;
  return (head);
 }

 //情况2:要删除的结点在中间或链尾
 for(pFind=head;pFind->next;pFind=pFind->next)/***
 {
  if(pFind->next->number==number)
  {
   p=pFind->next;//待删除的结点
   pFind->next=p->next;
   delete p;
   cout<<number<<" have been deleted!"<<endl;
   
   return (head);
  }
 }

 //没有这个结点
 cout<<" /n no the node ^Q^ "<<endl;
 return NULL;
}

//------------------------------------------------------------------------------
//function: Insert() 插入链表结点
//input: void
//output:
//return: Student *
//other: 1.fzh 2006-1-19
// 2.插入要求:创建链表时,结点的number是按小到大顺序排列的。
//插入后顺序一至
//------------------------------------------------------------------------------
Student *Insert(Student *head,Student *add_node)
{
 Student *pFind;        //"哨兵"指针

 //情况1:空链表
 if(!head)//(head==NULL)   空链表时,将结点置于链首
 {
  head=add_node;   //链首
  add_node->next=NULL; //链尾 
  return head;
 }
 //情况2:插到链首
 if(head->number > add_node->number)
 {
  add_node->next=head;
  head=add_node;
  return head;

 }
 //情况3:正常情况
 for(pFind=head;pFind;pFind=pFind->next)/
 {
  if(pFind->next->number > add_node->number)//找到插入位置
  {
   add_node->next=pFind->next;
   pFind->next=add_node;
   return head;
  }
 }
 return NULL;
}

 =================   C:链表初步(8个功能) ================

//chain

#include
<stdio.h>
#include
<string.h>
#include
<malloc.h>
#define NULL 0
#define LEN sizeof(struct node)

struct node{
long num;
char name[20];
struct node *next;
}
;

struct node *creat(void); //建立链表
struct node *insert(struct node *head); //插入节点
struct node *find(struct node *head); //查找
struct node *del(struct node *head); //删除
int count(struct node *head);//计数
void list(struct node *head);//列表
struct node *conv(struct node *head); //倒序
struct node *sort(struct node *head); //排序

void main()
{
struct node *head=NULL,*p;
int i=1;
while(i)
{
   printf(
"1.CREATE 2.COUNT 3.LIST 4.INSERT 5.FIND ");
   printf(
"6.DELETE 7.CONVERT 8.SORT 其他.QUIT ");
   
if(!scanf("%d",&i))break;
   
switch(i)
   
{
   
case 1: head=creat();break;
   
case 2: printf("%d个数据 ",count(head));break;
   
case 3: list(head);break;
   
case 4if(head)head=insert(head);break;
   
case 5:
    
{
     
if(p=find(head))printf("%ld %s ",p->num,p->name);
     
else printf("找不到 ");
     
break;
    }

   
case 6:head=del(head);break;
   
case 7:head=conv(head);break;
   
case 8:head=sort(head);break;
   
default:i=0;break;
   }

}

}


struct node *creat(void//先输入的在底部,后输入的在顶部
{
struct node *q=NULL,*p;
p
=(struct node *)malloc(LEN);
scanf(
"%ld%s",&p->num,p->name);
while(p->num)
{
   
if(!q)
   
{
    p
->next=NULL;
    q
=p;
   }

   
else
   
{
    
if(q!=p)
    
{
     p
->next=q;
     q=
p;
    }

    p
=(struct node *)malloc(LEN);
    scanf(
"%ld%s",&p->num,p->name);
   }

}

return(q);
}


struct node *insert(struct node *head) //插在第一个位置
{
struct node *p;
p
=(struct node *)malloc(LEN);
scanf(
"%ld%s",&p->num,p->name);
if(p->num)
{
   p
->next=head;
   head
=p;
}

return head;
}


struct node *find(struct node *head)
{
long a;
struct node *loc=NULL;
printf(
"输入学号:");
scanf(
"%ld",&a);
if(a||head)
{
   
for(;head;head=head->next)
    
if(head->num==a)loc=head;
}

return loc;
}


struct node *del(struct node *head)
{
struct node *p,*q;
if(p=find(head)) //先查找
{
   
if(p==head)head=head->next; //删除节点在表头
   else
   
{
    
for(q=head;q->next!=p;q=q->next); //找到p前面的节点
    q->next=p->next; //删除之
   }

}

else printf("无法删除 ");
return head;
}



int count(struct node *head)
{
int z;
struct node *p;
p
=head;
for(z=0;p;z++,p=p->next);
return z;
}


void list(struct node *head)
{
struct node *p=head;
for(;p;p=p->next)printf("%ld %s ",p->num,p->name);
}



struct node *conv(struct node *head)
{
struct node *p,*q,*r;
if(head)
{
   
if(head->next)
   
{
    q
=head->next;
    r
=head->next;
    p
=head;
    p
->next=NULL;
    
while(q->next)
    
{
     r
=r->next;
     q
->next=p;
     p
=q;
     q
=r;
    }

    q
->next=p;
    
return q;
   }

   
else return head;
}

return NULL;
}


struct node *sort(struct node *head)
{
struct node *newhead=NULL,*p,*q,*q1,*q2;
if(head)
{
   p
=head;
   newhead
=(struct node *)malloc(LEN);
   q1
=newhead;
   q2
=newhead;
   newhead
->next=NULL;
   newhead
->num=p->num;
   strcpy(newhead
->name,p->name);
   p
=p->next;
   
while(p)
   
{
    q
=(struct node *)malloc(LEN);
    q
->num=p->num;
    strcpy(q
->name,p->name);
    
while((p->num>q1->num)&&(q1->next!=NULL)) //如果要插入的比当前的大且没有到达末尾,则后移
    {
     q2
=q1;
     q1
=q1->next;
    }

    
if(p->num<=q1->num) //找到位置了
    {
     
if(newhead==q1)newhead=q; //头节点的情况
     else q2->next=q;
     q
->next=q1; //链好
    }

    
else //表尾
    {
     q1
->next=q;
     q
->next=NULL;
    }

   p
=p->next;
   q1
=newhead;
   q2
=newhead;
   }

}

return newhead;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值