题意:根据题意,意思就是实现插入,删除,展示,以及得到元素,并判断是否删除加入成功以及表内元素是否为空。
思路:一开始觉得跟昨天那个题一样,只不过加上一些判断,所以就着手用数组来写了!但是TLE了 重要的事情说三遍,数组写的不对,下面链表那个才对!
有想理解下数组写法的思路的可以看看代码如下:
#include<bits/stdc++.h>
#define maxn 10010
using namespace std;
int a[maxn];
int b[maxn];
int main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=n;i++){
b[i] = a[n-i+1];//我们先将其倒过来
}
int nn;
cin>>nn;
string s;
int pos;
int num = n;//初始的时候等于n
for(int i=1;i<=nn;i++){
cin>>s;
if(s == "show"){
if(num == 0){
cout<<"Link list is empty"<<endl;
}
else{
for(int i=1;i<=num;i++){
cout<<b[i]<<" ";
}
cout<<endl;
}
}
else if(s == "delete"){
cin>>pos;
if(num == 0){
cout<<"Link list is empty"<<endl;
}
else{
if(num >= pos){
cout<<"delete OK"<<endl;
for(int i=pos;i<num;i++){
b[i] = b[i+1];
}
num--;
}
else cout<<"delete fail"<<endl;
}
}
else if(s == "insert"){
cin>>pos;
int po;
cin>>po;
if(pos <= num){
cout<<"insert OK"<<endl;
for(int i=num;i>=pos;i--){
b[i+1] = b[i];
}
b[pos] = po;
num++;
}
else{
cout<<"insert fail"<<endl;
}
}
else if(s == "get"){
cin>>pos;
if(num < pos) cout<<"get fail"<<endl;
else
cout<<b[pos]<<endl;
}
}
return 0;
}
那么下面是正确的链表解法:
#include<bits/stdc++.h>
typedef struct node_ {
int data;
struct node_ * next;
}*node, Node;
node creat_list( int *Count );
void get_( node L, int a );
void insert_( node L, int a, int e, int *Count );
void delete_( node L, int a, int *Count );
void show_( node L );
int main()
{
char order[7];
int n, a, e, Count;
node L = creat_list( &Count );
scanf( "%d", &n );
for ( int i = 0; i < n; i++ )
{
scanf( "%s", order );
if ( !strcmp( order, "get" ) )
{
scanf( "%d", &a );
/*判断查找位置是否合法,*/
if ( a > Count || a < 1 )
printf( "get fail\n" );
else
get_( L, a );
}else
if ( !strcmp( order, "insert" ) )
{
scanf( "%d%d", &a, &e );
/*判断插入位置是否合法,若a=Count+1,表示插入在链表尾部*/
if ( a > Count + 1 || a < 1 )
printf( "insert fail\n" );
else
insert_( L, a, e, &Count );
}else
if ( !strcmp( order, "delete" ) )
{
scanf( "%d", &a );
/*判断删除位置是否合法*/
if ( a > Count || a < 1 )
printf( "delete fail\n" );
else
delete_( L, a, &Count );
}else
if ( !strcmp( order, "show" ) )
{
show_( L );
}
}
return(0);
}
/*============================================*/
node creat_list( int *Count )
{
int n, e;
node p;
scanf( "%d", &n );
(*Count) = n;
/*头结点*/
node head = (node) malloc( sizeof(Node) );
head->next = NULL;
for ( int i = 0; i < n; i++ )
{
scanf( "%d", &e );
p = (node) malloc( sizeof(Node) );
/*前插*/
p->data = e;
p->next = head->next;
head->next = p;
}
/*返回指向头结点的指针*/
return(head);
}
/*============================================*/
void get_( node L, int a )
{
node p = L;
for ( int i = 0; i < a; i++ )
p = p->next;
printf( "%d\n", p->data );
}
/*============================================*/
void insert_( node L, int a, int e, int *Count )
{
node p = L;
/*找到第a个位置之前的结点即第a-1个结点*/
for ( int i = 1; i < a; i++ ) /* for(int i=0;i<a-1;i++) */
p = p->next;
node q = (node) malloc( sizeof(Node) );
q->data = e;
/*插入结点*/
q->next = p->next;
p->next = q;
/*结点数加1*/
(*Count)++;
printf( "insert OK\n" );
}
/*============================================*/
void delete_( node L, int a, int *Count )
{
node d, p = L;
/*找到第a个位置之前的结点即第a-1个结点*/
for ( int i = 0; i < a - 1; i++ )
p = p->next;
/*删除结点*/
d = p->next;
p->next = d->next;
free( d );
/*结点数减去1*/
(*Count)--;
printf( "delete OK\n" );
}
/*============================================*/
void show_( node L )
{
node p = L->next;
if ( p == NULL )
printf( "Link list is empty\n" );
else{
while ( p != NULL )
{
printf( "%d ", p->data );
p = p->next;
}
printf( "\n" );
}
}