delete repeted entry
#include <stdio.h>
#include <malloc.h>
struct cell {//单链表结点结构体定义
int x;
struct cell* next;
};
struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回
struct cell* head, * tmp, * p;
head = tmp = p = NULL;
int n;
head=(struct cell*)malloc(sizeof(struct cell));
head->next=NULL;
tmp=head;
scanf("%d",&n);
while(n!=0){
p=(struct cell*)malloc(sizeof(struct cell));
p->x=n;
p->next=NULL;
tmp->next=p;
tmp=p;
scanf("%d",&n);
}
tmp=head;
head=head->next;
free(tmp);
return head;//返回单链表头
}
struct cell* del2one(struct cell* head) {//删除重复结点只保留一个,head是单链表首结点指针
//请在以下位置补充完整,实现函数del2one的功能
struct cell *guard=(struct cell*)malloc(sizeof(struct cell));
struct cell*p,*r,*r0,*tmp;
guard->next=head;
head=guard;
p=guard->next;
while(p!=NULL){
r0=p;r=p->next;
while(r!=NULL){
if(p->x!=r->x){
r0=r;
r=r->next;
}
else{
tmp=r;
r=r->next;
r0->next=r;
}
}
p=p->next;
}
head=head->next;
free(guard);
return head;
}
void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针
while(head!=NULL){
printf("%d",head->x);
if(head->next!=NULL) printf(" ");
head=head->next;
}
}
int main(void) {
struct cell* head;
head = build();
head=del2one(head);
if(head!=NULL)
print(head);
else
printf("NULL");
}
//print the count backwards entry
#include <stdio.h>
#include <malloc.h>
struct cell {//单链表结点结构体定义
int x;
struct cell* next;
};
struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回
struct cell* head, * tmp, * p;
head = tmp = p = NULL;
int n;
head=(struct cell*)malloc(sizeof(struct cell));
head->next=NULL;
tmp=head;
scanf("%d",&n);
while(n!=0){
p=(struct cell*)malloc(sizeof(struct cell));
p->x=n;
p->next=NULL;
tmp->next=p;
tmp=p;
scanf("%d",&n);
}
tmp=head;
head=head->next;
free(tmp);
return head;//返回单链表头
}
struct cell * back(struct cell* head, int k) {//求链表倒数第k个结点,head是单链表首结点指针
//请在以下位置补充完整,实现函数back的功能
struct cell* p;
p=head;
int cnt=0,cnt1;
while(p!=NULL){
cnt++;
p=p->next;
}
cnt1=cnt-(k-1);
if(cnt1<1) return NULL;
p=head;
int n=1;
while(n<cnt1){
p=p->next;
n++;
}
return p;
}
int main(void) {
struct cell* head,*result;
int k;
head = build();
scanf("%d", &k);
result = back(head,k);
if (result != NULL)
printf("%d",result->x);
else
printf("NULL");
}
//print the list from the middle
#include <stdio.h>
#include <malloc.h>
struct cell {//单链表结点结构体定义
int x;
struct cell* next;
};
struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回
struct cell* head, * tmp, * p;
head = tmp = p = NULL;
int n;
head=(struct cell*)malloc(sizeof(struct cell));
head->next=NULL;
tmp=head;
scanf("%d",&n);
while(n!=0){
p=(struct cell*)malloc(sizeof(struct cell));
p->x=n;
p->next=NULL;
tmp->next=p;
tmp=p;
scanf("%d",&n);
}
tmp=head;
head=head->next;
free(tmp);
return head;//返回单链表头
}
struct cell* mid(struct cell* head) {//寻找链表中间位置结点地址并返回,head是单链表首结点指针
//请在以下位置补充完整,实现函数mid的功能
int cnt=0;
struct cell*p=head;
while(p!=NULL){
cnt++;
p=p->next;
}
cnt=(cnt+1)/2;
if(cnt<1)return NULL;
int n=1;
p=head;
while(n<cnt){
p=p->next;
n++;
}
return p;
}
void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针
while(head!=NULL){
printf("%d",head->x);
if(head->next!=NULL) printf(" ");
head=head->next;
}
}
void release(struct cell* head) {//释放单链表空间,head是单链表首结点指针
/*请在以下位置补充完整,实现函数release的功能
......
......
......
*/
}
int main(void) {
struct cell* head,*half;
head = build();
half = mid(head);
if(half!=NULL)
print(half);
else
printf("NULL");
release(head);
}
//combine two lists,the first one is already in order,add the second orderless list in it.
#include <stdio.h>
#include <malloc.h>
struct cell {//单链表结点结构体定义
int x;
struct cell* next;
};
struct cell* build(void) {//新建单链表,并将建好的单链表首结点地址返回
struct cell* head, * tmp, * tail;
head = tmp = tail = NULL;
int n, i;
head=(struct cell*)malloc(sizeof(struct cell));
head->next=NULL;
tmp=head;
scanf("%d",&n);
while(n!=0){
tail=(struct cell*)malloc(sizeof(struct cell));
tail->x=n;
tail->next=NULL;
tmp->next=tail;
tmp=tail;
scanf("%d",&n);
}
tmp=head;
head=head->next;
free(tmp);
return head;//返回单链表头
}
struct cell* combine(struct cell* p, struct cell* q) {//合并两个链表p和q
struct cell* head= NULL,*p0=NULL,*q0=NULL,*r=NULL;
if (p == NULL && q!= NULL) return q;
if (p != NULL && q == NULL) return p;
if (p == NULL && q == NULL) return NULL;
//请在以下位置补充完整,实现函数combine的功能
else{
head=p;
struct cell* guard,*t;
guard=(struct cell*)malloc(sizeof(struct cell));
guard->next=head;
head=guard;
t=head;
while(q!=NULL){
p0=guard;
p=guard->next;
while(p!=NULL&&p->x<q->x){
p0=p;
p=p->next;
}
r=(struct cell*)malloc(sizeof(struct cell));
r->x=q->x;
r->next=p;
p0->next=r;
q=q->next;
}
}
head=head->next;
return head;
}
void print(struct cell* head) {//打印整个单链表,head是单链表首结点指针
while(head!=NULL){
printf("%d",head->x);
if(head->next!=NULL) printf(" ");
head=head->next;
}
}
int main(void) {
struct cell* head1,*head2, *result;
head1 = build();
head2 = build();
result = combine(head1,head2);
if (result != NULL)
print(result);
else
printf("NULL");
return 0;
}