#include<stdio.h>
#include<stdlib.h>
typedef struct _node{
int value;
struct _node *next;//不能使用 node *next;
}node;
typedef struct _List{
node *head;
node *tail;
}List;
void add(List* pList, int number);
void print(List *pList);
int main()
{
int number;
List list;
list.head =list.tail = NULL;
FILE *fp;
//用动态分配的方式创建一个链表
do{
scanf("%d", &number);
if(number != -1){
add(&list, number);
}
}while(number != -1);
print(&list);
scanf("%d",&number);
node*p;
int isFound = 0;
for( p=list.head; p; p=p->next ){
if ( p->value == number ){
printf("找到了\n");
isFound = 1;
break;
}
}
if( !isFound ){
printf("没找到\n");
}
return 0;
}
void add(List* pList, int number)
{
// add to linked-list
node *p = (node*)malloc(sizeof(node));
p->value = number;
p->next = NULL;
//find the last
node *last = pList->head;
if( last ){
while (last->next){
last = last->next;
}
}
else{
pList->head = p;
}
}
void print(List *pList)
{
node*p;
for( p=pList->head;p;p=p->next){
printf("%d\t", p->value);
}
printf("\n");
}
链表的应用
最新推荐文章于 2024-07-10 15:02:14 发布