8 solutions
-
7
锻炼还是要有的
#include <bits/stdc++.h> using namespace std; struct Node{ int data; Node *next; }; Node *head,*p,*r,*s; int idx = 0; int main(int argc, char **argv){ int n,x,a;bool f = 1; head = new Node; r = head; cin >> n; for (int i = 0;i < n;i++){ cin >> a; p = new Node; p -> data = a; p -> next = NULL; r -> next = p; r = p; } p = head -> next; cin >> x; if(x == 1){ p = head -> next -> next; for (int i = 0;i < n - 1;i++){ cout << p -> data << ' '; p = p -> next; } // cout << p -> data; return 0; } while (idx < x - 2){ p = p -> next; idx++; } if(p -> next == NULL){ f = 0; p = head -> next; }else{ s = p -> next; p -> next = p -> next -> next; delete s; p = head -> next; } while (p -> next != NULL){ cout << p -> data << ' '; p = p -> next; } if (f) cout << p -> data; return 0; }
花了好久才做出来,点个赞吧❤
-
1
思路(乱七八糟)
用链表存一个数组,删除一个内容(欸,数组......)
用一个struct存节点,插入节点后,由于1和2比较特殊(1:为头,2:头后第一个,在for循环中判断不到),进行特判。呆码
#include<bits/stdc++.h> using namespace std; struct Node{ int n; Node* next; }*front=new Node; int main(){ int n; cin>>n; Node *x,*p=NULL; x=front; for(int i=0;i<n;i++){ p=new Node; int www; cin>>www; p->n=www; p->next=NULL; x->next=p; x=p; } int c; cin>>c; if(c==1){//1的特判 Node* a=front->next->next; for(int i=2;i<=n;i++){ cout<<a->n<<" "; a=a->next; } return 0; } if(c==2){//2的特判 Node* a=front->next; cout<<a->n<<" "; a=a->next; a=a->next; for(int i=3;i<=n;i++){ cout<<a->n<<" "; a=a->next; } return 0; } Node* a=front->next; for(int i=1;i<n;i++){//其他情况 cout<<a->n<<" "; a=a->next; if(i==c-2)a->next=a->next->next; } }
完成后突然发现,h1表体好像有些太显眼......
幼稚题解,必须点赞 -
0
非常链表
#include <iostream> #include <cstdio> using namespace std; int n,m,cnt,x,index; struct Node{ int next;//下一个结点 int mine;//结点自己 }arr[11]; int main(){ scanf("%d",&n); for(int i = 1;i <= n;i++){ scanf("%d",&cnt); arr[i].mine = cnt; arr[i - 1].next = i;//标记下一个 } scanf("%d",&m);//输入删除位置 arr[m - 1].next++;//更改指针 for(int i = 1;i < n;i++){ int index = arr[x].next;//指针为下一个结点 printf("%d ",arr[index].mine);//输出下一个 x = index; } return 0; }
-
-1
#include<bits/stdc++.h> using namespace std; int n,x,c; struct Node{ int data; Node* next; }; int main(){ Node* head; cin >>n; Node* ptr=new Node; head=ptr; for(int i=1;i<=n;i++){ ptr->next=new Node; ptr=ptr->next; cin >>c; ptr->data=c; ptr->next=NULL; } cin>>x; ptr=head; for(int i=1;i<=x-1;i++){ ptr=ptr->next; } ptr->next=ptr->next->next; ptr=head->next; while(ptr!=NULL){ cout <<ptr->data<<" "; ptr=ptr->next; } return 0;//别抄我的QAQ }
-
-2
``` #include <iostream> using namespace std; int main() { int n,m; cin>>n; int a[n+1]; for(int i=1;i<=n;i++) { cin>>a[i]; } cin>>m; int b[n+1]; for(int i=1;i<=n;i++) { if(i!=m) b[i]=a[i]; else break; } for(int i=m;i<n;i++) { b[i]=a[i+1]; } for(int i=1;i<n;i++) { cout<<b[i]<<" "; } }
``` ```
- 1
Information
- ID
- 226
- Time
- 1000ms
- Memory
- 64MiB
- Difficulty
- 7
- Tags
- # Submissions
- 164
- Accepted
- 36
- Uploaded By