目录
1.循环链表
1.带尾指针循环链表的合并
代码示例:
2.双向链表
代码示例:
1.双向链表的插入
代码示例:
2.双向链表的删除
代码示例:
3.单链表,循环链表,双向链表时间效率的比较
4.顺序表和链表的比较
5.存储密度
6.线性表的应用
1.线性表的合并
代码示例:
2.有序表的合并
1.用顺序表实现
代码示例:
2.用链表实现
代码示例:
7.案例分析
1.一元多项式的运算
2.稀疏多项式的运算
3.图书信息管理系统
8.总的代码
1.循环链表
1.带尾指针循环链表的合并
代码示例:
linklist connect(linklist ta,linklist tb)
{
lnode * p;
p = ta -> next;
ta -> next = tb -> next -> next;
delete tb -> next;
tb -> next = p;
return tb;
}
2.双向链表
代码示例:
typedef struct dulnode{
int data;
struct dulnode *prior,*next;
}dulnode,*dulinklist;
1.双向链表的插入
代码示例:
void insert_dul(dulinklist &l,int i,int e)
{
if((p = getelem_dul(l,i)) == NULL) return 0;
dulnode * s;
s -> data = e;
s -> prior = p -> prior;
p -> prior -> next = s;
s -> next = p;
p -> prior = s;
return 1;
}
2.双向链表的删除
代码示例:
void delete_dullist(dulinklist &l,int i,int &e)
{
dulnode *p;
if((p = getelem_dul(l,i)) == NULL) return 0;
e = p -> data;
p -> prior -> next = p -> next;
p -> next -> prior = p -> prior;
delete p;
return 1;
}
3.单链表,循环链表,双向链表时间效率的比较
4.顺序表和链表的比较
5.存储密度
6.线性表的应用
1.线性表的合并
代码示例:
void union(list &la,list &lb)
{
int la_len = listlength(la);
int lb_len = listlength(lb);
for(int i = 1; i <= lb_len; i ++)
{
int e = getelem(lb,i,e)
//从b链表中取元素e,如果a链表中不存在元素e,就将元素e插入到链表a尾部并且让链表a长度加一
if(!locateelem(la,e)){listinsert(&la,++la_len,e)}
}
}
2.有序表的合并
1.用顺序表实现
代码示例:
void merge_list(sqlist la,sqlist lb,sqlist &lc)
{
lnode *pa,*pb,*pc;
pa = la.elem;
pb = lb.elem;
lc.length = la.length + lb.length;
lc.elem = new int[lc.length];
pc = lc.elem;
lnode *pa_last,*pb_last
pa_last = la.elem + la.length - 1;
pb_last = lb.elem + lb.length - 1;
while(pa <= pa_last && pb <= pb_last)
{
if(*pa < *pb){*pc++ = *pa++;}
else{*pc++ = *pb++;}
}
while(pa <= pa_last) *pc++ = *pa++;
while(pb <= pb_last) *pc++ = *pb++;
}
2.用链表实现
代码示例:
void merge_list(linklist &la,linklist &lb,linklist &lc)
{
lnode *pa,*pb,*pc;
pa = la -> next;
pb = lb -> next;
pc = lc = la;
while(pa != NULL && lb != NULL)
{
if(pa -> data <= pb -> data)
{
pc -> next = pa;
pc = pa;
pa = pa -> next;
}
else{
pc -> next = pb;
pc = pb;
pb = pb -> next;
}
}
if(pa == NULL) pc -> next = pb;
else if(pb == NULL) pc -> next = pa;
delete lb;
}
7.案例分析
1.一元多项式的运算
2.稀疏多项式的运算
3.图书信息管理系统
8.总的代码
typedef struct dulnode{
int data;
struct dulnode *prior,*next;
}dulnode,*dulinklist;
linklist connect(linklist ta,linklist tb)
{
lnode * p;
p = ta -> next;
ta -> next = tb -> next -> next;
delete tb -> next;
tb -> next = p;
return tb;
}
void insert_dul(dulinklist &l,int i,int e)
{
dulinklist p;
if((p = getelem_dul(l,i)) == NULL) return 0;
dulnode * s;
s -> data = e;
s -> prior = p -> prior;
p -> prior -> next = s;
s -> next = p;
p -> prior = s;
return 1;
}
void delete_dullist(dulinklist &l,int i,int &e)
{
dulnode *p;
if((p = getelem_dul(l,i)) == NULL) return 0;
e = p -> data;
p -> prior -> next = p -> next;
p -> next -> prior = p -> prior;
delete p;
return 1;
}
void union(list &la,list &lb)
{
int la_len = listlength(la);
int lb_len = listlength(lb);
for(int i = 1; i <= lb_len; i ++)
{
int e = getelem(lb,i,e)
//从b链表中取元素e,如果a链表中不存在元素e,就将元素e插入到链表a尾部并且让链表a长度加一
if(!locateelem(la,e)){listinsert(&la,++la_len,e)}
}
}
void merge_list(sqlist la,sqlist lb,sqlist &lc)
{
lnode *pa,*pb,*pc;
pa = la.elem;
pb = lb.elem;
lc.length = la.length + lb.length;
lc.elem = new int[lc.length];
pc = lc.elem;
lnode *pa_last,*pb_last
pa_last = la.elem + la.length - 1;
pb_last = lb.elem + lb.length - 1;
while(pa <= pa_last && pb <= pb_last)
{
if(*pa < *pb){*pc++ = *pa++;}
else{*pc++ = *pb++;}
}
while(pa <= pa_last) *pc++ = *pa++;
while(pb <= pb_last) *pc++ = *pb++;
}
void merge_list(linklist &la,linklist &lb,linklist &lc)
{
lnode *pa,*pb,*pc;
pa = la -> next;
pb = lb -> next;
pc = lc = la;
while(pa != NULL && lb != NULL)
{
if(pa -> data <= pb -> data)
{
pc -> next = pa;
pc = pa;
pa = pa -> next;
}
else{
pc -> next = pb;
pc = pb;
pb = pb -> next;
}
}
if(pa == NULL) pc -> next = pb;
else if(pb == NULL) pc -> next = pa;
delete lb;
}