-
C第十天
今天一天都在講鏈表,下午講了單項(xiàng)循環(huán)鏈表,而且被同樣的題型虐了4遍,這4道題目都是相似,看著也不是很難,畢竟知道解題思路,但就是不能轉(zhuǎn)化成代碼表達(dá)出來(lái)巩剖。仿照例題也寫(xiě)不出答案,真的需要好好消化一下鏈表的內(nèi)容者甲。
#include <stdio.h>
#include <stdlib.h>
struct student
{
int data;
struct student *next;
};
struct student *Create()
{
int n,i;
printf("要?jiǎng)?chuàng)建幾個(gè)節(jié)點(diǎn):");
scanf("%d",&n);getchar();
struct student *head=(struct student *)malloc(sizeof(struct student));
struct student *p=(struct student *)malloc(sizeof(struct student));
printf("請(qǐng)輸入數(shù)據(jù):");
scanf("%d",&p->data);getchar();
head->next=p;
for(i=1;i<n;i++)
{
struct student *q=(struct student *)malloc(sizeof(struct student));
printf("請(qǐng)輸入數(shù)據(jù):");
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("請(qǐng)輸入數(shù)據(jù):");
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("請(qǐng)輸入數(shù)據(jù):");
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("請(qǐng)輸入要查找的值:");
scanf("%d",&data);
while(p->next && p->data!=data)
{
p=p->next;
}
if(p->data==data)
return p;
else
{
printf("沒(méi)有找到球切!\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);
}
練習(xí):輸入一行字符串弃舒,并以此建立一條鏈表,一個(gè)字符占一個(gè)節(jié)點(diǎn)堤尾,并對(duì)該字符串逆序輸出
如:abcde
edcba
#include <stdio.h>
#include <stdlib.h>
//確定鏈節(jié)的類型
struct node
{
char info;
struct node *next;
};
void main()
{ //創(chuàng)建一條鏈表
struct node *head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
char c;
while((c=getchar())!='\n')
{//新建節(jié)點(diǎn)
struct node *q=(struct node *)malloc(sizeof(struct node));
q->info=c;
//頭插
q->next=head->next;
head->next=q;
}
//打印鏈表的數(shù)據(jù)
struct node *p=head->next;
while(p)
{
putchar(p->info);
p=p->next;
}
putchar('\n');
}
練習(xí):從鍵盤(pán)輸入2行字符服赎,并分別按輸入時(shí)的逆序建立2條鏈表并分別,然后合并2條鏈表為1條鏈表并輸出合并后的鏈表梁沧。
#include <stdio.h>
#include <stdlib.h>
//確定鏈節(jié)的類型
struct node
{
char info;
struct node *next;
};
struct node *Create()
{ //創(chuàng)建一條鏈表
struct node *head=(struct node *)malloc(sizeof(struct node));
head->next=NULL;
char c;
while((c=getchar())!='\n')
{//新建節(jié)點(diǎn)
struct node *q=(struct node *)malloc(sizeof(struct node));
q->info=c;
//頭插
q->next=head->next;
head->next=q;
}
return head;
}
//打印鏈表的數(shù)據(jù)
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);
}
練習(xí):從鍵盤(pán)輸入一串字符檀何,并以此建立一條正序的單項(xiàng)循環(huán)鏈表并輸出。
#include <stdio.h>
#include <stdlib.h>
//確定鏈節(jié)類型
struct node
{
char data;
struct node *next;
};
struct node *Create()
{ //創(chuàng)建一條單向循環(huán)鏈表
struct node *head=(struct node *)malloc(sizeof(struct node));head->next=head;
struct node *last=head;//last指向鏈表的最后一個(gè)節(jié)點(diǎn)
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);
}
練習(xí):從鍵盤(pán)輸入2串字符串廷支,分別創(chuàng)建單向循環(huán)鏈表频鉴,然后把2個(gè)環(huán)合并成一個(gè)環(huán)并輸出。
#include <stdio.h>
#include <stdlib.h>
//確定鏈節(jié)類型
struct node
{
char data;
struct node *next;
};
struct node *Create()
{ //創(chuàng)建一條單向循環(huán)鏈表
struct node *head=(struct node *)malloc(sizeof(struct node));head->next=head;
struct node *last=head;//last指向鏈表的最后一個(gè)節(jié)點(diǎn)
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);
}