单链表创建及操作
#include<stdio.h>
#include<stdlib.h>
typedef struct node//定义节点
{
int data;
struct node *next;
}Node,*Link;//struct node a,*p->typedef struct node Node,*Link
Link Create_Link()//将节点连成链表
{
Link head,cur;
int input;
head=(Link)malloc(sizeof(Node));//开辟Link类型的内存空间
if(head==NULL)//检测是否开辟空间
{
printf("malloc error");
exit(0);
}
head->next=NULL;
printf("Please input data:");
scanf("%d",&input);
while(input)//输入不为0则执行循环
{
cur=(Link)malloc(sizeof(Node));
cur->data=input;
cur->next=head->next;
head->next=cur;
scanf("%d",&input);
}
return head;
}
void Insert_Link(Link head)//插入一个节点
{
Link cur;
int input;
printf("Please input a new data:");
scanf("%d",&input);
cur=(Link)malloc(sizeof(Node));
cur->data=input;
cur->next=head->next;
head->next=cur;
}
Link Search_Link(Link head)//搜索一个节点
{
int find;
printf("Please input your find:");
scanf("%d",&find);
head=head->next;
while(head!=NULL)
{
if(head->data==find)
return head;
head=head->next;
}
return NULL;
}
void Delete_Link(Link head,Link pfind)//删除一个节点
{
while(head->next!=pfind)
{
head=head->next;
}
head->next=pfind->next;
free(pfind);
pfind=NULL;
}
void print_Link(Link head)//打印链表
{
head=head->next;
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
printf("\n");
}
int Size_Link(Link head)//计算链表长度
{
int size=0;
head=head->next;
while(head!=NULL)
{
size++;
head=head->next;
}
return size;
}
void Sort_Link(Link head,int size)//冒泡排序
{
int i,j;
Link list,p,q,tmp;
for(i=0;i<size-1;i++)
{
list=head;
p=list->next;
q=p->next;
for(j=0;j<size-1-i;j++)
{
if(p->data>q->data)
{
list->next=q;
p->next=q->next;
q->next=p;
//址排序后,需将指针p和q交换
tmp=p;
p=q;
q=tmp;
}
list=list->next;
p=p->next;
q=q->next;
}
}
}
void Destroy_Link(Link head)//释放内存空间
{
Link p;
while(head!=NULL)
{
p=head;
head=head->next;
free(p);
}
}
int main()
{
Link head,pfind;
int size;
head=Create_Link();
print_Link(head);
Insert_Link(head);
printf("After Insert:\n");
print_Link(head);
pfind=Search_Link(head);
if(pfind==NULL)
printf("find none\n");
else
{
printf("Your find is %d\n",pfind->data);
Delete_Link(head,pfind);
printf("After Delete:\n");
print_Link(head);
}
size=Size_Link(head);
printf("The size is %d\n",size);
Sort_Link(head,size);
printf("After sort:\n");
print_Link(head);
Destroy_Link(head);
return 0;
}
双链表创建及操作 #include <stdio.h> #include <stdlib.h> typedef struct node//创建双链表节点 { int data; struct node *pre;//前驱,指向前一个节点地址 struct node *next;//后继,指向后一个节点地址 }Node,*Link; Link CreateLink()//连接双链表节点 { Link head,cur,list; int input; head=(Link)malloc(sizeof(Node)); if(head==NULL) { printf("Malloc error"); exit(0); } list=head; head->pre=head->next=NULL; printf("Please input data:"); scanf("%d",&input); while(input) { cur=(Link)malloc(sizeof(Node)); cur->data=input; list->next=cur; cur->pre=list; list=cur; scanf("%d",&input); } cur->next=head; head->pre=cur; return head; } void InsertLink(Link head)//插入节点 { int input; Link cur; printf("Pleasw input new node:"); scanf("%d",&input); cur=(Link)malloc(sizeof(Node)); if(cur==NULL) { printf("Malloc error!"); exit(0); } cur->data=input; cur->pre=head; cur->next=head->next; head->next=cur; cur->next->pre=cur; } Link SearchLink(Link head) { int find; Link list=head; printf("Please input your find:"); scanf("%d",&find); list=list->next; while(list!=head) { if(find==list->data) { return list; } list=list->next; } return NULL; } void DeleteLink(Link pfind) { if(pfind==NULL) { printf("delete none!"); return; } pfind->pre->next=pfind->next; pfind->next->pre=pfind->pre; free(pfind); } int SizeLink(Link head) { Link list=head; int size=0; list=list->next; while(list!=head) { size++; list=list->next; } return size; } void SortLink(Link head,int size)//冒泡排序 { int i,j; Link p,q,tmp; for(i=0;i<size-1;i++) { p=head->next; q=p->next; for(j=0;j<size-1-i;j++) { if(p->data>q->data) { p->pre->next=q; q->pre=p->pre; p->pre=q; p->next=q->next; q->next=p; p->next->pre=p; tmp=p; p=q; q=tmp; } p=p->next; q=q->next; } } } void PrintLink(Link head)//打印链表 { Link list=head; list=list->next; while(list!=head) { printf("%d ",list->data); list=list->next; } printf("\n"); } void DestroyLink(Link head)//释放内存 { Link p; head->pre->next=NULL;//为节点的后继设为NULL(单链表思想) while(head!=NULL) { p=head; head=head->next; free(p); } } int main() { Link head,pfind; int size; head=CreateLink(); PrintLink(head); InsertLink(head); printf("after insert\n"); PrintLink(head); pfind=SearchLink(head); if(pfind==NULL) printf("find none\n"); else { printf("Your find is %d\n",pfind->data); DeleteLink(pfind); printf("after delete:\n"); PrintLink(head); } size=SizeLink(head); printf("The size is:%d\n",size); SortLink(head,size); printf("After sort:\n"); PrintLink(head); DestroyLink(head); return 0; }