半条命16吧 关注:393贴子:26,042
  • 0回复贴,共1

链表元素左移

只看楼主收藏回复

#include <iostream>
using namespace std;
typedef struct LNode {
int data;
struct LNode *next;
}LNode,*Linklist;
Linklist Create() {
Linklist head = (Linklist)malloc(sizeof(struct LNode));
head->data = 0;
head->next = NULL;
return head;
}
Linklist Insert(Linklist& L) {
int x;
cout << "输入想要插入的数据,9999结束输入" << endl;
L = (Linklist)malloc(sizeof(LNode));
LNode* s, * r = L;
cin >> x;
while (x != 9999) {
s = (Linklist)malloc(sizeof(LNode));
s->data = x;
r->next = s;
r = s;
cin >> x;
}
r->next = NULL;
return L;
}
void Print(Linklist L) {
Linklist p = L;
while (p->next != NULL) {
cout << p->next->data << " ";
p = p->next;
if (p == L) break;
}
}
Linklist Enloop(Linklist &L) {
Linklist p = L;
while (true) {
if (p->next != NULL) {
p = p->next;
}
else {
p->next = L->next;
break;
}
}
return L;
}
Linklist Move(Linklist &L) {
int k;
cout << "输入要移动的位数" << endl;
cin >> k;
for (int i = 0; i < k; i++) {
L = L->next;
}
return L;
}
int main()
{
Linklist L;
L=Create();
Insert(L);
Print(L);
Enloop(L);
Move(L);
Print(L);
}


IP属地:河南1楼2021-05-08 09:59回复