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);
}