数据结构中的合并顺序表问题
发布网友
发布时间:2022-05-19 08:04
我来回答
共1个回答
热心网友
时间:2023-10-09 04:22
#define ElementType char
#define MAX_LEN 1024#include<stdio.h>
#include<malloc.h>
typedef struct Node{
ElementType data ;
Node *next;
}*ListHead,Node;ListHead ListEmerge(ListHead lista,ListHead listb){
ListHead scan_a,scan_b,listc,scan_c;
scan_a = lista;
scan_b = listb;
scan_c = listc = new Node; while(lista&&listb){
if(scan_a->data < scan_b->data){
lista = lista->next;
scan_c->next = scan_a;
scan_a = lista;
}
else
{
listb = listb->next;
scan_c->next = scan_b;
scan_b = listb; }
scan_c = scan_c->next;
}
while(lista){scan_c->next = lista;lista = NULL;}
while(listb){scan_c->next = listb;listb = NULL;}
scan_c = listc;
listc = listc->next;
free(scan_c);
return listc;}ListHead ListFilter(ListHead list){
ListHead scan_all,scan_pre,res_list,scan_cur ; res_list=scan_cur = list;
while(scan_cur){//1
scan_pre = scan_cur;
scan_all = scan_cur->next;
while(scan_all){//2
if(scan_all->data == scan_cur->data){
scan_pre->next = scan_all->next;
free(scan_all);
scan_all = scan_pre->next;
// printf("equal.\n");
}
else{
scan_pre = scan_pre->next;
scan_all = scan_all->next;
// printf("Not equal.\n");
}
}//2
scan_cur= scan_cur->next;
}//while1
return res_list;
}ListHead ListCreate(){
int i = 0;
ListHead head,scan,pre_scan;
scan=head= (ListHead) malloc(sizeof(Node));
printf("Enter the letters to be filtered:(less than %d)\n",MAX_LEN);
for(ElementType in_char;i<MAX_LEN&&(in_char = getchar())&&(in_char != '\n')&&(in_char != '0');i++)
{
scan->data = in_char;
scan->next = (ListHead) malloc(sizeof(Node));
pre_scan = scan;
scan = scan->next;
}
pre_scan->next = NULL;
return head;
}void ListShow(ListHead list){
ListHead scan = list;
while(scan != NULL)
{
printf(" -> %c ",scan->data);
scan = scan->next;
}
printf("\n");
}
void main(){
ListHead lista = ListCreate();
ListHead listb = ListCreate();
//FilterList(list);
printf("Lista");
ListShow(lista);
printf("Listb ");
ListShow(listb);
ListHead listc = ListEmerge(lista,listb);
printf("Listc ");
ListShow(listc);
////////
listc = ListFilter(listc); printf("Listac AfterFilter");
ListShow(listc);
}这个应该符合你要求