题目链接
2.1数据结构实验之链表一:顺序建立链表
#include<bits/stdc++.h>
using namespace std;
typedef struct lnode{
int data;
struct lnode *next;
}lnode,*linklist;
int n;
void buildlist(linklist &L){
L=(linklist)malloc(sizeof(lnode));
lnode *s,*r=L;
for(int i=0;i<n;i++){
s=(lnode *)malloc(sizeof(lnode));
cin>>s->data;
r->next=s;
r=r->next;
}
r->next=NULL;
}
void disp(linklist L){
lnode *s=L->next;
while(s){
cout<<s->data<<" ";
s=s->next;
}
cout<<endl;
}
int main(){
cin>>n;
linklist L;
buildlist(L);
disp(L);
return 0;
}
数据结构实验之链表二:逆序建立链表
#include<bits/stdc++.h>
using namespace std;
typedef struct lnode{
int data;
struct lnode *next;
}lnode,*linklist;
int n;
void buildlist(linklist &L){
L=(linklist)malloc(sizeof(lnode));
lnode *s;
L->next=NULL;
for(int i=0;i<n;i++){
s=(lnode *)malloc(sizeof(lnode));
scanf("%d",&s->data);
s->next=L->next;
L->next=s;
}
}
void disp(linklist L){
lnode *s=L->next;
while(s){
cout<<s->data<<" ";
s=s->next;
}
cout<<endl;
}
int main(){
cin>>n;
linklist L;
buildlist(L);
disp(L);
return 0;
}
2-4 无序单链表中重复元素的删除
#include<bits/stdc++.h>
using namespace std;
int n;
typedef struct lnode {
int data;
struct lnode *next;
} lnode,*linklist;
void buildlist(linklist &L) {
L=(linklist)malloc(sizeof(lnode));
lnode *s;
L->next=NULL;
for(int i=0; i<n; i++) {
s=(lnode *)malloc(sizeof(lnode));
cin>>s->data;
s->next=L->next;
L->next=s;
}
}
void deletelist(linklist &L){
lnode *s,*q,*k;
s=L->next;
q=L;
while(s){
int flag=1;
k=L->next;
while(k!=s){
if(k->data==s->data){
flag=0;
n--;
q->next=s->next;
s=s->next;
break;
}
k=k->next;
}
if(flag==1){
s=s->next;
q=q->next;
}
}
}
void disp(linklist L) {
lnode *s=L->next;
while(s) {
cout<<s->data<<" ";
s=s->next;
}
cout<<endl;
}
int main() {
cin>>n;
linklist L;
buildlist(L);
cout<<n<<endl;
disp(L);
deletelist(L);
cout<<n<<endl;
disp(L);
return 0;
}