代码如下:
#include <iostream>
using namespace std;
typedef struct _stnode
{
int data;
struct _stnode* next;
}StNode;
int main()
{
int i, n, pos;
StNode* head = new StNode;
head->next = NULL;
StNode* p = head;
StNode* t = 0;
cin >> n; //输入个数
for (i = 0; i < n; i++)
{
t = new StNode;
cin >> t->data;
t->next = NULL;
p->next = t;
p = t;
}
//输入需要删除的位置
cin >> pos;
StNode* front = head;
p = front->next;
i = 1;
while (p && i < pos)
{
front = p;
p = p->next;
i++;
}
if (p == NULL)
cout << "error";
else
{
front->next = p->next;
delete p;
}
//遍历输出
p = head->next;
if (p == NULL)
{
cout << "null";
return 0;
}
int flag = 0;
while (p)
{
if (flag == 0)
{
cout << p->data;
flag = 1;
}
else
cout << " " << p->data;
p = p->next;
}
return 0;
}
#include <iostream>
using namespace std;
typedef struct _linknode
{
int data;
struct _linknode *next;
}linknode,*linklist;
void createNode(linklist &head)
{
head = new linknode();
linklist p = head;
int n;
cin>>n;
for(int i=0;i<n;i++)
{
linklist q = new linknode();
cin>>q->data;
q->next = NULL;
p->next = q;
p = q;
}
}
void deleteNode(linklist head,int index)
{
linklist p = head;
int i=1;
while(p->next != NULL && i<index)
{
p = p->next;
i++;
}
if(p->next != NULL)
{
linklist q = p->next;
p->next = p->next->next;
delete q;
}
}
void print(linklist head)
{
if(head->next == NULL)
cout<<"null";
else
{
linklist p = head->next;
while(p != NULL)
{
cout<<p->data<<" ";
p = p->next;
}
}
}
void main()
{
linklist head = NULL;
createNode(head);
int idx;
cin>>idx;
deleteNode(head,idx);
print(head);
return 0;
}
这题很简单,C++提供了list、map等容器,里面有排序的功能;只要把数字插入进去就可以了!谢谢,望采纳!