3-7

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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • include<iostream> using namespace std; //單鏈表 typedef stru...
    jmychou閱讀 450評論 0 0
  • 內存:全局區+代碼區+堆區+棧區全局區:全局變量,static修飾的變量,const修飾的變量,常量,的存儲區域代...
    henry_bin閱讀 355評論 0 0
  • 一、內存分區:數據區+代碼區+堆區+棧區1、數據區:分為靜態數據區,全局變量區的存儲是放在一塊的。即static,...
    SuperDing閱讀 1,183評論 1 1
  • 結構體 基本定義:struct 結構名{//成員列表};成員列表:有基本數據類型定義的變量或者是構造類型的變量ex...
    霸王小閱讀 792評論 0 0
  • 誰會專注于誰的故事?如果可以請看完。 現在是凌晨的時間,而我明天還有一大堆的課要上,現在的自己處于一個關鍵...
    薄荷藍k閱讀 414評論 1 0