單鏈表很全的例子,增加,刪除,排序,都有了
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int nDate;
struct node *pstnext;
}Node;
//鏈表輸出
void output(Node *head)
{
Node *p = head->pstnext;
while(NULL != p)
{
printf("%d ", p->nDate);
p = p->pstnext;
}
printf("\r\n");
}
//鏈表建立
Node* creat()
{
Node *head = NULL, *p = NULL, *s = NULL;
int Date = 0, cycle = 1;
head = (Node*)malloc(sizeof(Node));
if(NULL == head)
{
printf("分配內存失敗\r\n");
return NULL;
}
head->pstnext = NULL;
p = head;
while(cycle)
{
printf("請輸入數據且當輸入數據為0時結束輸入\r\n");
scanf("%d", &Date);
if(0 != Date)
{
s = (Node*)malloc(sizeof(Node));
if(NULL == s)
{
printf("分配內存失敗\r\n");
return NULL;
}
s->nDate = Date;
p->pstnext = s;
p = s;
}
else
{
cycle = 0;
}
}
p->pstnext = NULL;
return(head);
}
//單鏈表測長
void length(Node *head)
{
Node *p = head->pstnext;
int j=0;
while(NULL != p)
{
p = p->pstnext;
j++;
}
printf("%d\r\n", j);
}
//鏈表按值查找
void research_Date(Node *head, int date)
{
Node *p;
int n=1;
p = head->pstnext;
while(NULL != p && date != p->nDate)
{
p = p->pstnext;
++n;
}
if(NULL == p)
{
printf("鏈表中沒有找到該值");
}else if(date == p->nDate)
{
printf("要查找的值%d在鏈表中第%d個位置\r\n", date, n);
}
return;
}
//按序號查找
void research_Number(Node *head, int Num)
{
Node *p=head;
int i = 0;
while(NULL != p && i < Num)
{
p = p->pstnext;
i++;
}
if(p == NULL)
{
printf("查找位置不合法\r\n");
}else if(i == 0)
{
printf("查找位置為頭結點\r\n");
}else if(i == Num)
{
printf("第%d個位置數據為%d\r\n", i, p->nDate);
}
}
//在指定元素之前插入新結點
void insert_1(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre && j < i-1)
{
pre = pre->pstnext;
j++;
}
if(NULL == pre || j > i-1)
{
printf("插入位置不存在\r\n");
}else
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配內存失敗\r\n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}
}
//在指定元素之後插入新結點
void insert_2(Node *head, int i, int Newdate)
{
Node *pre = head, *New = NULL;
int j = 0;
while(NULL != pre->pstnext && j < i)
{
pre = pre->pstnext;
j++;
}
if(j == i)
{
New = (Node*)malloc(sizeof(Node));
if(NULL == New)
{
printf("分配內存失敗\r\n");
return;
}
New->nDate = Newdate;
New->pstnext = pre->pstnext;
pre->pstnext = New;
}else
{
printf("插入位置不存在\r\n");
}
}
//刪除指定結點
void Delete_1(Node *head, int i3)
{
Node *p = head, *pre = NULL;
int j = 0;
while(NULL != p && j < i3)
{
pre = p;
p = p->pstnext;
j++;
}
if(NULL == p)
{
printf("刪除位置不存在\r\n");
}else
{
pre->pstnext = p->pstnext;
free(p);
}
}
//指定刪除單鏈表中某個數據,並統計刪除此數據的個數
int Delete_2(Node *head, int Delete_date)
{
int count = 0;
Node *p = head, *q;
while(NULL != p->pstnext)
{
q = p->pstnext;
if(q->nDate == Delete_date)
{
p->pstnext = q->pstnext;
free(q);
++count;
}
else
{
p = q;
}
}
return count;
}
//鏈表逆置
void Reverse_list(Node *head)
{
Node *q, *s;
if(NULL == head->pstnext || NULL == head->pstnext->pstnext)
{
return;
}
q = head->pstnext->pstnext;
head->pstnext->pstnext = NULL;
while(NULL != q)
{
s = q->pstnext;
q->pstnext = head->pstnext;
head->pstnext = q;
q = s;
}
}
//單鏈表的連接
void connect_list(Node *head, Node *head_New)
{
Node *p = head;
while(NULL != p->pstnext)
{
p = p->pstnext;
}
p->pstnext = head_New->pstnext;
}
//單鏈表銷毀
void destroy_list(Node* head)
{
while (NULL != head)
{
Node* temp = head;
head = head->pstnext;
free(temp);
}
}
main()
{
int date, num; //待查找數據
int i3; //指定刪除元素的位置
int i1, i2, Newdate_1, Newdate_2; //待插入的新數據
int Delete_date, k; //待刪除的數據與其個數
Node *Head = NULL; //定義頭結點
Node *Head_New = NULL;
//鏈表建立
Head = creat();
printf("輸出建立的單鏈表\r\n");
output(Head);
//單鏈表測長
printf("單鏈表長度為\r\n");
length(Head);
//鏈表按值查找
printf("請輸入待查找的數據\r\n");
scanf("%d", &date);
research_Date(Head, date);
//鏈表按序號查找
printf("請輸入待查找序號\r\n");
scanf("%d", &num);
research_Number(Head, num);
//在指定第i1個元素之前插入新元素Newdate
printf("在指定第i個元素之前插入新元素Newdate");
printf("請輸入i與元素且以逗號間隔\r\n");
scanf("%d,%d", &i1, &Newdate_1);
insert_1(Head, i1, Newdate_1);
printf("插入後新鏈表\r\n");
output(Head);
//在指定第i2個元素之後插入新元素Newdate
printf("在指定第i個元素之後插入新元素Newdate");
printf("請輸入i與元素且以逗號間隔\r\n");
scanf("%d,%d", &i2, &Newdate_2);
insert_2(Head, i2, Newdate_2);
printf("插入後新鏈表\r\n");
output(Head);
//指定刪除i3元素
printf("刪除元素的位置\r\n");
scanf("%d", &i3);
Delete_1(Head, i3);
printf("刪除後新鏈表\r\n");
output(Head);
//指定刪除單鏈表中某個數據,並統計刪除此數據的個數
printf("請輸入待刪除的元素\r\n");
scanf("%d", &Delete_date);
k = Delete_2(Head, Delete_date);
printf("刪除後新鏈表\r\n");
output(Head);
printf("刪除指定元素在鏈表中的個數為:");
printf("%d\r\n", k);
//單鏈表逆置
Reverse_list(Head);
printf("逆置後輸出\r\n");
output(Head);
//單鏈表的連接
printf("建立一個新鏈表\r\n");
Head_New = creat();
printf("輸出新鏈表");
output(Head);
printf("將新鏈表連接到原來鏈表的尾部並輸出\r\n");
connect_list(Head, Head_New);
output(Head);
destroy_list(Head);
}
下面是輸出結果: