|
|
#2
寒风中的细雨2011-09-19 06:58
程序代码:#include <stdio.h>//递增有序单链表,删除表中相同的多余元素 #include <stdlib.h> typedef char DataType; typedef struct Node { DataType data; struct Node *next; }LinkList; void DeleteSame(LinkList *L); int main(void) { LinkList *head = NULL; LinkList *L = NULL; LinkList *p = NULL, *q = NULL; char ch; head = malloc (sizeof (LinkList)); head->next = NULL; while ((ch=getchar()) != '$') { L = malloc (sizeof(LinkList)); L->data = ch; p = head; q = head->next; while (NULL!=q && q->data<=ch) { p = p->next; q = q->next; } L->next = p->next; p->next = L; //flushall(); } //输出未经处理的字样 p = head->next; while (NULL != p) { printf("%c", p->data); p = p->next; } printf("\n"); DeleteSame(head); //输出经过处理的字样 p = head->next; while (NULL != p) { printf("%c", p->data); p = p->next; } printf("\n"); return 0; } void DeleteSame(LinkList *L) { LinkList *p=NULL, *fp=NULL, *t=NULL; fp = L->next; p = fp->next; while (NULL !=fp && NULL != p) { if (p->data == fp->data) { t = p; fp->next = p->next; p = p->next; free(t); } else { fp = fp->next; p = p->next; } } } |
#include <stdio.h>//递增有序单链表,删除表中相同的多余元素
#include <stdlib.h>
#define DATATYPE char
typedef struct node
{
DATATYPE data;
struct node *next;
}LINKLIST;
void deletesame(LINKLIST *L);
int main(void)
{
LINKLIST *head,*L,*p,*q,*t,*k;
char ch;
L=(LINKLIST *)malloc(sizeof(LINKLIST));
head=L,head->next=NULL;
while((ch=getchar())!='$')
{
L=(LINKLIST *)malloc(sizeof(LINKLIST));
L->data=ch;
p=head;
q=head->next;
while(q!=NULL&&q->data<=ch)
{
p=q;
q=q->next;
}
p->next=L;
L->next=q;
}
t=head->next;
while(t!=NULL)
{
printf("%c",t->data);
t=t->next;
}
deletesame(head);
k=head->next;
while(k!=NULL)
{
printf("%c",k->data);
k=k->next;
}
}
void deletesame(LINKLIST *L)
{
LINKLIST *p,*q,*t;
p=L->next;q=L->next->next;
while(p!=NULL)
{
if(p->data==q->data)
{
t=q;
p->next=t->next;
q=q->next;
free(t);
}
p=p->next;
}
}
[ 本帖最后由 来学C语言啊 于 2011-9-18 20:35 编辑 ]
程序代码: