思維導圖
文章來源:http://www.zghlxwxcb.cn/news/detail-822479.html
練習
頭文件
#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
enum
{
FALSE=-1,
SUCCESS
};
typedef int datatype;
//定義節(jié)點結構體
//節(jié)點:數(shù)據(jù)域 指針域
typedef struct Node
{
//數(shù)據(jù)域:存儲數(shù)據(jù)元素
datatype data;
//指針域:存儲指針
struct Node *next;
}*Linklist;
Linklist creat();
Linklist insert_head(Linklist head,datatype element);
void output(Linklist head);
Linklist insert_rear(Linklist head,datatype element);
Linklist delete_head(Linklist head);
Linklist delete_rear(Linklist head);
Linklist insert_pos(Linklist head,int pos,datatype element);
Linklist delete_pos(Linklist head,int pos);
Linklist change_pos(Linklist head,int pos,datatype element);
void search_pos(Linklist head,int pos);
int search_element(Linklist head,datatype element);
Linklist rev(Linklist head);
int search_transpos(Linklist head,int pos);
Linklist chang_element(Linklist head,datatype key,datatype element);
Linklist delete_element(Linklist head,datatype key);
void Bubble(Linklist head);
void Select(Linklist head);
Linklist free_space(Linklist head);
#endif
自定義函數(shù)
#include "head.h"
/*
* function: 創(chuàng)建新節(jié)點
* @param [ in]
* @param [out]
* @return
*/
Linklist creat()
{
Linklist s=(Linklist)malloc(sizeof(struct Node));
if(NULL==s)
return NULL;
//成功則初始化
s->data=0;
s->next=NULL;
return s;
}
/*
* function: 頭插入
* @param [ in]
* @param [out]
* @return
* 如果形參頭指針發(fā)生指向的改變,則必須返回
*/
Linklist insert_head(Linklist head,datatype element)
{
//創(chuàng)建新節(jié)點s
Linklist s=creat();
s->data=element;
//判斷鏈表是否為空
if(head==NULL)
{
head=s;
}
else
{
s->next=head;
head=s;
}
return head;
}
/*
* function: 遍歷輸出
* @param [ in]
* @param [out]
* @return
*/
void output(Linklist head)
{
if(NULL==head)
return;
Linklist p=head;
while(p!=NULL)//也可以是while(p)
{
printf("%-5d",p->data);
p=p->next;
}
puts("");
}
/*
* function: 尾插入
* @param [ in]
* @param [out]
* @return
*/
Linklist insert_rear(Linklist head,datatype element)
{
Linklist s=creat();
s->data=element;
Linklist p=head;
if(NULL==head)
head=s;
else
{
while(p->next!=NULL)
{
p=p->next;
}
p->next=s;
}
return head;
}
/*
* function: 頭刪
* @param [ in]
* @param [out]
* @return
*/
Linklist delete_head(Linklist head)
{
if(head==NULL)
return NULL;
else
{
Linklist del=head;
head=head->next;
free(del);
del=NULL;
}
return head;
}
/*
* function: 尾刪
* @param [ in]
* @param [out]
* @return
*/
Linklist delete_rear(Linklist head)
{
if(head==NULL)
return NULL;
else if(head->next==NULL)
{
free(head);
head==NULL;
return head;
}
else
{
Linklist del=head;
while(del->next->next!=NULL)
{
del=del->next;
}
free(del->next);
del->next=NULL;
}
return head;
}
/*
* function: 計算鏈表長度
* @param [ in]
* @param [out]
* @return
*/
int length(Linklist head)
{
int len=0;
while(head!=NULL)
{
head=head->next;
len++;
}
return len;
}
/*
* function: 按任意位置插入
* @param [ in]
* @param [out]
* @return
*/
Linklist insert_pos(Linklist head,int pos,datatype element)
{
if(pos<0||pos>length(head)+1)
return head;
Linklist s=creat();
s->data=element;
if(pos==1)
{
head=insert_head(head,element);
return head;
}
Linklist p=head;
for(int i=1;i<pos-1;i++)
{
p=p->next;
}
s->next=p->next;
p->next=s;
return head;
}
/*
* function: 按任意位置刪除
* @param [ in]
* @param [out]
* @return
*/
Linklist delete_pos(Linklist head,int pos)
{
if(pos<0||pos>length(head)+1)
return head;
if(pos==1)
{
head=delete_head(head);
return head;
}
Linklist p=head;
for(int i=1;i<pos-1;i++)
{
p=p->next;
}
Linklist s=p->next;
p->next=s->next;
free(s);
s=NULL;
return head;
}
/*
* function: 按任意位置修改
* @param [ in]
* @param [out]
* @return
*/
Linklist change_pos(Linklist head,int pos,datatype element)
{
if(pos<0||pos>length(head)+1)
return head;
if(pos==1)
{
head->data=element;
return head;
}
Linklist p=head;
for(int i=1;i<pos;i++)
{
p=p->next;
}
p->data=element;
return head;
}
/*
* function: 按任意位置查找
* @param [ in]
* @param [out]
* @return
*/
void search_pos(Linklist head,int pos)
{
if(pos<0||pos>length(head))
return;
if(head==NULL)
return;
Linklist p=head;
for(int i=1;i<pos;i++)
{
p=p->next;
}
printf("%d\n",p->data);
}
/*
* function: 按任意元素查找
* @param [ in]
* @param [out]
* @return
*/
int search_element(Linklist head,datatype element)
{
if(head==NULL)
return FALSE;
Linklist p=head;
int pos=0;
while(p)
{
pos++;
if(p->data==element)
{
return pos;
}
p=p->next;
}
if(pos==0)
return FALSE;
}
/*
* function: 逆置鏈表
* @param [ in]
* @param [out]
* @return
*/
Linklist rev(Linklist head)
{
if(head==NULL)
return head;
Linklist p=head->next;
head->next=NULL;
Linklist t=p;
while(p)
{
t=p;
p=p->next;
t->next=head;
head=t;
}
return head;
}
/*
* function: 查找倒數(shù)第n個值
* @param [ in]
* @param [out]
* @return
*/
int search_transpos(Linklist head,int pos)
{
if(head==NULL)
return FALSE;
if(pos<0||pos>length(head))
return FALSE;
Linklist p=head;
Linklist q=head;
for(int i=0;i<pos;i++)
{
p=p->next;
}
while(p)
{
p=p->next;
q=q->next;
}
return q->data;
}
/*
* function: 按任意元素修改
* @param [ in]
* @param [out]
* @return
*/
Linklist chang_element(Linklist head,datatype key,datatype element)
{
if(head==NULL)
return NULL;
int pos=search_element(head,key);
head=change_pos(head,pos,element);
return head;
}
/*
* function: 按任意元素刪除
* @param [ in]
* @param [out]
* @return
*/
Linklist delete_element(Linklist head,datatype key)
{
if(head==NULL)
return NULL;
int pos=search_element(head,key);
head=delete_pos(head,pos);
return head;
}
/*
* function: 冒泡排序(從小到大)
* @param [ in]
* @param [out]
* @return
*/
void Bubble(Linklist head)
{
if(head==NULL)
return;
for(int i=0;i<length(head)-1;i++)
{
Linklist p=head;
for(int j=0;j<length(head)-i-1;j++)
{
if(p->data >p->next->data)
{
datatype temp=p->data;
p->data=p->next->data;
p->next->data=temp;
}
p=p->next;
}
}
}
/*
* function: 簡單選擇排序(從大到小)
* @param [ in]
* @param [out]
* @return
*/
void Select(Linklist head)
{
if(head==NULL)
return;
Linklist p=head;
for(int i=0;i<length(head)-1;i++)
{
datatype max=p->data;
Linklist q=p->next;
for(int j=i+1;j<length(head);j++)
{
if(p->data<q->data)
{
max=q->data;
}
q=q->next;
}
chang_element(head,max,p->data);
p->data=max;
p=p->next;
}
}
/*
* function: 釋放空間
* @param [ in]
* @param [out]
* @return
*/
Linklist free_space(Linklist head)
{
if(head==NULL)
return NULL;
for(int i=0;i<length(head);i++)
{
Linklist s=head;
head=head->next;
free(s);
s=NULL;
}
return head;
}
主函數(shù)
#include "head.h"
int main(int argc, const char *argv[])
{
//定義單鏈表的頭指針
Linklist head=NULL;
int n;
//插入的值
datatype element;
printf("請輸入鏈表長度\n");
scanf("%d",&n);
for(int i=0;i<n;i++)
{
printf("請輸入第%d個值:",i+1);
scanf("%d",&element);
//head=insert_head(head,element);
head=insert_rear(head,element);
}
// head=delete_head(head);
// head=delete_rear(head);
// 按位置插入
int pos;
/*
printf("請輸入要插入的位置");
scanf("%d",&pos);
printf("請輸入要插入的值");
scanf("%d",&element);
head=insert_pos(head,pos,element);
output(head);
printf("請輸入要刪除的位置");
scanf("%d",&pos);
head=delete_pos(head,pos);
output(head);
*/
/*
printf("請輸入要修改的位置");
scanf("%d",&pos);
printf("請輸入要修改后的值");
scanf("%d",&element);
head=change_pos(head,pos,element);
output(head);
printf("請輸入要查找的位置");
scanf("%d",&pos);
search_pos(head,pos);
*/
printf("請輸入要查找的值");
scanf("%d",&element);
pos=search_element(head,element);
printf("%d\n",pos);
// head=rev(head);
output(head);
/* printf("請輸入要查找倒數(shù)第幾個");
scanf("%d",&pos);
int num=search_transpos(head,pos);
printf("倒數(shù)第%d個的值為:%d\n",pos,num);
*/
int key;
printf("請輸入要修改的值");
scanf("%d",&key);
printf("請輸入修改后的值");
scanf("%d",&element);
head=chang_element(head,key,element);
output(head);
printf("請輸入要刪除的值");
scanf("%d",&element);
head=delete_element(head,element);
output(head);
Bubble(head);
output(head);
Select(head);
output(head);
free_space(head);
return 0;
}
效果圖
?文章來源地址http://www.zghlxwxcb.cn/news/detail-822479.html
到了這里,關于數(shù)據(jù)結構之鏈表的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!