遍历单链表

遍历是在单链表中执行的最常见操作。 遍历表示访问链表的每个节点一次,以便对其执行某些操作。这将通过使用以下语句来完成。

ptr = head;   
while (ptr!=NULL)  
{  
    ptr = ptr -> next;  
}

算法


第1步:设置PTR = HEAD
第2步:如果PTR = NULL
    提示“空链表”
    转到第7步
   结束条件

第4步:重复第5步和第6步直到PTR!= NULL
第5步:打印PTR→DATA
第6步:PTR = PTR→NEXT
[循环结束]

第7步:退出

C语言示例代码 -

#include<stdio.h>  
#include<stdlib.h>  
void create(int);
void traverse();
struct node
{
    int data;
    struct node *next;
};
struct node *head;
void main()
{
    int choice, item;
    do
    {
        printf("1.Append List\n");
        printf("2.Traverse\n");
        printf("3.Exit\n");
        printf("4.Enter your choice ? ");
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            printf("\nEnter the item\n");
            scanf("%d", &item);
            create(item);
            break;
        case 2:
            traverse();
            break;
        case 3:
            exit(0);
            break;
        default:
            printf("\nPlease enter valid choice\n");
        }

    } while (choice != 3);
}
void create(int item)
{
    struct node *ptr = (struct node *)malloc(sizeof(struct node *));
    if (ptr == NULL)
    {
        printf("\nOVERFLOW\n");
    }
    else
    {
        ptr->data = item;
        ptr->next = head;
        head = ptr;
        printf("\nNode inserted\n");
    }

}
void traverse()
{
    struct node *ptr;
    ptr = head;
    if (ptr == NULL)
    {
        printf("Empty list..");
    }
    else
    {
        printf("printing values . . . . .\n");
        while (ptr != NULL)
        {
            printf("\n%d", ptr->data);
            ptr = ptr->next;
        }
    }
}

执行以下示例代码,得到以下结果 -

1.Append List
2.Traverse
3.Exit
4.Enter your choice?1

Enter the item
23

Node inserted

1.Append List
2.Traverse
3.Exit
4.Enter your choice?1

Enter the item
233

Node inserted

1.Append List
2.Traverse
3.Exit
4.Enter your choice?2
printing values . . . . .

233
23

上一篇: 链表 下一篇: 双链表