数据结构(王道)链表部分习题代码实现
#include <bits/stdc++.h>
int main()
{
}
void deleteAllDataIsX(SqList &head,int x){
Lnode *p = head->next;
Lnode *pre = head;
Lnode = *q;
while(p!=null){
if(p->data == x){
q = p;
p = p->next;
pre->next = p;
free(q);
}
else{
pre = p;
p = p->next;
}
}
}
SqList DeleteMinInList(SqList &L){
Lnode *p = L->next;
Lnode *pre = L;
Lnode *min = p;
Lnode *minpre = pre;
while(p!=NULL){
if(p->data<min->data){
min = p;
minpre = pre;
}
pre = p;
p = p->next;
}
minpre->next = min->next;
free(min);
return L;
}
List Reverse(SqList &L){
Lnode *p = L->next;
Lnode *pre;
Lnode *right = p->next;
p->next = NULL;
while(right!=NULL){
pre = p;
p = right;
right = right->next;
p->next = pre;
}
L->next = p;
return L;
}
List DeleteBetweenAB(Sqlist &L,int a,int b){
Lnode *p = L->next;
Lnode *pre = L;
Lnode *q;
while(p!=NULL){
if(p->data>=a&&p->data<=b){
q = p;
pre->next = p->next;
free(q);
}
else{
pre = p;
p = p->next;
}
}
return L;
}
int getLen(SqList &T){
Lnode *p = T->next;
int count = 0;
while(p!=NULL){
count++;
p = p->next;
}
return count;
}
List FindParrellNode(SqList &L1,SqList &L2){
int len1 = getLen(L1);
int len2 = getLen(L2);
Lnode *longList;
Lnode *shortList;
int chazhi;
if(len1>len2){
longList = L1->next;
shortList = L2->next;
chazhi = len1-len2;
}
else{
longList = L2->next;
shortList = L1->next;
chazhi = len2-len1;
}
while(chazhi--){
longList = longList->next;
}
while(longList!=NULL){
if(longList==shortList){
return longList;
}else{
longList = longList->next;
shortList = shortList->next;
}
}
return NULL;
}
void PrintNumber(SqList &L){
Lnode *p = L->next;
while(p!=NULL){
Lnode *pre = L;
Lnode *t = pre->next;
while(t->next!=NULL){
if(t->next->data < pre->next->data){
pre = t;
t = t->next;
}
}
printf(pre->next->data);
Lnode q = pre->next;
pre->next = q->next;
free(q);
}
free(L);
}
List CreateAnother(SqList &A){
int number = 0;
B = (SqList)malloc(sizeof(Lnode));
B->next = NULL;
Lnode *Apre = A;
Lnode *Bpre = B;
Lnode p = A->next;
A->next = NULL;
while(p!=NULL){
number++;
if(number%2!=0){
Bpre->next = p;
Bpre = p;
}else{
Apre->next = p;
Apre = p;
}
}
Apre->next = NULL;
Bpre->next = NULL;
return B;
}
Link Gap(SqList &A){
B = (List)malloc(sizeof(Lnode));
B->next = NULL;
Lnode *p = A->next;
Lnode *q;
Lnode *leftA = A;
while(p!=NULL){
leftA->next = p;
leftA = p;
p = p->next;
if(p!=NULL){
q = p->next;
}
p->next = B->next;
B->next = p;
p = q;
}
leftA->next = NULL;
return B;
}
List quChong(SqList &L){
Lnode *p = L->next;
Lnode *last ;
if(p==NULL){
return 0;
}
while(p->next!=NULL){
last = p->next;
if(last->data == p->data){
p->next = last->next;
free(last);
}
else{
p=p->next;
}
}
return L;
}
List hebing(SqList &L1,SqList &L2){
Lnode *p1 = L1->next;
Lnode *p2 = L2->next;
Lnode *p3;
L1->next = NULL;
while(p1&&p2){
if(p1->data <= p2->data){
p3 = p1->next;
p1->next = L1->next;
L1->next = p1;
p1 = p3;
}
else{
p3 = p2->next;
p2->next = L1->next;
L1->next = p2;
p2 = p3;
}
}
if(p1)
p2=p1;
while(p2){
p3 = p2->next;
p2->next = L1->next;
L1->next = p2;
p2 = p3;
}
free(L2);
}
List generateC(Sqlist &A,Sqlist &B){
Lnode *p1 = A->next;
Lnode *p2 = B->next;
List C = (List)malloc(sizeof(Lnode));
Lnode r = C;
while(p1!=NULL&&p2!=NULL){
if(p1->data < p2->data){
p1 = p1->next;
}
else if(p1->data<p2->data){
p2 = p2->next;
}
else{
Lnode s = (Lnode)malloc(sizeof(Lnode));
s->data = p1->data;
r->next = s;
r = s;
p1 = p1->next;
p2 = p2->next;
}
}
r->next = NULL;
return C;
}
List jiaoji(SqList &A,SqList &B){
Lnode *p1 = A->next;
Lnode *p2 = B->next;
Lnode *p3 = A;
Lnode *u;
while(p1&&p2){
if(p1->data == p2->data){
p3->next = p1;
p3 = p1;
u = p2;
p2 = p2->next;
free(u);
}
else if(p1->data < p2->data){
u = p1;
p1 = p1->next;
free(u);
}
else{
u = p2;
p2 = p2->next;
free(u);
}
}
while(p1){
u = p1;
p1 = p1->next;
free(u);
}
while(p2){
u = p2;
p2 = p2->next;
free(u)
}
p3->next = NULL;
free(p2);
return l1;
}
bool IsSmall(SqList &A,SqList &B){
Lnode *p = A;
Lnode *pre = p;
Lnode *q = B;
while(p&&q){
if(p->data == q->data){
p = p->next;
q = q->next;
}
else{
pre = pre->next;
p = pre;
q =B;
}
}
if(q== NULL){
return true;
}else{
return false;
}
}
bool IsSymmetry(SqList &L){
Lnode *p = L->next;
Lnode *last = L->pre;
while(p!=last&&last->next!=p){
if(p->data == last->data){
p= p->next;
last = last->pre;
}
else{
return false;
}
}
return true;
}
void DeleteMin(SqList &L){
Lnode *p ,*pre , *minnp,*minpre;
while(L->next != L) {
p = L->next;
pre = L;
minp = p;
minpre = pre;
while (p != L) {
if (p->data < minp->data) {
minp = p;
minpre = pre;
}
pre = pre->next;
p = p->next;
}
cout << minp->data << endl;
minpre->next = minp->next;
free(minp);
}
free(L);
}
typedef struct Lnode{
int data;
struct Lnode *link;
}Lnode,*list;
int searchLastK(list l1,int k ){
Lnode *p=l1->link;
Lnode *q = l1->link;
int number = 0;
while(p!=NULL){
if(number<k){
number++;
}else{
q=q->link;
}
p=p->link;
}
if(number<k){
return 0;
}else{
cout<<q->data<<endl;
return 1;
}
}
typedef struct Lnode{
int data;
struct Lnode *link;
}Lnode,*List;
int SearchLastK(List L,int k){
Lnode *p = L;
Lnode *q = L;
while(k--){
if(q == NULL){
return 0;
}else{
q = q->link;
}
}
while(q!=NULL){
q= q->link;
p = p->link;
}
cout<< p->link->data <<endl;
return 1;
}
typedef struct Lnode{
char juzi;
struct Lnode *next;
}Snode,*Linklist;
int Length(LinkList &L){
int count=0;
while(p!=NULL){
count++;
p = p->next;
}
return count;
}
int mainfunction(Snode *str1,Snode *str2){
int str1L = Length(str1);
int str2L = Length(str2);
Snode *l1,*l2;
for(l1 = str1;str1L>str2L;str1L--){
str1 = str1->next;
}
for(l2= str2;str2<str1L;str2L--){
str2 = str2->next;
}
while(str1->next!= NULL&&str1->next!=str2->next){
str1= str1->next;
str2= str2->next
}
return str1->next;
}
int mainfun(Snode *str1,Snode *str2){
int len1 = Length(str1);
int len2 = Length(str2);
Snode *longL,*shortL;
int chazhi;
if(len1<len2){
longL= str2->next;
shortL=str1->next;
chazhi = len2-len1;
}
else if(len2<len1) {
longL = str1->next;
shortL=str2->next;
chazhi = len1 - len2;
}
while(chazhi--){
longL = longL->next;
}
while(longL!=NULL){
if(longL == shortL){
return longL;
}else{
longL = longL->next;
shortL = shortL->next;
}
}
return NULL;
}
typedef struct Lnode{
int data;
struct Lnode *link;
}node;
void func(node head,int n){
int a[n+1];
for(int i= 0 ;i<n+1;i++){
a[i] =0;
}
node *p=head;
node *d;
while(p->link!=NULL){
p->link->data>0?p->link->data:-(p->link->data);
if(a[p->link->data]==0){
a[p->link->data] = 1;
p= p->link;
}else{
d = p->link;
p->link=d->link;
free(d);
}
}
free(a);
}