单向链表的c++实现

来源:互联网 发布:nginx htaccess 编辑:程序博客网 时间:2024/06/15 23:45

********************************************************************
* File Name    : example1.h  example1.cpp                                                   *
* Created      : 2012.5.25                                                                      *
* Author       : anderson                                                                   *
* Description  : 单向链表的创建,删除,插入.c++实现
*********************************************************************/

example1.h文件


#ifndef LINKED_LIST_H
#define LINKED_LIST_H

typedef struct student_info
 {
 int number; //编号
 float score;//成绩
 struct student_info *next;
 }STUDENTINFO;

class Linked_List
 {
 private:
  int Nodenumber; //节点数量
  STUDENTINFO *head; //链表头
  int Deletenumber; //要删除的节点
  STUDENTINFO stuinfo; //插入对象
 public:
       
 public:
  

 public:
  STUDENTINFO *CreateList(); //创建链表

  void PrintList(); //输出链表

        STUDENTINFO *DeleteNode(int); //删除节点

        STUDENTINFO *InsertNode(STUDENTINFO&); //插入节点
 };
#endif

 

example1.cpp文件

#include "example1.h"
#include <iostream.h>
#include <stdlib.h>

STUDENTINFO* Linked_List::CreateList()
 {
 STUDENTINFO *p1,*p2;
 Nodenumber=0;
 head=NULL;

 p1=p2=new STUDENTINFO;
 cout<<"please input students info:"<<endl;
 cin>>p1->number>>p1->score;

 while (p1->number!=0)
 {
 Nodenumber++;

 if(Nodenumber==1)
  head=p1;
 else
  p2->next=p1;

 p2=p1;
 p1=new STUDENTINFO;
    cin>>p1->number>>p1->score;
 }
 p2->next=NULL;

     return head;
 }

STUDENTINFO* Linked_List::DeleteNode(int deletednum)
 {
 STUDENTINFO *p1,*p2;

 //cout<<"please input deleted number:";
 //cin>>Deletenumber;
 Deletenumber=deletednum;

 if (head==NULL)
 {
 cout<<"list null!"<<endl;
 
// goto end;
 exit(0);
 }
    p1=head;

 while(Deletenumber!=p1->number&&p1->next!=NULL)
  {
  p2=p1;
  p1=p1->next;
  }

 if (Deletenumber==p1->number)
 {
 if (p1==head)
  head=p1->next;
 else
  p2->next=p1->next;
 cout<<"delete "<<Deletenumber<<endl;
    Nodenumber--;
 }
 else
  cout<<Deletenumber<<"not found!"<<endl;
// end;
     return head;
 }

STUDENTINFO* Linked_List::InsertNode(STUDENTINFO &stu) //按编号插入
 {
  STUDENTINFO *p1,*p2;
     stuinfo=stu;
//  cout<<"please input inserted students info:";
//  cin>>stuinfo.number>>stuinfo.score;

//   p0=&stuinfo;
  p1=head;
  if (head==NULL)
  {
  head=&stuinfo;
  stuinfo.next=NULL;
  }
  else
   {
   while (stuinfo.number>p1->number&&p1->next!=NULL)
   {
   p2=p1;
   p1=p1->next;
   }
   if (stuinfo.number<=p1->number)
   {
   if(head==p1)
    head=&stuinfo;
   else
    p2->next=&stuinfo;
   stuinfo.next=p1;
   }
   else
    {
    p1->next=&stuinfo;
    stuinfo.next=NULL;
    }
   }
  Nodenumber++;
     return head;
 }

void Linked_List::PrintList(/*STUDENTINFO **/) //此处根本不需要参数
 {
     STUDENTINFO *p=head;
  cout<<"there are "<<Nodenumber<<" students info:"<<endl;
  while(p!=NULL)
   {
   cout<<p->number<<"\t"<<p->score<<endl;
   p=p->next;
   }
 }

void main()
 {
     Linked_List list;
  int deletednum;
  STUDENTINFO stu;

  list.CreateList();
  list.PrintList();

  cout<<"please input deleted number:";
  cin>>deletednum;
  list.DeleteNode(deletednum);
  list.PrintList();

     cout<<"please input inserted students info:";
  cin>>stu.number>>stu.score;
  list.InsertNode(stu);
  list.PrintList();
 }

 

本来是想利用c++封装实现抽象链表操作,后来也不知道怎么写了,只能写到这些,如果哪位大神,知道,请告诉我啊。
原创粉丝点击