Experiment - Relevant operation of linear table

Experiment  - Relevant operation of linear table

1. Experiment hours: 2 hours

Second, background knowledge: insertion, deletion and application of singly linked list.

3. Purpose Requirements:

1. Master the storage characteristics and implementation of singly linked list.

2. Understand and master the type definition method and node generation method of singly linked list.

3. Master the insertion and deletion algorithm of singly linked list and the program realization of its application algorithm.

4. Experiment content:

1. Randomly generate or input a set of elements from the keyboard to create a singly linked list (unordered) with a head node.

2. Traverse the singly linked list (display).

3. Reverse the elements in the singly linked list (not allowed to apply for a new node space).

4. Delete all even-numbered elements (even-numbered) nodes in the singly linked list.

5. Write a function that inserts an element into a non-decreasing ordered linked list to keep the elements of the linked list in order, and use this function to build a non-decreasing ordered singly linked list.

6. Use Algorithm 5 to establish two non-decreasing ordered singly linked lists, and then merge them into one non-increasing linked list.

7. Use Algorithm 5 to establish two non-decreasing ordered singly linked lists, and then merge them into one non-decreasing linked list.

8. Write a main function to debug the above algorithm.

5. Experiment Description:

1. Type definition

typedefint  ElemType ; // element type

typedef  struct node

{ElemTypedata

structnode *next

}LNode,*LinkList

2. In order to simplify the implementation of the algorithm, it is best to use a singly linked list with a head node.

Six, pay attention to the problem:

1. Focus on understanding the characteristics of chain storage and the meaning of pointers.

2. Pay attention to the respective characteristics of sequential storage and chain storage.

3. Pay attention to the difference between the headed node and the headless node linked list when implementing the insertion and deletion algorithms.

4. The operation of the singly linked list is the basis of the data structure, and we must pay attention to the understanding of the common algorithms in this part.

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef int ElemType;
  typedef struct node
  {
      int data;
      struct node *next;
  } LNode,*LinkList;
   void  init(LinkList &L)
  {
      L=(LinkList)malloc(sizeof(LNode));
      L->next=NULL;
  }
 void createlist(LinkList &L,int n)
 {
     LinkList p;
     L=(LinkList)malloc(sizeof(LNode));
     L->next=NULL;
     for(int i=0;i<n;i++)
     { p=(LinkList)malloc(sizeof(LNode));
        cin>>p->data;
        p->next=L->next;L->next=p;
     }
 }
 void outputlist(LinkList &L)
 {  LinkList p;
    p=L->next;
     while(p)
    {
        printf("%d  ",p->data);
        p=p->next;
    }
    printf("\n");
  printf("        显示完成!!!!!!\n");
 }
//void converse(LinkList head)
//{
//    LinkList p,q;
//    p=head->next;
//    head->next=NULL;
//    while(p)
//    {
//        /*向后挪动一个位置*/
//        q=p;
//        p=p->next;
//
//        /*头插*/
//        q->next=head->next;
//        head->next=q;
//    }
//}
void Reverse(LinkList &L)
{
    LinkList last = L->next;
    LinkList first;
    while(last->next)
    {
        first = L->next;
        L->next = last->next;
        last->next = L->next->next;
        L->next->next = first;
    }
}
void shanou(LinkList head)
{
LinkList p,q;
q=head;
p=head->next;
while(p)
{
if((p->data%2!=0))

{
 p=p->next;
q=q->next;
}
else
{q->next=p->next;
p=p->next;

}
}
printf("The linked list after shanou is:\n");

p=head->next;
while(p)
{printf("%3d",p->data);

p=p->next;

}
printf("\n");
}
void Srot(LinkList L) // sort the data non-decreasingly

{
    LinkList p,q,T;
    ElemType m;
    int chan=-1;
    p = L->next;
    for(;p!=NULL;p=p->next)
    {
        T = p;
        for(q=p->next;q!=NULL;q=q->next)
        {
            if((q->data)<(T->data))
             T = q;
        }
        m = T->data;
        T->data = p->data;
        p->data = m;
    }
   printf("排序完成!\n"); }
void ListInsert(LinkList &L1,ElemType e)  //插入数据;
{  //  init(L);
    LinkList p,q,T;
    //int chan = -1;
    //Srot(L);
    T=(LinkList)malloc(sizeof(LNode)); //Create an inserted node;

    T->data = e;
    p = L1;
    q = p->next;
    while(q)
    {
//        if(q->data>e||q->data==e)
//            break;
//        p = p->next;
//        q = q->next;
    q=q->next;
    }
    q->next=T;
    T->next=NULL;
    Srot(L1);
   // T->next = q;
   // p->next = T;
//    printf("插入完成!\n");
//    printf("按“1”输出:");
//  scanf("%d",&chan);
//   if(chan)
//    outputlist(L);
}
//void insert_sert(LinkList &L,int i)
//{
//    LinkList s;
//    s=(LinkList)malloc(sizeof(LNode));
//    s->data=i;
//    static int n=0;
//    int m=0;
//    LinkList p,q;
//    p=L;
////    cout<<n<<endl;
////    Sleep(1000);
//    if(n==0)
//    {
//        s->next=NULL;
//        L->next=s;
//        n++;
//    }
//   else
//    {
//    while(p->next!=NULL)
//    {
//        q=p->next;
//        if(s->data<q->data)
//        {
//            s->next=q;
//            p->next=s;
//            m=1;
//            break;
//        }
//        p=p->next;
//    }
//    if(m==0)
//    {
//    //s->next=NULL;
//    p->next=s;
//    }
//    }
//
//}
void DecreasePro_L(LinkList &La,LinkList &Lb)
{
    LinkList pa,pb,pc,Lc;
     Reverse(La);
    Reverse(Lb);
     Lc = pc = La;
    pa = La->next;
    pb = Lb->next;
    while(pa&&pb)
    {
        if((pa->data)>(pb->data)||(pa->data)==(pb->data))
        {   pc->next = pa;
            pc = pa;
            pa = pa->next;
        }else{
            pc->next = pb;
            pc = pb;
            pb = pb->next;
        }
    }
       if(pa==NULL)
        {
            pc->next = pb;
        }
        if(pb==NULL)
        {
            pc->next = pa;
        }
     La = Lc;
     free(Lb);
     printf("Non-incremental merge complete!\n");
}
void surface(){
    int i;
    printf(" ");
    for(i=1;i<=47;i++)
    printf("%c",4);
    printf("\n");
    printf(" | %c%c%c%c Linear table related operations%c%c%c%c|\n",3,4,5,6,3,4,5,6);
    printf(" |%c 1 . Input data to generate an unordered linked list 6. Synthesize an increasing linked list%c|\n",5,5);
    printf(" |%c 2. Insert elements to generate an incremental linked list 7. Traverse a singly linked list (display) %c|\ n",5,5);
    printf(" |%c 3. The elements in the singly linked list are reversed in place 8. %c|\n",5,5);
    printf(" |%c 4. The singly linked list Delete all even-numbered elements
    in
    printf("              ");
    for(i=1;i<=47;i++)
    printf("%c",4);
    printf("\n");
    printf("                                                                 ------王祥\n");
}
int main()
{
int num;
   LinkList L1;
while(1){
      surface();
      int i;
      printf("Please input a number from 1 to 10 to realize its corresponding function:\n");
      for(i=1;i<=37;i++)
      printf("%c",4);
      printf("\ n");
      cin>>num;
      if(num==10){
      printf("%c%c%cThanks for your use!%c%c%c\n",1,1,1,1,1 ,1);
      for(i=1;i<=37;i++)
      printf("%c",4);
      printf("\n");
      break;
      }
      if(num<=0||num>10) {
      printf("x_x please input x_x\n" in the correct format);
      for(i=1;i<=37;i++)
      printf("%c",4);
      printf("\n");
      continue;
      }
     if(num==1){
     printf("Please enter the length of the linked list\n");
      int n;
     cin>>n;
  createlist(L1,n);
     }
     if(num==7){
     outputlist(L1);
     }
    if(num==2){
     cout<<"Please enter the number of data"<<endl;
                    int i,j,a;
                    cin>>i;
                      createlist(L1,i);
                      Srot(L1);

    }
  if(num==3){
  Reverse(L1);
  outputlist(L1);
  cout<<"Complete in-place inversion"<<endl;
  }
 if(num==4){
 shanou(L1);
   cout<<"删偶完成!!!"<<endl;
 }
 if(num==6){
  LinkList L2;
  int q,w;
  printf("Please enter the length of the first linked list\n");
  scanf("%d",&q);
  createlist(L1,q);
  printf("Please enter the second linked list length of \n");
   scanf("%d",&w);
   createlist(L2,w);
   DecreasePro_L(L1,L2);
   Srot(L1);
    outputlist(L1);
 }
 if(num==5){
  LinkList L2;
  int q,w;
  printf("Please enter the length of the first linked list\n");
  scanf("%d",&q);
  createlist(L1,q);
  printf("Please enter the second linked list length of \n");
   scanf("%d",&w);
   createlist(L2,w);
   DecreasePro_L(L1,L2);
   Srot(L1);
   Reverse(L1);
    outputlist(L1);
 }
}
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324622755&siteId=291194637