严丽敏奶奶的数据结构单向链表的操作,自己写的
因为严奶奶的书里面好多伪代码 就自己写了 我不知道数据结构到底是学什么的。。这算是吗。很困惑 到底要怎么学数据结构呢.?#include<stdio.h>
#include<stdlib.h>
#define SIZE sizeof(struct List)
/* the List basic structure */
typedef struct List { //基本节点组成 以0作为结束标志!
int shuju;
struct List *next;
}Link;
/* the operate of create */
struct List* CreatList() //创建链表操作
{
int n = 1;
Link *Lis1,*Lis2,*head;
head = Lis1 = Lis2 = (Link*) malloc (SIZE);
printf("输入第1个数据:");
scanf ("%d",&Lis1->shuju);
while(Lis1->shuju)
{
if (n++ == 1) head = Lis1;
else Lis2->next = Lis1;
Lis2 = Lis1;
Lis1 = (Link*) malloc (SIZE);
printf("输入第%d个数据:",n);
scanf ("%d",&Lis1->shuju);
}
Lis2->next = Lis1;
return(head);
}
/* the output of chain */
void PutLink(Link *Lis) //输出链表操作
{
while(Lis->shuju)
{
printf("%d ",Lis->shuju);
Lis = Lis->next;
}
}
/* Insert operate */
struct List* Insert (Link *Lis,Link *add,int n) //插入链表操作
{
if (n == 1) {
add->next = Lis; //貌似如果没return的话 Lis还是会指向原来的地址
return (add);
}
else {
for (int y=1; y != n-1; y++)
Lis = Lis->next;
add->next = Lis->next;
Lis->next = add;
return(Lis);
}
}
/* the delete operate */
struct List* Delete(Link *Lis,int n) //删除链表第N个节点的操作
{
Link *del;
if(n == 1) {
del = Lis,Lis = Lis->next,free(del);
}
else {
for (int y=1; y != n-1;y++)
Lis = Lis->next;
del = Lis->next;
Lis->next = Lis->next->next;
free(del);
}
return (Lis);
}
/* sorts from small */
void Sort(Link *Lis) //链表排序操作 1大到小
{
int n;
Link *p1,*p2,*tail = Lis;
while(tail->shuju) tail = tail->next;
for (p1 = Lis;p1 != tail; p1 = p1->next)
for (p2 = p1->next; p2 != tail; p2 = p2->next)
if(p1->shuju < p2->shuju) {
n = p1->shuju;
p1->shuju = p2->shuju;
p2->shuju = n;}
}
/* operate of Combine */
struct List* Combine(Link *p1,Link *p2) //合并链表 随便合并没顺序排序之分 合并后的链表再来排序
{
Link *p12 = p1;
Link *head = p12;
p1 = p1->next;
while(p1->shuju && p2->shuju)
{
if(p1->shuju != 0) p12->next = p1,p12 = p1,p1 = p1->next;
if(p2->shuju != 0) p12->next = p2,p12 = p2,p2 = p2->next;
}
p12->next = p2;
return(head);
}
/* operate of Research */
void Research (Link *Lis,int who) //查找数据
{
int n = 0;
int press = 0;
while(Lis->shuju) {
n++;
if (who == Lis->shuju) press = 1,printf ("Find it!\n在第%d个节点里面有个相同数据\n",n);
Lis = Lis->next;
}
if (press == 0 ) printf ("Didn't reserch anything\n");
}
/* Acount knots */
void Acount (Link *Lis)
{
int n=0;
while(Lis->shuju)
{
n++;
Lis = Lis->next;
}
printf ("共%d个节点\n",n);
}
/* the main program */
void main()
{
}