C + + implementation of one-way linked list

Source: Internet
Author: User

********************************************************************
* File Name:example1.h Example1.cpp *
* created:2012.5.25 *
* Author:anderson *
* Description: The creation, deletion, insertion of one-way linked list. C + + implementation
*********************************************************************/

Example1.h file


#ifndef Linked_list_h
#define Linked_list_h

typedef struct STUDENT_INFO
{
int number; Number
Float score;//Grade
struct Student_info *next;
}studentinfo;

Class Linked_list
{
Private
int nodenumber; Number of nodes
Studentinfo *head; Linked table Header
int deletenumber; The node to delete
Studentinfo Stuinfo; Insert Object
Public

Public

Public
Studentinfo *createlist (); Create a linked list

void Printlist (); Output linked list

Studentinfo *deletenode (int); Delete a node

Studentinfo *insertnode (studentinfo&); Insert Node
};
#endif

Example1.cpp file

#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::D eletenode (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)//by number insert
 {
  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;
 }
&nbs P 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;
 & nbsp
   Else
    {
    p1->next=&stuinfo;
     Stuinfo.next=null;
   }
  }
  nodenumber++;
     return head;
 }

void Linked_list::P rintlist (/*studentinfo **/)//No parameters are required here
{
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 ();
}

Originally want to use C + + package to achieve abstract linked list operation, and later did not know how to write, can only write these, if the great God, know, please tell me AH.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.