链表 树 哈希表
单向链表
1、链表建立
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE *create()
{
NODE *head,*q,*p;
char ch;
int flag,a;
head=(NODE *)malloc(sizeof(NODE));
q=head;
scanf("%d",&flag);
ch=getchar();
scanf("%d",&a);
while(ch!="!")
{
a!=flag;
p=(NODE *)malloc(sizeof(NODE));
p->data=a;
q->next=p;
q=p;
ch=getchar();
scanf("%d",&a);
}
q->next=NULL;
return (head);
}
2、查找
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE *find_number(NODE *head,int i)
{
int j;
NODE *p;
j=1;
p=head->next;
while(j<i&&(p!=NULL))
{
p=p->next;
j++;
}
return (p);
}
按值查找算法如下:
typedef struct node
{
int data;
struct node *next;
}NODE;
NODE *find_value(NODE *head,int v)
{
NODE *p;
p=head->next;
while((p->data!=v)&&(p!=NULL))
{
p=p->next;
}
return (p);
}
3、求链表长度
typedef struct node
{
int data;
struct node *next;
}NODE;
int ListLength(NODE *head)
{
int length=0;
NODE *temp;
temp=head;
while(temp->next!=NULL)
{
length++;
temp=temp->next;
}
return length;
}
4、插入运算
void insert(NODE *head,NODE *p,int x)
{
NODE *q;
q=(NODE *)malloc(sizeof(NODE));
q->data=x;
if(head=NULL)
{
head=q;
q->next=NULL;
}
else
{
q->next=p->next;
p->next=q;
}
}5、删除运算
void del(NODE *head,NODE *p,int x)
{
NODE *p,*q;
q=head;
p=head->next;
while((p!=NULL)&&(p->data!=x))
{
q=p;
p=p-next;
}
if(p=NULL)
printf("cannot found\n");
else
{
q->next=p-next;
free(p);
}
}
浙公网安备 33010602011771号