單鏈表 建表 輸出 插入 刪除 逆序

2021-07-01 21:06:29 字數 1189 閱讀 6484

面試的時候老是被問到單鏈表知識,索性總結一下。

這篇文章主要總結了單鏈表的建立、輸出、逆序、插入和刪除。

逆序原理圖:

源**:

#include using namespace std;

typedef int elemtype;//資料型別模板

typedef struct node//結點

node;

typedef struct node * linklist;

//建表

node* creat_link(node *head)

return head;//建完錶後返回頭結點

}//輸出

void output_link(node * head)

}//逆序

node * reverse_link(node * head)

return head;

}//插入

node * insert_link(node *head,int i, elemtype e)//i位置前插入值e

node *s;//在iq處插入s節點

//s=(linklist)malloc(sizeof(node)); //生成新節點

s=(node *)malloc(sizeof(node));

s->data=e;

s->next=iq->next;

iq->next=s;

return head;

}//刪除

node * delete_link(node *head,int i)//刪除位置i

p->next=q->next;

delete q;

//free(q);

} return head;

}int main()

{ node *head=null;

//creat_link(head);

node * m=creat_link(head);//建表

cout<

測試結果:

單鏈表逆序或者逆序輸出

分為兩種情況,一種是只逆序輸出,實際上不逆序 另一種是把鍊錶逆序。逆序輸出 include include includeusing namespace std typedef struct nodenode 尾部新增 node add int n,node head else if head ne...

單鏈表的建立, 刪除, 插入, 輸出

include include typedef struct node node,linklist int node num 0 void creat node head 建立鍊錶 void print node head 輸出鍊錶 node query node head,int value 查詢...

如何逆序輸出單鏈表?

怎樣才能逆序輸出單鏈表呢?剛剛有人考了我一道題,逆序輸出單鏈表 我是這樣答的 下面的 為偽 不能通過編譯 void printslist slist plist printf s str.c str 後來他讓我想一想還有沒有更為簡單的方法了,當時允許我上網,我用了幾分鐘到網上找了一下,沒有找到更好的...