鏈表算法示例

輸入一個鏈表的頭結點,從尾到頭反過來印出來

具體程序:

可以將鏈表的插入,刪除,移除等程序寫入,頭文件。cpp中,然後使用頭文件調用的方式#include "..\utilistise\list.h"來調用即可

由於鏈表順序 然後反轉的實現是實現後入後出的方式,與棧相同,可以使用stack

聲明: void PrintList Reversing_Iteralively(ListNode*pHead)

std::stack<List*Node>nodes;

的方式聲明和使用棧

例如:一下是鏈表的具體使用:

#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)

/*----------------數據定義----------------------*/

//定義一個學生信息的結構體,包括學號,姓名和結構體類型的指針
struct student
{
    long num;                //學號
    char name[128];            //姓名
    struct student *next;    //結構體指針
};

typedef struct student * stuNode;

int n=0;                    //全局變量,記錄鏈表的長度
 
/*---------------函數聲明---------------------*/
 
stuNode Create();            //創建一個新的鏈表                     

void Print(stuNode head);    //通過傳入的鏈表頭指針打印整個鏈表

stuNode Delete(stuNode head,int num);    //通過傳入的鏈表頭指針和學生學號刪除節點

stuNode Insert(stuNode head,stuNode newStu);    //依照學生學號的順序向鏈表中插入新元素


/*---------------函數定義----------------------*/

struct student *Create()
{
    struct student *head,*p1,*p2;
    
    //開闢一個LEN大小的空間,並讓p1,p2指針指向它
    p2=p1=(struct student *)malloc(LEN);
    //將頭指針置爲NULL
    head=NULL;
    
    //創建鏈表節點並給節點的元素賦值
    printf("請輸入學生的學號和姓名:");
    scanf("%ld %s",&p1->num,p1->name);
    while(p1->num!=0)
    {
        n=n+1;
        if(NULL==head)
        {
            head=p1;
        }
        else
        {
            p2->next=p1;
        }
        p2=p1;
        p1=(struct student *)malloc(LEN);
        printf("請輸入學生的學號和姓名:");
        scanf("%ld %s",&p1->num,p1->name);
    }
    //將尾節點的指針置爲NULL
    p2->next=NULL;
    return head;
}


void Print(struct student *head)
{
    struct student * p;
    p=head;
    
    //判斷鏈表是否爲空
    if(NULL==head)
    {
        printf("鏈表爲空!\n");
        return head;
    }
    else
    {
        //循環打印鏈表中的元素
        printf("%d 個記錄分別爲:\n",n);
        while(p!=NULL)
        {
            printf("%ld %s\n",p->num,p->name);
            //指針指向下一個節點
            p=p->next;
        }
    }
}


struct student *Delete(struct student * head,int num)
{
    struct student *p1;
    struct student *p2;
    p1=head;
    //判斷鏈表是否爲空
    if(NULL==head)
    {
        printf("鏈表爲空!\n");
        return head;
    }
    //遍歷節點,判斷當前節點是不是需要刪除的節點及是否爲尾節點
    //如果找到相應節點,或者已經遍歷到尾節點就跳出循環
    while(p1->num!=num&&p1->next!=NULL)
    {
        p2=p1;
        p1=p1->next;
    }
    //判斷是否找到相應節點
    if(p1->num==num)
    {
        //要刪除的節點是不是鏈表的第一個節點
        //如果是,就將頭指針指向該節點的後一個節點
        //如果不是,就將該節點的前一個節點的指針指向該節點的後一個節點
        if(head==p1)
        {
            head=p1->next;
        }
        else
        {
            p2->next=p1->next;
        }
        n=n-1;
        printf("%ld 節點已刪除.\n",num);
    }
    else
    {
        printf("鏈表中沒有要刪除的元素.\n");
    }
    return head;
}


struct student *Insert(struct student * head,struct student * newStu)
{
    struct student *p0;
    struct student *p1;
    struct student *p2;
    p0=newStu;
    p1=head;
    //判斷鏈表是否爲空,如果是空鏈表,就將新節點作爲第一個節點
    if(NULL==head)
    {
        head=p0;
        p0->next=NULL;
    }
    else
    {
        //遍歷每一個節點中的學號,與新學號比較大小
        //如果找到一個學號比新學號大,就將新學號的節點插入它之前
        //如果尾節點的學號仍比新學號小,就將新節點插入到鏈表尾部
        while((p0->num > p1->num)&&(p1->next!=NULL))
        {
            p2=p1;
            p1=p1->next;
        }
        //找到一個比新學號大的節點
        if(p0->num <= p1->num)
        {
            //判斷該節點是否爲頭節點,如果是,則將新節點設置爲頭節點
            if(p1==head)
            {
                head=p0;
            }
            else
            {
                p2->next=p0;
            }
              p0->next=p1;
        }
        else
        {
            p1->next=p0;
            p0->next=NULL;
        }
    }
    //鏈表長度加1
    n=n+1;
    printf("%ld 插入成功!\n",newStu->num);
    return head;
}

void main()
{
    struct student *head;
    struct student *stu;
    int num;
    head=Create();
    Print(head);
    printf("請輸入要刪除的學號:");
    scanf("%ld",&num);
    while(num!=0)
    {
        head=Delete(head,num);
        Print(head);
        printf("請輸入要刪除的學號:");
        scanf("%ld",&num);
    }
    printf("請輸入要插入的節點:");
    stu=(struct student *)malloc(LEN);
    scanf("%ld %s",&stu->num,stu->name);
    while(stu->num!=0)
    {
        head=Insert(head,stu);
        printf("請輸入要插入的節點:");
        stu=(struct student *)malloc(LEN);
        scanf("%ld %s",&stu->num,stu->name);
    }
    Print(head);
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章