#include <stdio.h>
typedef struct Node
{
int data;
struct Node *next;
}LNode, *LinkList;
void create_list_rear(LinkList *head,int n)//尾插法
{
int i;
LinkList p, q;
p = q = *head = (LinkList)malloc(sizeof(LNode));
p= NULL;
int count = 0;
while (count<n){
++count;
p = (LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next = NULL;
q->next = p;
q = p;
}
printf("\n");
}
int getlist(LinkList head,int x)//查
{
int count=0;
head=head->next;
while (head != NULL){
if(head->data==x)
count++;
head = head->next;
}
return count;
}
void printList(LinkList head)//打印
{
head=head->next;
while (head != NULL){
printf("%d ",head->data);
head = head->next;
}
printf("\n");
}
int main()
{
LinkList head;
int count;
/*尾插法*/
printf("原数列的顺序为:\n");
create_list_rear(&head,5);
printf("链表的顺序为:\n");
printList(head);
printf("x出现: ");
count=getlist(head,2);
printf("%d",count);
return 0;
}
单链表(不含头结点)--尾插,查找
最新推荐文章于 2023-08-26 15:19:02 发布