#include <iostream>
#include <math.h>
#include <stack>
using namespace std;
//删除动态链表中指定的节点
#if 0
class Node
{
public:
int number;
Node *pnext;
Node* creat_list();
void show_list(Node *h);
bool delete_list(Node *phead, int pos, int *pval);
bool insert_list(Node *phead, int pos, int val);
};
Node * Node::creat_list()
{
Node *head = NULL, *p1, *p2 = NULL;
p1 = new Node;
p1->number = 1;
head = p1;
p2 = head;
for (int i = 2; i <= 13; i++)
{
p1 = new Node;
p1->number = i;
p2->pnext = p1;
p2 = p1;
}
p2->pnext = NULL;
return head;
}
bool Node::delete_list(Node *phead, int pos, int *pval)
{
int i = 0;
Node *p = phead;
while (p->pnext != NULL && i < pos - 1)
{
p = p->pnext;
i++;
}
if (i >(pos - 1) || p->pnext == NULL)
{
return false;
}
Node *q = p->pnext;
*pval = q->number;
//删除p节点后面的节点
p->pnext = p->pnext->pnext;
free(q);
q = NULL;
cout << "the delete value is" << *pval << endl;
return true;
}
bool Node::insert_list(Node *phead, int pos, int val)
{
int i = 0;
Node *p = phead;
while ((NULL != p) && (i<pos - 1))
{
p = p->pnext;
i++;
}
if ((i > pos - 1) || NULL == p)
{
return false;
}
Node *pnew = new Node;
pnew->number = val;
Node *q = p->pnext;
p->pnext = pnew;
pnew->pnext = q;
return true;
}
void Node::show_list(Node *h)
{
Node*p;
p = h;
while (p != NULL)
{
cout << p->number << " ";
p = p->pnext;
}
cout << endl;
}
void main()
{
int val;
Node *p = NULL;
p = p->creat_list();
p->show_list(p);
p->delete_list(p, 4, &val);
p->show_list(p);
p->insert_list(p,6,60);
p->show_list(p);
}
#endif
//a b 合并 按学好的升序排列
#if 0
class student
{
public:
long num;
float score;
student *next;
void print( student * head);
friend student* sx(student * head);
};
class student a[5] =
{
{ 1005, 10.23, &a[1] },
{ 1004, 10.54, &a[2] },
{ 1003, 11.55, &a[3] },
{ 1002, 12.32, &a[4] },
{ 1001, 15.65, NULL }
};
class student b[5] =
{
{ 2005, 11.23, &b[1] },
{ 2004, 12.54, &b[2] },
{ 2003, 16.55, &b[3] },
{ 2002, 14.32, &b[4] },
{ 2001, 17.65, NULL }
};
void student::print( student * head)//打印链表的函数
{
student * p;
p = head;
if (head == NULL)
{
cout << "该链表为空" << endl;
}
else{
do{
cout << p->num <<" "<<p->score<<endl;
p = p->next;
} while (p != NULL);
}
}
student* sx( student * head)
{ // 用递归,每次找出原链表中学号最小的元素,插入到新链表的后面。
student *cursor, *first, *prev, *min;
first = NULL;
if (head == NULL)
return NULL;
for (cursor = min = head; cursor->next != NULL; cursor = cursor->next)
{
if ((cursor->next->num) < (min->num))
{
prev = cursor;
min = cursor->next;
}
}
first = min;
if (min == head)
{
head = head->next;
}
else
{
prev->next = min->next;
}
first->next = sx(head);
return first;
}
void main()
{
a[4].next = &b[0];//将两个链表先连接起来
student * head;
head = &a[0];
head->print(head);
cout << "num 按升序排列之后的输出结果是" << endl;
head = sx(head);//将经过升序排列之后的节点链表的首地址,赋值给head
head->print(head);//打印升序之后的链表
}
#endif
// a b 从a 中删除与b 中相同学号的元素
#if 0
class student
{
public:
long num;
float score;
student *pnext;
void print(student * head);
friend student *del(student *head1, student *head2);
};
void student::print(student * head)//打印链表的函数
{
student * p;
p = head;
while (p != NULL)
{
cout << p->num << " " << p->score << endl;
p = p->pnext;
}
}
student *del(student *head1, student *head2)
{
student *pa = head1;
student *pb = head2;
student *prev = NULL;
if (pa == NULL)
{
cout << "链表A为空" << endl;
return head1;
}
while (pa != NULL)
{
if (pa->num == pb->num)
{
if (pa == head1)
{
head1 = pa->pnext;
}
else
{
prev->pnext = prev->pnext->pnext;
pb = pb->pnext;
}
return head1;
}
else
{
prev = pa;
}
pa = pa->pnext;
pb = pb->pnext;
}
return head1;
}
class student a[5] =
{
{ 1005, 10.23, &a[1] },
{ 1004, 10.54, &a[2] },
{ 1003, 11.55, &a[3] },
{ 1002, 12.32, &a[4] },
{ 1001, 15.65, NULL }
};
class student b[5] =
{
{ 2005, 11.23, &b[1] },
{ 1004, 12.54, &b[2] },
{ 2003, 16.55, &b[3] },
{ 2002, 14.32, &b[4] },
{ 2001, 17.65, NULL }
};
void main()
{
student * head;
head = &a[0];
head->print(head);
cout << "b链表的数据为 " << endl;
head = &b[0];
head->print(head);
cout << "删除相同元素之后" << endl;
head = del(&a[0],&b[0]);
head->print(head);
}
#endif
//如果链表中输入一个年龄 该链表包含此年龄则删除该节点
#if 0
#include<stdio.h>
class student
{
public:
int num;
char name[10];
int age;
student *next;
friend student * del(student *head);
void print(student * head);
};
void student::print(student * head)//打印链表的函数
{
student * p;
p = head;
while (p != NULL)
{
cout << p->num << " " << p->name <<" "<<p->age<< endl;
p = p->next;
}
}
student * del(student *head)
{
int n;
student *p = head;
student *prev = NULL;
printf("请输入年龄:");
scanf("%d", &n);
while (p != NULL)
{
if (p->age == n)
{
head = p->next;
p = p->next;
}
else if (p->next->age == n)
{
cout << "被删除的数为" << n << endl;
p->next = p->next->next;
break;
}