-
C第十天
今天一天都在讲链表,下午讲了单项循环链表,而且被同样的题型虐了4遍,这4道题目都是相似,看着也不是很难,毕竟知道解题思路,但就是不能转化成代码表达出来。仿照例题也写不出答案,真的需要好好消化一下链表的内容。
#include <stdio.h>
#include <stdlib.h>
struct student
{
int data;
struct student *next;
};
struct student *Create()
{
int n,i;
printf("要创建几个节点:");
scanf("%d",&n);getchar();
struct student *head=(struct student *)malloc(sizeof(struct student));
struct student *p=(struct student *)malloc(sizeof(struct student));
printf("请输入数据:");
scanf("%d",&p->data);getchar();
head->next=p;
for(i=1;i<n;i++)
{
struct student *q=(struct student *)malloc(sizeof(struct student));
printf("请输入数据:");
scanf("%d",&q->data);getchar();
p->next=q;
p=q;
}
p->next=NULL;
return head;
}
struct student *T_insert(struct student *head)
{
struct student *q=(struct student *)malloc(sizeof(struct student));
printf("请输入数据:");
scanf("%d",&q->data);
q->next=head->next;
head->next=q;
return head;
}
struct student *W_insert(struct student *head)
{
struct student *q=(struct student *)malloc(sizeof(struct student));
printf("请输入数据:");
scanf("%d",&q->data);
struct student *p=head->next;
while(p->next)
{
p=p->next;
}
p->next=q;
q->next=NULL;
return head;
}
void Print(struct student *head)
{
struct student *p=head->next;
while(p)
{
printf("%d\n",p->data);
p=p->next;
}
}
struct student *Find2(struct student *head)
{
int data;
struct student *p=head;
printf("请输入要查找的值:");
scanf("%d",&data);
while(p->next && p->data!=data)
{
p=p->next;
}
if(p->data==data)
return p;
else
{
printf("没有找到!\n");
return NULL;
}
}
struct student *Delete(struct student *head)
{ struct student *p=head;
struct student *q=Find2(head);
if(head->next==q)
{ head->next=q->next;free(q);
return head;
}
else if(NULL==q->next)
{ while(p)
{ p=p->next;
if(p->next==q)
{ p->next=NULL;free(q);
return head;}
}
}
else
{
while(p)
{
p=p->next;
if(p->next==q)
{
p->next=q->next;
free(q);
return head;
}
}
}
}
void main()
{
struct student *head=Create();
Print(head);
head=Delete(head);
Print(head);
}
练习:输入一行字符串,并以此建立一条链表,一个字符占一个节点,并对该字符串逆序输出
如:abcde
edcba
#include <stdio.h>
#include <stdlib.h>
//确定链节的类型
struct node
{
char info;
struct node *next;
};
void main()
{ //创建一条链表
struct node *head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
char c;
while((c=getchar())!='\n')
{//新建节点
struct node *q=(struct node *)malloc(sizeof(struct node));
q->info=c;
//头插
q->next=head->next;
head->next=q;
}
//打印链表的数据
struct node *p=head->next;
while(p)
{
putchar(p->info);
p=p->next;
}
putchar('\n');
}
练习:从键盘输入2行字符,并分别按输入时的逆序建立2条链表并分别,然后合并2条链表为1条链表并输出合并后的链表。
#include <stdio.h>
#include <stdlib.h>
//确定链节的类型
struct node
{
char info;
struct node *next;
};
struct node *Create()
{ //创建一条链表
struct node *head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
char c;
while((c=getchar())!='\n')
{//新建节点
struct node *q=(struct node *)malloc(sizeof(struct node));
q->info=c;
//头插
q->next=head->next;
head->next=q;
}
return head;
}
//打印链表的数据
void Print(struct node *head)
{
struct node *p=head->next;
while(p)
{
putchar(p->info);
p=p->next;
}
putchar('\n');
}
struct node *Sum(struct node *head1,struct node *head2)
{
struct node *p=head1->next;
while(p->next)
{
p=p->next;
}
p->next=head2->next;
return head1;
}
void main()
{
struct node *head1=Create();
Print(head1);
struct node *head2=Create();
Print(head2);
struct node *head=Sum(head1,head2);
Print(head);
}
练习:从键盘输入一串字符,并以此建立一条正序的单项循环链表并输出。
#include <stdio.h>
#include <stdlib.h>
//确定链节类型
struct node
{
char data;
struct node *next;
};
struct node *Create()
{ //创建一条单向循环链表
struct node *head=(struct node *)malloc(sizeof(struct node));head->next=head;
struct node *last=head;//last指向链表的最后一个节点
char c;
while((c=getchar())!='\n')
{
struct node *q=(struct node *)malloc(sizeof(struct node));
q->data=c; last->next=q; last=q;
}
last->next=head;
return last;
}
void Print(struct node *tail)
{ struct node *p=tail->next->next;
while(p!=tail->next) //p!=head
{
putchar(p->data);p=p->next;
}
putchar('\n');
}
void main()
{
struct node *tail=Create();
Print(tail);
}
练习:从键盘输入2串字符串,分别创建单向循环链表,然后把2个环合并成一个环并输出。
#include <stdio.h>
#include <stdlib.h>
//确定链节类型
struct node
{
char data;
struct node *next;
};
struct node *Create()
{ //创建一条单向循环链表
struct node *head=(struct node *)malloc(sizeof(struct node));head->next=head;
struct node *last=head;//last指向链表的最后一个节点
char c;
while((c=getchar())!='\n')
{
struct node *q=(struct node *)malloc(sizeof(struct node));
q->data=c; last->next=q; last=q;
}
last->next=head;
return last;
}
void Print(struct node *tail)
{ struct node *p=tail->next->next;
while(p!=tail->next) //p!=head
{
putchar(p->data);p=p->next;
}
putchar('\n');
}
struct node *Sum(struct node *tail1,struct node *tail2)
{
struct node *head=tail1->next;
tail1->next=tail2->next->next;
tail2->next=head;
return tail2;
}
void main()
{
struct node *tail1=Create();
Print(tail1);
struct node *tail2=Create();
Print(tail2);
struct node *tail=Sum(tail1,tail2);
Print(tail);
}
网友评论