B - 链表基本操作
Description
这个链表共有五种操作
Input
loc 为正数,x为一整型数,
"insert loc x"表示将整数x插入链表作为链表第loc个数据
"erase loc"表示删除链表的第loc个数据(测试数据保证loc小于链表的结点总数)
"output"表示输出链表所有的数据,数据间用空格隔开
"find x"表示在链表中查找值为x的数据,若查找到则输出"hava found",没找到输出"not found"
"end"表示程序结束
"insert loc x"表示将整数x插入链表作为链表第loc个数据
"erase loc"表示删除链表的第loc个数据(测试数据保证loc小于链表的结点总数)
"output"表示输出链表所有的数据,数据间用空格隔开
"find x"表示在链表中查找值为x的数据,若查找到则输出"hava found",没找到输出"not found"
"end"表示程序结束
Output
按要求输出
Sample Input
insert 1 2insert 2 6insert 3 5find 6outputerase 2find 6outputinsert 3 10outputinsert 2 11outputend
Sample Output
have found 2 6 5 not found 2 5 2 5 10 2 11 5 10
// File Name: 链表的基本操作.cpp
// Author: rudolf
// Created Time: 2013年03月12日 星期二 16时18分17秒
#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
using namespace std;
typedef int datatype;
typedef struct slonode
{
int data;
struct slonode *next;
}slonode,*linklist;
////////////
linklist creatlist(linklist head)
{
head=(linklist)malloc(sizeof(slonode));
head->next=NULL;
return head;
}
/////////////////////////
void insertlist(linklist head,int i,datatype x)
{
linklist q,s;
q=head;
int j=0;
while(q->next!=NULL&&j<i-1)
{
q=q->next;
j++;
}
s=(linklist)malloc(sizeof(slonode));
s->data=x;
s->next=q->next;
q->next=s;
}
////////////
linklist deletelist(linklist head,int i)
{
linklist q,p;
q=head;
int j=0;
while(q->next!=NULL&&j<i-1)
{
q=q->next;
j++;
}
p=q->next;
q->next=p->next;
free(p);
}
///////////////
void print(linklist head)
{
linklist q;
q=head;
while(q->next!=NULL)
{
cout<<q->next->data<<' ';
q=q->next;
}
//cout<<q->next->data;
cout<<endl;
}
/////////////////
void getlist(linklist head,datatype x)
{
linklist q;
q=head;
int flag=0;
while(q->next!=NULL)
{
if(q->next->data==x)
{
break;
// flag=1;
// cout<<"hava found"<<' ';
}
q=q->next;
// flag=1;
// cout<<"not found"<<endl;
}
if(!q->next)
cout<<"not found "<<endl;
else
cout<<"have found "<<endl;
}
////////////
int main()
{
int loc,x;
linklist head,p,q;
char str[100];
head=(linklist)malloc(sizeof(slonode));
head=creatlist(head);
while(scanf("%s",str)!=EOF)
{
if(!strcmp(str,"insert"))
{
cin>>loc>>x;
insertlist(head,loc,x);
// print(head);
}
if(!strcmp(str,"erase"))
{
cin>>loc;
deletelist(head,loc);
}
if(!strcmp(str,"output"))
{
print(head);
}
if(!strcmp(str,"find"))
{
cin>>x;
getlist(head,x);
}
if(!strcmp(str,"end"))
{
break;
}
}
return 0;
}
浙公网安备 33010602011771号