#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<string.h>
using namespace std;
typedef struct student
{
int data;
struct student *next; //链指针
}node;
node *create() //创建单链表
{
node *head, *p, *s;
int x , cycle = 1;
head = (node*)malloc(sizeof(node));
p = head;
cout<<"Input the data:"<<endl;
while(cycle)
{
cin>>x;
if(x != 0)
{
s = (node*)malloc(sizeof(node));
s->data = x;
//cout<<s->data;
p->next = s;
p = s;
}
else cycle = 0;
}
head = head->next;
p ->next = NULL;
//cout<<head->data;
return head;
}
int length(node *head) //求单链表的长度
{
int n = 0;
node *p ;
p = head;
while(p != NULL)
{
p = p->next;
n++;
}
return n;
}
void print(node *head) //打印链表
{
node *p;
int n = length(head);
p = head;
while(p != NULL)
{
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
void del(node *head, int num) //删除链表中某一节点
{
node *p1, *p2;
p1 = head;
while(num != p1->data && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(num == p1->data)
{
if(p1 == head)
{
head = p1->next;
free(p1);
}
else
{
p2 ->next = p1 ->next;
}
}
else
cout<<num<<"counld not been found";
//return head;
}
void insert(node *head, int num)//在链表中按大小顺序插入某节点
{
node *p0, *p1, *p2;
p0 = (node*)malloc(sizeof(node));
p0->data = num;
p1 = head;
while(p0->data > p1->data && p1->next !=NULL)
{
p2 = p1;
p1 = p1->next;
}
if(p0->data <= p1->data)
{
if(head == p1)
{
p0->next = p1;
head = p0;
}
else
{
p2->next = p0;
p0->next = p1;
}
}
else
{
p1->next = p0;
p0->next = NULL;
}
}
node *sort(node *head)//单链表的排序
{
node *p1, *p2;
int n, temp;
n = length(head);
if( head == NULL ||head->next == NULL)
return head;
for(int i = 1; i <= n; i++)
{
p1 = head;
for(int j = 0; j < n-1; j++)
{
if(p1->data > p1->next->data)
{
temp = p1->data;
p1->data = p1->next->data;
p1->next->data = temp;
}
p1 = p1->next;
}
}
return head;
}
node *reverse(node *head)
{
node *p1,*p2,*p3;
if(head==NULL || head->next == NULL)
return head;
p1 = head; p2 = p1->next;
while(p2)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
head->next = NULL;
head = p1;
}
void RemoveHead(node *head) //删除头节点
{
node *p;
p = head->next;
head ->next = p ->next;
free(p);
}
void searchMid(node *head)
{
node *p1, *p2;
node *mid;
p1 = head;
p2 = head;
while(p1->next->next != NULL)
{
p1 = p1->next->next;
p2 = p2->next;
mid = p2;
}
cout<<mid->data<<endl;
}
int main()
{
node *head;
head = create();
int l,de_num,in_num;
l = length(head);
cout<<"链表长度:"<<l<<endl;
print(head);
//head = del(head,2);
cout<<"请输入需要删除的值:"<<endl;
cin>>de_num;
del(head,de_num);
l = length(head);
cout<<"链表长度:"<<l<<endl;
print(head);
cout<<"请输入需要插入的值:"<<endl;
cin>>in_num;
insert(head,in_num);
l = length(head);
cout<<"链表长度:"<<l<<endl;
print(head);
head = sort(head);
cout<<"排序后的结果:"<<endl;
print(head);
head = reverse(head);
cout<<"逆置后的结果:"<<endl;
print(head);
RemoveHead(head);
cout<<"删除头结点后的结果:"<<endl;
print(head);
cout<<"链表中间值是:"<<endl;
searchMid(head);
return 0;
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
using namespace std;
typedef struct student
{
int data;
struct student *next; //链指针
}node;
node *create() //创建单链表
{
node *head, *p, *s;
int x , cycle = 1;
head = (node*)malloc(sizeof(node));
p = head;
cout<<"Input the data:"<<endl;
while(cycle)
{
cin>>x;
if(x != 0)
{
s = (node*)malloc(sizeof(node));
s->data = x;
//cout<<s->data;
p->next = s;
p = s;
}
else cycle = 0;
}
head = head->next;
p ->next = NULL;
//cout<<head->data;
return head;
}
int length(node *head) //求单链表的长度
{
int n = 0;
node *p ;
p = head;
while(p != NULL)
{
p = p->next;
n++;
}
return n;
}
void print(node *head) //打印链表
{
node *p;
int n = length(head);
p = head;
while(p != NULL)
{
cout<<p->data<<" ";
p = p->next;
}
cout<<endl;
}
void del(node *head, int num) //删除链表中某一节点
{
node *p1, *p2;
p1 = head;
while(num != p1->data && p1->next != NULL)
{
p2 = p1;
p1 = p1->next;
}
if(num == p1->data)
{
if(p1 == head)
{
head = p1->next;
free(p1);
}
else
{
p2 ->next = p1 ->next;
}
}
else
cout<<num<<"counld not been found";
//return head;
}
void insert(node *head, int num)//在链表中按大小顺序插入某节点
{
node *p0, *p1, *p2;
p0 = (node*)malloc(sizeof(node));
p0->data = num;
p1 = head;
while(p0->data > p1->data && p1->next !=NULL)
{
p2 = p1;
p1 = p1->next;
}
if(p0->data <= p1->data)
{
if(head == p1)
{
p0->next = p1;
head = p0;
}
else
{
p2->next = p0;
p0->next = p1;
}
}
else
{
p1->next = p0;
p0->next = NULL;
}
}
node *sort(node *head)//单链表的排序
{
node *p1, *p2;
int n, temp;
n = length(head);
if( head == NULL ||head->next == NULL)
return head;
for(int i = 1; i <= n; i++)
{
p1 = head;
for(int j = 0; j < n-1; j++)
{
if(p1->data > p1->next->data)
{
temp = p1->data;
p1->data = p1->next->data;
p1->next->data = temp;
}
p1 = p1->next;
}
}
return head;
}
node *reverse(node *head)
{
node *p1,*p2,*p3;
if(head==NULL || head->next == NULL)
return head;
p1 = head; p2 = p1->next;
while(p2)
{
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
}
head->next = NULL;
head = p1;
}
void RemoveHead(node *head) //删除头节点
{
node *p;
p = head->next;
head ->next = p ->next;
free(p);
}
void searchMid(node *head)
{
node *p1, *p2;
node *mid;
p1 = head;
p2 = head;
while(p1->next->next != NULL)
{
p1 = p1->next->next;
p2 = p2->next;
mid = p2;
}
cout<<mid->data<<endl;
}
int main()
{
node *head;
head = create();
int l,de_num,in_num;
l = length(head);
cout<<"链表长度:"<<l<<endl;
print(head);
//head = del(head,2);
cout<<"请输入需要删除的值:"<<endl;
cin>>de_num;
del(head,de_num);
l = length(head);
cout<<"链表长度:"<<l<<endl;
print(head);
cout<<"请输入需要插入的值:"<<endl;
cin>>in_num;
insert(head,in_num);
l = length(head);
cout<<"链表长度:"<<l<<endl;
print(head);
head = sort(head);
cout<<"排序后的结果:"<<endl;
print(head);
head = reverse(head);
cout<<"逆置后的结果:"<<endl;
print(head);
RemoveHead(head);
cout<<"删除头结点后的结果:"<<endl;
print(head);
cout<<"链表中间值是:"<<endl;
searchMid(head);
return 0;
}