# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
# include <stdbool.h>
typedef struct node
{
int data;
struct node *pNext;
}Node,*pNode;
pNode creat_list ();
void travese_list ();
bool is_empty (pNode );
int length_empty (pNode );
void sort_list (pNode,int );
bool insert_list (pNode );
bool delete_list (pNode );
bool inversion_list (pNode );
int main ()
{
pNode head = NULL;
int len;
head = creat_list (); //創建一個非回圈單鏈表
travese_list (head); //遍歷此單鏈表
if (is_empty(head))
printf ("鏈表為空!\n");
else
printf ("鏈表不為空!\n");
len=length_empty (head); //計算鏈表的長度
sort_list (head,len); //對鏈表中的元素進行排序
travese_list (head); //輸出排序后的鏈表
insert_list (head); //插入一個元素
travese_list (head); //輸出插入后的新鏈表
delete_list(head); //洗掉鏈表中的某個資料
travese_list (head); //輸出洗掉后的新鏈表
inversion_list (head);
travese_list (head);
return 0;
}
//創建一個鏈表(帶頭節點的尾插法)
pNode creat_list ()
{
int len; //節點個數
int i; //回圈變數
int val; //用來保存資料域的資料
pNode head=(pNode)malloc(sizeof(Node));
pNode Tail = head; //定義一個尾結點指向頭結點
Tail->pNext = NULL; //尾結點的指標域置空
if (head == NULL)
{
printf ("鏈表創建失敗!");
exit (-1);
}
else
{
printf ("您想創建幾個節點?");
scanf ("%d",&len);
for (i=0;i<len;i++)
{
printf ("請輸入第%d個節點的值:",i+1);
scanf ("%d",&val);
pNode pNew = (pNode)malloc(sizeof(Node)); //創建一個新的臨時結點
pNew->data = val; //給新節點的資料域賦值
Tail->pNext = pNew; //將信結點掛在前一個結點的后面
pNew->pNext = NULL; //新節點的資料域置空
Tail = pNew; //尾結點指向新節點
}
}
return head; //回傳頭指標
}
//遍歷此鏈表
void travese_list (pNode head)
{
pNode p = head->pNext; //將頭結點的指標域賦給p
while (p!=NULL) //判斷指標域是否為空
{
printf ("%d ",p->data); //輸出資料域中的資料
p=p->pNext; //指標向后移動一個位置
}
printf ("\n");
return;
}
//判斷鏈表是否為空
bool is_empty (pNode head)
{
if (head->pNext == NULL)
return true;
else
return false;
}
//計算鏈表的長度
int length_empty (pNode head)
{
int cnt=0;
pNode p = head->pNext; //p指向首節點
while (p!=NULL) //判斷p的指向是否為空
{
cnt++;
p=p->pNext;
}
printf ("這個鏈表的長度為:%d\n",cnt);
return cnt;
}
//對鏈表的元素進行排序
void sort_list (pNode head,int len)
{
pNode p,q;
pNode temp; //int k
int i,j,t;
printf ("鏈表排序如下:\n");
for (i=0,p=head->pNext;i<len-1;i++,p=p->pNext) //p相當于陣列中的a[i]
{
temp=p; //將下標保存下來,相當于k=i;
for (j=i+1,q=p->pNext;j<len;j++,q=q->pNext) //q相當于陣列中的a[j]
if (temp->data > q->data) //相當于a[k]>a[j]
temp=q; //相當于k=j
if (temp!=p) //相當于k!=i;
{
t=temp->data; //相當于t=a[k]
temp->data = p->data; //相當于a[k]=a[i]
p->data = t; //相當于a[i]=t
}
}
return;
}
//在鏈表中插入一個資料
bool insert_list (pNode head)
{
int i=0,pos,val;
pNode p=head,q;
printf ("您想在第幾個位置插入資料:");
scanf ("%d",&pos);
printf ("您想插入的數字是:");
scanf ("%d",&val);
while (p!=NULL && i<pos-1)
{
p=p->pNext;
i++;
}
if (i>pos-1 || p==NULL) //判斷要插入的資料的位置是否超過鏈表的長度+1
return false;
pNode pNew = (pNode)malloc(sizeof(Node)); //創建一個新的節點
if (pNew == NULL)
{
printf ("節點創建失敗!");
exit (-1);
}
pNew->data = val; //將要插入的資料存在新節點的資料域
q = p->pNext; //將要插入節點的資料域先保存到q里面
p->pNext = pNew; //讓p指向新節點
pNew->pNext = q; //新節點的指向p后面的節點q
return true;
}
//洗掉鏈表中的某個資料
bool delete_list(pNode head)
{
int i=0,pos;
pNode p=head,q;
printf ("您想在第幾個位置洗掉資料:");
scanf ("%d",&pos);
while (p->pNext!=NULL && i<pos-1)
{
p=p->pNext;
i++;
}
if (i>pos-1 || p->pNext==NULL) //判斷要洗掉的資料的位置是否超過鏈表的長度+1
{
printf ("洗掉失敗!");
return false;
}
q=p->pNext; //將要洗掉的節點保存起來
p->pNext=q->pNext; //指向后一個節點
free(q); //釋放掉要洗掉節點的記憶體
q = NULL;
return true;
}
//翻轉鏈表
bool inversion_list (pNode head)
{
pNode tmp = NULL; //定義一個臨時節點
pNode p = NULL; //定義p為頭結點
printf ("翻轉后的鏈表是:\n");
if (head == NULL) //判斷鏈表是否為空
{
return false;
}
tmp = head->pNext; //tmp指向第一個節點
while (tmp->pNext != NULL) //判斷臨時節點是否為空
{
p = tmp->pNext; //p指向第二個節點
tmp->pNext = p->pNext; //第一個節點指向第三個節點
p->pNext = head->pNext; //第二個節點指向第一個節點
head->pNext = p; //頭結點的指標域指向p
}
return true;
}
uj5u.com熱心網友回復:
這是成果展示?轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/41205.html
標籤:C語言
