由于做的毕业设计用到了链表,所以将就就链表的一些基本的操作自己写了下来,以后看看吧,省得每次用到链表都要自己写了,写一次就行了
(很多不合理的地方,以后留着改吧)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char str[100];
int num;
struct node *next;
}link;
link *cre_link()
{
link *head = (link*)malloc(sizeof(link*));
head->next = NULL;
return head;
}
link *insert_link(link *head,link *new_node)
{
link *temp = (link*)malloc(sizeof(link*));
temp = head;
if(temp->next == NULL)
{
temp->next = new_node;
new_node->next = NULL;
printf("head->next flag is %d\n",head->next->num);
return head;
}
while(temp->next)
{
temp = temp->next;
}
temp->next = new_node;
new_node->next = NULL;
return head;
}
void display_link(link *head)
{
link *temp = (link*)malloc(sizeof(link));
temp = head->next;
if(temp == NULL)
{
printf("empty\n");
}
while(temp)
{
printf("%d ",temp->num);
printf("%s\n",temp->str);
temp = temp->next;
}
}
link* delete_node(link *head,char *ptr)
{
link *p = (link*)malloc(sizeof(link));
link *q = (link*)malloc(sizeof(link));
p = q = head;
while(strcmp(p->next->str,ptr))
{
p = p->next;
q = p;
}
p = p->next;
q->next = p->next;
free(p);
return head;
}
void find_node(link *head,char *ptr)
{
link *p = (link*)malloc(sizeof(link));
if(head->next == NULL)
{
printf("empty\n");
}
p = head->next;
while(p)
{
if(strcmp(p->str,ptr) == 0)
{
printf("the element is fined %d %s\n",p->num,p->str);
return;
}
p = p->next;
}
printf("no this element\n");
}
link *reverse_link(link *head)
{
link *p,*s,*t;
p=head;
s=p->next;
while(s->next!=NULL)//主要置换过程
{
t=s->next;
s->next=p;
p=s;
s=t;
}
s->next=p;
head->next->next=NULL;//收尾
head->next=s;//赋头
return head;
}
主函数就不用了,方便以后和我一样的菜鸟学习