数据结构与算法(王卓)——线性表习题
(仅用于记录学习过程)
目录
- 线性表的合并(顺序表形式)
- 有序表的合并(顺序表+链表)
- 稀疏多项式相加(链表)
- 图书管理系统(先挖个坑)
线性表的合并
# define MAXSIZE 10
typedef struct
{
int data[MAXSIZE];
int length;
}Sqlist;
// 初始化顺序表
void init(Sqlist &a)
{
cout << "how many elements: ";
cin >> a.length;
for(int i=0; i<a.length; i++)
{
cout << "enter your element #" << i+1 << ": ";
cin >> a.data[i];
}
}
// 按值索引
int GetItem(Sqlist &a, int value)
{
for(int i=0; i<a.length; i++)
{
if(a.data[i]==value)
return 1;
}
return 0;
}
// 插入
void Insert(Sqlist &a, int value)
{
a.data[a.length] = value;
a.length++;
}
// 合并
void Merger(Sqlist &a, Sqlist &b)
{
int Lb_len = b.length;
int La_len = a.length;
for(int ib=0; ib<Lb_len; ib++)
{
if(!GetItem(a, b.data[ib]))
Insert(a, b.data[ib]);
}
}
int main()
{
Sqlist La, Lb;
init(La);
init(Lb);
Merger(La, Lb);
for(int i=0; i<La.length; i++)
{
cout << La.data[i];
}
system("pause");
return 0;
}
有序表的合并—顺序表形式
# define MAXSIZE 20
typedef struct
{
int data[MAXSIZE];
int length;
}Sqlist;
// 初始化顺序表
void init(Sqlist &a)
{
cout << "how many elements: ";
cin >> a.length;
for(int i=0; i<a.length; i++)
{
cout << "enter your element #" << i+1 << ": ";
cin >> a.data[i];
}
}
// 合并
void Merger(Sqlist &a, Sqlist &b, Sqlist &c)
{
int Lb_len = b.length;
int La_len = a.length;
// 先确定Lc的长度
c.length = La_len + Lb_len;
int *pa = &a.data[0];
int *pb = &b.data[0];
int *pc = &c.data[0];
int *pa_last = &a.data[La_len-1];
int *pb_last = &b.data[Lb_len-1];
while((pa<=pa_last)&&(pb<=pb_last))
{
if(*pa<*pb)
{
*pc = *pa;
pa++;
}
else if(*pa>*pb)
{
*pc = *pb;
pb++;
}
else
{
*pc = *pb;
*pc++;
*pc = *pa;
pa++;
pb++;
}
pc++;
}
while((pb<=pb_last))
{
*pc = *pb;
pb++;
pc++;
}
while((pa<=pa_last))
{
*pc = *pa;
pa++;
pc++;
}
}
int main()
{
Sqlist La, Lb, Lc;
init(La);
init(Lb);
Merger(La, Lb, Lc);
for(int i=0; i<Lc.length; i++)
{
cout << Lc.data[i] << ", ";
}
system("pause");
return 0;
}
有序表的合并—链表形式
struct element
{
int number;
};
typedef struct Lnode
{
struct element data;
struct Lnode *next;
}Lnode, *LinkList;
// 初始化链表:头插法,一个辅助指针
void init(Lnode &a)
{
int n;
cout << "how many elements: ";
cin >> n;
for(int i=0; i<n; i++)
{
Lnode *p = new Lnode;
cout << "input your number: ";
cin >> p->data.number;
p->next = a.next;
a.next = p;
}
}
// 输出链表的值
void show(Lnode &a)
{
Lnode *p = a.next;
while(p)
{
cout << p->data.number << " ";
p = p->next;
}
}
// 合并
void Merger(Lnode &a, Lnode &b, Lnode &c)
{
Lnode *pa, *pb, *pc;
pa = a.next;
pb = b.next;
c = a; // **用a的头结点作为c的头节点
pc = &c;
while(pa&&pb)
{
if(pa->data.number<pb->data.number)
{
pc->next = pa;
pc = pa;
pa = pa->next;
}
else if(pa->data.number>pb->data.number)
{
pc->next = pb;
pc = pb;
pb = pb->next;
}
else
{
pc->next = pa;
pc = pa;
pa = pa->next;
pc->next = pb;
pc = pb;
pb = pb->next;
}
}
pc->next = pa?pa:pb;
}
int main()
{
Lnode La, Lb, Lc;
La.next = NULL;
Lb.next = NULL;
Lc.next = NULL;
init(La);
init(Lb);
show(La);
show(Lb);
Merger(La, Lb, Lc);
show(Lc);
system("pause");
return 0;
}
稀疏多项式运算—链表形式
struct element
{
int index;
int coffcient;
};
typedef struct Lnode
{
struct element data;
struct Lnode *next;
}Lnode, *LinkList;
// 初始化链表:头插法,一个辅助指针
void init(Lnode &a)
{
int n;
cout << "how many elements: ";
cin >> n;
for(int i=0; i<n; i++)
{
Lnode *p = new Lnode;
cout << "input your index: ";
cin >> p->data.index;
cout << "input your conffcient: ";
cin >> p->data.coffcient;
p->next = a.next;
a.next = p;
}
}
// 输出链表的值
void show(Lnode &a)
{
Lnode *p = a.next;
while(p)
{
cout << p->data.coffcient << "x" << p->data.index << "+";
p = p->next;
}
}
// 合并
void Merger(Lnode &a, Lnode &b, Lnode &c)
{
Lnode *pa, *pb, *pc;
pa = a.next;
pb = b.next;
c = a; // **用a的头结点作为c的头节点
pc = &c;
while(pa&&pb)
{
if(pa->data.index<pb->data.index)
{
pc->next = pa;
pc = pa;
pa = pa->next;
}
else if(pa->data.index>pb->data.index)
{
pc->next = pb;
pc = pb;
pb = pb->next;
}
else
// 相等就要相加
{
int coff;
coff = pa->data.coffcient + pb->data.coffcient;
if(coff!=0)
{
pc->next = pa;
pc = pa;
pc->data.coffcient = coff;
pa = pa->next;
pb = pb->next;
}
else
{
pa = pa->next;
pb = pb->next;
pc->next = pa;
}
}
}
pc->next = pa?pa:pb;
}
int main()
{
Lnode La, Lb, Lc;
La.next = NULL;
Lb.next = NULL;
Lc.next = NULL;
init(La);
init(Lb);
show(La);
cout << endl;
show(Lb);
cout << endl;
Merger(La, Lb, Lc);
cout << endl;
show(Lc);
cout << endl;
// Merger(La, Lb, Lc);
// for(int i=0; i<Lc.length; i++)
// {
// cout << Lc.data[i] << ", ";
// }
system("pause");
return 0;
}