//一直觉得学计算机归根到底都是算法。试着回忆了一下链表的知识
//单向链表结构
typedef struct node
{
int info;
struct node *next; // 再加类似这样的一条语句就是双向链表了
}node;
//单向链表首部插入结点
node *push_front(node *head,int info)
{
node* p=(node *)malloc(sizeof(node));
p->next=head;
p->data=info;
return p;
}
//返回链表的第一个元素
int front_element(node *head)
{
return first(head)->data;
}
node* first(node* head)
{
return head;
}
//返回链表最后一个元素
int back_element(node* head)
{
rerurn last(head)->data;
}
node* last(node* head)
{
node *p=head;
for (;p->next!=NULL;p=p->next);
return p;
}
//记录单向链表中结点的个数
int count(node *head)
{
int numberofnodes=0;
node *p=head;
if (p==NULL)
return p;
else
{
for (;p!=NULL;p=p->next)
numberofnodes++;
return numberofnodes;
}
}
//得到单向链表中数据项出现的频率
int frequency(node *head,int value)
{
int freq=0;
node *p=head;
for (;p!=NULL;p=p->next)
if (p->data==value)
freq++;
return freq;
}
//搜索单向链表中特定数据项
int searchindex(node *head,int s)
{
int search_status=NOTFOUND;
int c=0;
node *p=head;
for (;p!=NULL;p=p->next)
{
c++;
if (s==p->data)
{
search_status=FOUND;
break;
}
}
if (search_status==FOUND)
return c;
else
return -1;
}
//在单向链表中返回特定结点的地址
node* get_address(node* head,int index)
{
node *p=head;
int c=0;
for (;p!=NULL;p=p->next)
{
c++;
if (c==index)
break;
}
return p;
}
//在单向链表中的特定位置插入结点
node* insert(node *head,int location,int info)
{
int c=0;
node *p=head;
node *r=p;
node *q=(ndoe*)malloc(sizeof(node));
if (location < count(head))
{
//找出与给定位置匹配的位置
for (;p!=NULL;p=p->next)
{
c++;
if (c==location)
break;
}
q->next=p->next;
q->next=q;
q->data=info;
}
return r;
}
//显示单向链表的所有内容
void dispaly(node *head)
{
node *p=head;
//让我们遍历到末尾,直到NULL为止
for(;p!=NULL;p=p->next)
printf("Value=%d Address %u Next Address %u\n",p->data,p,p->next);
}
//得到单向链表的最大元素,得到最小值则同理
int findmax(node *head)
{
node *P;
int max=0;
p=head;
max=p->data;
while (p!=NULL)
{
if (p->data > max)
max=p->data;
p=p->next;
}
return max;
}
//使用给定值编辑特定结点的内容
node* replace(node* head,int location,int info)
{
int c=0;
node *p=head;
node *r=p;
node *q=(node*)malloc(sizeof(node*));
if (location<count(head))
{
//找到目标结点的位置
for (;p!=NULL;p=p->next)
{
c++;
if(c==location-1)
break;
}
}
}