The basic operation of a single linked list: build, length, output, sort, insert, delete, invert

Source: Internet
Author: User

The process realizes the establishment of a single chain list, asks the length of the single chain table, prints the output single linked list, sorts the single linked list, inserts the elements, deletes the elements, and resets the single linked list.

I am referring to reference materials, and then write their own specifications, the function has been called, each call, there are output single linked list. The program is complete and has been debugged and run.

SOURCE program:

#include <iostream> #include <stdio.h> #include <string.h> #include <conio.h> using namespace
Std
  typedef struct STUDENT {int data;
struct student *next;
}node;
  Establish a single link table node *creat () {node *head,*p,*s;
  int x,cycle=1;
  Head= (node*) malloc (sizeof (node));
  P=head;
  cout<< "Please enter the data of a single linked list and use 0 as an end mark:" <<endl;
    while (cycle) {cin>>x;
      if (x!=0)//is identified with 0 {s= (node *) malloc (sizeof (node));
      s->data=x;
      p->next=s;
    P=s;
  else cycle=0;
  } head=head->next;
  p->next=null;
  cout<<endl;
return (head);
  //To find the length of the single linked list (int length (node *head) {int n=0;
  Node *p;
  P=head;
    while (p!=null) {p=p->next;
  n++;
return (n);
  //output single linked list void print (node *head) {node *p;int n;
  N=length (head);
  cout<< "Now, a single linked list with a length of <<n<< is updated to:" <<endl;
  P=head;
      if (Head!=null) {while (p!=null) {cout<<p->data<< ' \ t ';
    p=p->next;
  }
  }printf ("\ n");
  //Delete one of the element node *del (node *head,int num) {node *P1,*P2 in a single linked list;
  P1=head;
    while (num!=p1->data&&p1->next!=null) {p2=p1;
  p1=p1->next;
      } if (Num==p1->data) {if (p1==head) {head=p1->next;
    Free (p1);
  else p2->next=p1->next; Else cout<<num<< "is not found in a single linked list. "<<endl;//printf (" \n%d not found in a single linked list.)
  ", num);
return head;
  ///Insert an element node *insert (node *head,int num) {node *P0,*P1,*P2 in a single linked list;
  P1=head;
  p0= (node *) malloc (sizeof (node));
  p0->data=num;
  while (p0->data>p1->data&&p1->next!=null) {p2=p1;p1=p1->next;
      } if (P0->data<=p1->data) {if (HEAD==P1) {p0->next=p1;
    Head=p0;
      else {p2->next=p0;
    p0->next=p1;
    } else {p1->next=p0;
  p0->next=null;
return head;
  //single linked list from small to large sort: node *sort (node *head) {node *p;
  int N;int temp;
  N=length (head); if (head==null| |
  Head->next==null) return head;
  P=head;
    for (int j=1;j<n;++j) {p=head;
        for (int i=0;i<n-j;++i) {if (p->data>p->next->data) {temp=p->data;
        p->data=p->next->data;
      p->next->data=temp;
    } p=p->next;
} return head;
  
  ///Inverse node *reverse (node *head) {node *p1,*p2,*p3 for a single linked list; if (head==null| |
  Head->next==null) return head;
  p1=head,p2=p1->next;
    while (p2) {p3=p2->next;
    p2->next=p1;
    P1=P2;
  P2=P3;
  } head->next=null;
  HEAD=P1;
return head;
  int main () {node *head;

  int n,del_num,insert_num;
  Head=creat ();

  Print (head);
  cout<< "\ n the length of a single linked list:";
  N=length (head);
  
  cout<<n<<endl;
  cout<< "\ n Please sort the single linked list:";
  Head=sort (head);

  Print (head);
  cout<< "\ n Please enter the data to be deleted in a single list:";
  cin>>del_num;
  Head=del (Head,del_num);

  Print (head);
  cout<< "\ n Please enter the data to be inserted in a single list:"; Cin>>insert_Num
  Head=insert (Head,insert_num);

  Print (head);
  cout<< "\ n Please reverse the single linked list:";
  Head=reverse (head);

  Print (head);
return 0; }

Run Result:

Summary: The basic operation of a single linked list, including the establishment of a single linked list, the single chain to find the length of the list, print out a single linked list, the single linked list to sort, delete elements, insert elements, the single linked list inversion. These operations are the programmer must master, although it is a relatively simple data structure, but to be able to master the use of it will take time.

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.