Today to write 5 data structure algorithm of the problem ... A few questions that feel very valuable. Be interested to sit down.

Source: Internet
Author: User
Tags clear screen
1. To judge whether a sequential table is symmetric 2 using vector as storage structure, the algorithm is designed to use only one auxiliary node to realize the operation of the node loop to the right K-bit in the linear table 3. The elements in a "n" are known to be shaped. The design algorithm adjusts it to both the left and right parts. All the elements on the left are odd, all the elements on the right are even, 4, design an algorithm, reverse the lead node dynamic chain table L, 5 single linked list in the order of non descending order, design algorithm to delete the same value in a single list of redundant nodes, 6 false with two by the element value of the orderly order of linear table A and B, all with a single linked table as storage structure , is to write an algorithm to merge a table and B table into a descending order by element value of the linear table C, and require the use of the original table node space to store c I wrote the code below 1, *
Requirements: To determine whether a sequential table is symmetric
*/
#include <iostream>
using namespace Std;
#define MAX_SIZE 200
Template <class t>
BOOL Issymmetrical (T list[],int length); Use templates to compare any type of sequential table
void Main ()
{
int group= 0;
BOOL *pgroup=null;//saved is the result of the comparison
int *pnum =null; This is an example of a plastic sequence table
int ncount=0;//number of data per group
cout<< "Please enter a few sets of data to compare:" &LT;&LT;ENDL;
cin>>group;
Pgroup=new Bool[group]; Dynamically allocating array memory to save symmetric comparison results
for (int n=0;n<group;n++)//loops four times each time is a comparison result
{
Cin>>ncount;
Pnum=new Int[ncount]; Dynamically allocating memory for each row of data
for (int m=0;m<ncount;m++)//To assign to an array just allocated
cin>>pnum[m];
Pgroup[n]=issymmetrical (Pnum,ncount); To save the bool value of the function return result directly in the dynamically allocated array
delete []pnum;//first set of memory use complete release to prevent memory leaks
}
for (n=0;n<group;n++)//output symmetric y N
{
if (Pgroup[n])
cout<< "Y" &LT;&LT;ENDL;
Else
cout<< "N" &LT;&LT;ENDL;
}
delete []pgroup; Finally releasing heap memory again
}
Template <class t>
BOOL Issymmetrical (T list[],int length)//using templates to compare any type of sequential table
{
int l=0;
for (int n=0;n<=length-1;n++)
if (List[n]==list[length-1-n])
l++;
if (l==length)
return true;
return false;
} 2,

/*
Requirements: Using vector as the storage structure, the algorithm is designed to use only one auxiliary node to realize the operation of the node cyclic right shift K position in the linear table.

Vector dynamic Array in the STL

Time reason here I've only got a linear table to loop the move algorithm in the following annotation section needs to be like the first question multiple sets of input, only need to put the entire implementation part in a loop to implement can
*/
#include <iostream>
#include <vector>
using namespace Std;
#include "Windows.h"
struct node//node data
{
int data;
};
void Main ()
{
Vector<node> List; The definition of a vector
int size;//Receive input size
cout<< "Please enter the number of initial data in the table:" &LT;&LT;ENDL;
Cin>>size;
Node *parray=null;//Accept input data
Parray=new Node[size];
for (int n=0;n<size;n++)
{
cout<< "Please enter the data of the" <<n+1<< "node and enter a carriage return end input at a time:" &LT;&LT;ENDL;
Cin>>parray[n].data;
List.push_back (Parray[n]); Inserting data
}
Vector<node>::iterator P=list.begin ()//return iterator we can access the vector through the iterator
System ("CLS");/clear Screen
cout<< "The data in the initial table is as follows:" <<endl;
for (n=0;n<size;n++)//through an iterator to enter data in a table
cout<<p[n].data<< "";
Cout<<endl;
cout<< "Data Entry complete please enter the position to move to the right K:" &LT;&LT;ENDL;
int move; The position to move
Cin>>move;
Move=move%size; Take the mold
Node tem;
List.insert (P,move,tem); Insert 5 temporary nodes in front
P=list.begin (); Iterate to the first node
Algorithm implementation part
Size=list.size (); int m=0;
For (N=size-move, m=0;n<size;n++,m++)//assigning values to inserted nodes
P[m]=p[n];
for (n=0;n<move;n++)
List.pop_back ();//pop the tail element
///////////////////////////////////
P=list.begin ();
cout<< "Right-move element position:" <<endl;
for (n=0;n<size;n++)
cout<<p[n].data<< "";
cout<<endl;

} 3,

/*
3. The elements in a "n" are known to be cosmetic. The design algorithm adjusts it to both the left and right parts. All elements on the left are odd, all elements on the right are even,
Operation Example
Number of groups 2
First Group 5 (number) 1 2 3 4 5
Results 1 3 5 2 4 then continue to enter the next group as prompted

*/
#include <iostream>
using namespace Std;
#define MAX_SIZE 100
void SetArray (int arr[],int length);//Algorithm implementation function
void Main ()
{

cout<< "\t\t\t Operation Example" <<endl<< "\t\t\t Group number 2--> carriage return" <<endl<< "\t\t\t First Group 5 1 2 3 4 5--> back Car End "<<endl;
cout<< "\t\t\t Results Output 1 3 5 2 4--> display results then continue to enter the next group" &LT;&LT;ENDL;
int *parray=null;//Dynamic Array pointer
int ncount=0;//the number of arrays per row
int group=0;
cout<< "Please enter the number of groups:" <<endl;
cin>>group;
for (int i=0;i<group;i++)
{
cout<< "Please enter" << "<<i+1<<" group number and data such as: 5 1 2 3 4 5 "&LT;&LT;ENDL;
cin>>ncount; Number of inputs
Parray=new Int[ncount];
for (int n=0;n<ncount;n++)
cin>>parray[n];
SetArray (Parray,ncount); Call the setting function to change odd even position
for (n=0;n<ncount;n++)
cout<<parray[n]<< "";
Cout<<endl;
delete []parray; Freeing memory in the heap
}


}

void SetArray (int arr[],int length)//Algorithm implementation function
{
Char Buf1[max_size];
Char Buf2[max_size];
memset (BUF1, ' * ', max_size); Store Odd
memset (buf2, ' * ', max_size);//Storing even numbers
Buf2[max_size-1]= ' ";
Buf1[max_size-1]= ' "; Prevent Cross-border
for (int n=0,m=0,i=0;n<length;n++)
{
if (arr[n]%2==0)
{
buf2[m]= ' 0 ' +arr[n]; Save even ASC code
m++;

}
Else
{
buf1[i]= ' 0 ' +arr[n];//save odd ASC code
i++;
}
}
for (n=0,m=0,i=0;n<length;n++)
{
if (buf1[m]!= ' * ')
{
arr[n]=buf1[m]-' 0 ';
m++;

}
Else
{
arr[n]=buf2[i]-' 0 ';
i++;
}
}
} 4,

/*
, design an algorithm to reverse the lead node dynamic chain table L,
Create a linked list and then reverse the sequence
*/
#include <iostream>
using namespace Std;
typedef struct node//linked list nodes
{
int data;
struct Node*next;
}*head,*linknode;
Head head;//Node
int length=0;
Head creatlinklist (); Custom lead linked list creation function
void Reserve (head head); The realization of inverse sequence algorithm is to realize the reverse sequence of data
Void Show (head head);//Display function
void Main ()
{
int group;
cout<< "Please enter the number of list groups to reverse order" &LT;&LT;ENDL;
Cin>>group;
Head *plink=new Head[group]; Save the header of a linked table
cout<< "Enter the data in the list, separate the spaces between the data, end with 66666 input for example: 1 2 3 4 5 66666-> carriage return" <<endl;
for (int n=0;n<group;n++)
{
Head=creatlinklist ()///Create a linked list
Reserve (head);
Plink[n]=head;
length = 0;
}
cout<< after "reverse sequence:" <<endl;
cout<< "<------------------------->" &LT;&LT;ENDL;
for (n=0;n<group;n++)
Show (Plink[n]);
cout<< "<------------------------->" &LT;&LT;ENDL;


}
The implementation of void Reserve (head head)//inverse sequence algorithm is to realize the reverse sequence of data
{
int COUNT=LENGTH/2; Number of comparisons
Linknode P1=head,p2=head;
int tem;
for (int n=0;n<count;n++)
{
for (int m=0;m<length-n-1;m++)
P2=p2->next;
Tem=p1->data;
P1->data=p2->data;
P2->data=tem;
P1=p1->next;
P2=head;
}
}
Void Show (head Head)//Display function
{
while (Head!=null)
{
cout<head=head->next;
}
Cout<<endl;
}
Head creatlinklist ()//custom lead linked list creation function
{
Linknode P1=null,p2=null,head =null;
P1=new Node;
HEAD=P1;
P2=P1;
length++;
Cin>>p1->data;
if (p1->data==66666)
{
Delete P1;  P1=null; P2=null;
length--;
return NULL;
}
while (p1->data!=66666)
{
P2=P1;
P1=new Node;
length++;
Cin>>p1->data;
if (p1->data==66666)
{
Delete P1;  P1=null; p2->next=null;
length--;
return head;
}
P2-&GT;NEXT=P1;
}

return head;
}
5,

/*
The single linked list is arranged in a non descending order, and the design algorithm realizes the deletion of redundant nodes with the same value in the single linked list.
The implementation of the algorithm is to use 2 loops traversal link list algorithm implementation in the next annotation section
*/
#include <iostream>
using namespace Std;
typedef struct node//linked list nodes
{
int data;
struct Node*next;
}*head,*linknode;
Head heads;//Header nodes
Head creatlinklist ()//custom lead linked list creation function
void Delete (head head); Delete the duplicate node algorithm implementation section ....
Void Show (head head);//Display function
void Main ()
{
Head=creatlinklist ();
System ("CLS");
cout<< "The data in the table is as follows:" <<endl;
Show (head);
Delete (head); Delete duplicate nodes
cout<< "Delete the linked list after the duplicate node:" <<endl;
Show (head);
}

Head creatlinklist ()//custom lead linked list creation function
{
Linknode P1=null,p2=null,head =null;
P1=new Node;
HEAD=P1;
P2=P1;
cout<< "Please enter the data of the linked list node, the data between the blanks enter 66666 end input for example 1 2 3 4 5 66666" <<endl;
Cin>>p1->data;
if (p1->data==66666)
{
Delete P1;  P1=null; P2=null;
return NULL;
}
while (p1->data!=66666)
{
P2=P1;
P1=new Node;
Cin>>p1->data;
if (p1->data==66666)
{
Delete P1;  P1=null; p2->next=null;
return head;
}
P2-&GT;NEXT=P1;
}
return head;
}
/////////////////////////////////////////////////////////////////////////////////
void Delete (head head)//delete duplicate node algorithm implementation section ....
{
Head P1=head,p2=head;
Linknode Tem=null; Temporary node pointer
while (P1!=null)
{
while (P2->next!=null)//When P2 next arrives at the end of the launch, the following p2->next->next will collapse.
{
if (P2->next->data==p1->data)
{
Tem=p2->next;
P2->next=p2->next->next;
Delete tem;
Continue;
}
Else
P2=p2->next;
}
p1=p1->next;
P2=P1;
}
}
///////////////////////////////////////////////////////////////////////////////////
Void Show (head Head)//Display function
{
while (Head!=null)
{
cout<head=head->next;
}
Cout<<endl;
} 6,/*
6 It is assumed that there are two linear tables, A and B, that are sequentially ordered by element values, and are stored in a single linked table,
is to write an algorithm to merge a table and B table into a descending order by element value of the linear table C, and require the use of the original table node space to store C
<---------Be sure to enter data incrementally when entering data------>
*/
#include <iostream>
using namespace Std;
typedef struct node//linked list nodes
{
int data;
struct Node*next;
}*head,*linknode;
Linknode A =null;//Table 1
Linknode B =null;//Table 2
Linknode C=null; Table 3
int Getmax (head head); Returns the largest data in a linked list
Head creatlinklist ()//custom lead linked list creation function
Head combinelinklist (head a,head B); The implementation part of the merging algorithm
Void Show (head head);//Display function
void Main ()
{
cout<< "Table A:" <<endl;
A=creatlinklist (); CREATE Table A
cout<< "Table B:" <<endl;
B=creatlinklist (); CREATE TABLE B
C=combinelinklist (A,B);
Show (C);
}
The implementation part of the head combinelinklist (Head a,head B)//Merging algorithm
{

Linknode Tem=a;
while (Tem->next!=null)
{
tem=tem->next;
}
Tem->next=b;
C=a//a B table joins then descending sort
Linknode P1=c;
Linknode p2=c->next;
int inter=c->data;//temporary variable for Exchange
while (P1->next!=null)
{
while (P2!=null)
{
if (p2->data>p1->data)//Exchange data
{
Inter=p1->data;
p1->data=p2->data;
P2->data=inter;
}
P2=p2->next;
}
p1=p1->next;
p2=p1->next;
}
return C;
}
Void Show (head Head)//Display list function
{
while (Head!=null)
{
cout<head=head->next;
}
Cout<<endl;
}
int Getmax (head head)//return the maximum data in the list
{
while (Head->next!=null)
Head=head->next;
Return head->data;
}
Head creatlinklist ()//custom lead linked list creation function
{
Linknode P1=null,p2=null,head =null;
P1=new Node;
HEAD=P1;
P2=P1;
cout<< "Please enter the data of the linked list node, the data between the blanks enter 66666 end input for example 1 2 3 4 5 66666" <<endl;
Cin>>p1->data;
if (p1->data==66666)
{
Delete P1;  P1=null; P2=null;
return NULL;
}
while (p1->data!=66666)
{
P2=P1;
P1=new Node;
Cin>>p1->data;
if (p1->data==66666)
{
Delete P1;  P1=null; p2->next=null;
return head;
}
P2-&GT;NEXT=P1;
}
return head;
}

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.