當前位置:首頁 » 編程語言 » c語言簡單鏈表的處理
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言簡單鏈表的處理

發布時間: 2022-11-04 20:19:30

⑴ 用c語言實現對單鏈表的基本操作

#include<stdio.h>
#include<stdlib.h>

typedefintDataType;

typedefstructnode{
DataTypemember;
structnode*next;
}*LinkList,*pNode;

//初始化鏈表
LinkListGetEmptyList(){
LinkListhead=(pNode)malloc(sizeof(structnode));
head->member=0;
head->next=NULL;
returnhead;
}


//在非增鏈表中插入結點
voidInsertNode(LinkListhead,DataTypex){
pNodep,q;
for(p=head;p->next!=NULL;p=p->next){
if(p->next->member<=x){
q=(pNode)malloc(sizeof(structnode));
q->member=x;
q->next=p->next;
p->next=q;
return;
}
}
q=(pNode)malloc(sizeof(structnode));
q->member=x;
q->next=p->next;
p->next=q;
}

//新結點插入為首結點
voidPushNode(LinkListhead,DataTypex){
pNodep=(pNode)malloc(sizeof(structnode));
p->member=x;
p->next=head->next;
head->next=p;
}

//刪除結點
intDeleteNode(LinkListhead,DataTypex){
pNodep,q;
for(p=head;p!=NULL;p=p->next){
if(p->next->member==x){
q=p->next;
p->next=q->next;
free(q);
return1;//成功刪除member(第一個)為x的結點
}
}
return0;//沒有找到member為x的結點
}

//查找結點
intFindNode(LinkListhead,DataTypex){
pNodep;
for(p=head->next;p!=NULL;p=p->next){
if(p->member==x)return1;//找到了
}
return0;//沒有找到
}

//銷毀鏈表
voidDestroyList(LinkListhead){
pNodeq,p=head;
while(p){
q=p;
p=q->next;
free(q);
}
head=NULL;
}

//遍歷鏈表
voidShowList(LinkListhead){
pNodep=head->next;
while(p!=NULL){
printf("%d",p->member);
p=p->next;
}
printf(" ");
}

intmain(){
DataTypex,res;
LinkListhead=GetEmptyList();
printf("輸入一個整數('q'toquit):");
while(scanf("%d",&x)==1){
InsertNode(head,x);//創建非增鏈表
printf("輸入一個整數('q'toquit):");
}
fflush(stdin);
ShowList(head);
printf("輸入待查找的整數:");
scanf("%d",&x);
res=FindNode(head,x);
if(res)printf("找到了。 ");
elseprintf("沒找到! ");
printf("輸入待刪除的整數:");
scanf("%d",&x);
res=DeleteNode(head,x);
if(res)printf("成功刪除。 ");
elseprintf("沒找到數據為:%d的結點! ",x);
ShowList(head);
DestroyList(head);
return0;
}

⑵ C語言鏈表的簡單操作

if(insert = NULL) 這里應該是 if(insert == NULL)

=是賦值,==是比較

⑶ C語言鏈表操作

包括鏈表的創建刪除添加和釋放操作!!
#include<stdio.h>
#include<stdlib.h>
struct node *create();
void print_list(struct node *head);
struct node * insert_node(struct node *h,int x,int y);
struct node * delete_node(struct node *h,int z);
void shifang(struct node *head);
struct node
{
char data;
struct node *next;
};
void main()
{
struct node *head;
int x,y,z;
head=create();
print_list(head);
printf("\n輸入插入結點的位置的值和插入的數值:");
scanf("%d%d",&x,&y);
head=insert_node(head,x,y);
print_list(head);
printf("\n輸入要刪除的結點:");
scanf("%d",&z);
head=delete_node(head,z);
print_list(head);
printf("\n釋放鏈表.\n");
}
struct node *create() //建立鏈表函數
{
printf("請輸入各節點(以-1結尾):\n");
int x;
//定義指針*head,*tail,*s;
struct node *head,*tail,*s;
//head和tail初始化,生成一個頭結點
head=tail=(struct node *)malloc(sizeof(struct node));
//在循環中,生成新結點、賦值、連接、尾指針後移
scanf("%d",&x);
while(x!=-1)
{
s=(struct node *)malloc(sizeof(struct node));
s->data=x;
tail->next=s;
tail=s;
scanf("%d",&x);
}
//尾結點的指針域賦NULL
tail->next=NULL;
return head;
}
void print_list(struct node *head) //輸出鏈表函數
{
//定義工作指針*p並賦初值p=head->next;即指向第一個結點
struct node *p;
p=head->next;
//判斷鏈表是否為空,空:輸出空表的信息,否則:輸出所有結點
if(p==NULL)
printf("The list is NULL.");
else
//在循環中輸出當前結點,工作指針後移
{
printf("head->");
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
printf("end.");
}
}
struct node * insert_node(struct node *h,int x,int y) //添加結點函數
{
struct node *p,*q,*s;
//生成要插入的新結點
s=(struct node *)malloc(sizeof(struct node));
s->data=y;
q=h;
p=h->next;
//查找要插入結點的位置
while((p!=NULL)&&(p->data!=x))
{
q=p;
p=p->next;
}
//插入結點
q->next=s;s->next=p;
return(h);
}
struct node * delete_node(struct node *h,int z) //刪除結點函數
{
struct node *p,*q;
q=h;
p=h->next ;
//查找要刪除結點的位置
if(p!=NULL)
{
while((p!=NULL)&&(p->data!=z))
{
q=p;
p=p->next;
}
//釋放結點
if(p->data ==z)
{
q->next=p->next ;
free(p);
}
}
return(h);
}
void shifang(struct node *head) //釋放鏈表函數
{
struct node *p;
//逐個釋放結點
while(head!=NULL)
{
p=head;
head=head->next;
free(p);
}
}

⑷ 用C語言實現建立一個單鏈表的過程,並實現列印鏈表中每一個元素,寫出完整程序

這是個很簡單的鏈表創建和輸出

#include<stdio.h>

#include<stdlib.h>

typedef struct linkednode

{

char data;

struct linkednode *next;

}node,*link_list;//鏈表節點的結構及重命名

link_list creat()//創建一個鏈表返回類型是鏈表的首地址

{

link_list L;

node *p1,*p2;

char data;

L=(node*)malloc(sizeof(node));//開辟存儲空間

p2=L;

while((data=getchar())!=' ')//輸入回車鍵時結束輸入

{

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

p1->data=data;

p2->next=p1;

p2=p1;

}

p2->next=NULL;

return L;

}

void print(link_list L)//把鏈表輸出

{

node *p;

p=L->next;

while(p!=NULL)

{

printf("%c",p->data);

p=p->next;

}

printf(" ");

}

void main()

{

link_list L=NULL;

char x;

printf("請輸入鏈表節點: ");

L=creat();

print(L);

}

⑸ c語言中鏈表的概念和簡單的實現

鏈表

鏈表概述
鏈表是一種常見的重要的數據結構。它是動態地進行存儲分配的一種結構。它可以根據需要開辟內存單元。鏈表有一個「頭指針」變數,以head表示,它存放一個地址。該地址指向一個元素。鏈表中每一個元素稱為「結點」,每個結點都應包括兩個部分:一為用戶需要用的實際數據,二為下一個結點的地址。因此,head指向第一個元素:第一個元素又指向第二個元素;……,直到最後一個元素,該元素不再指向其它元素,它稱為「表尾」,它的地址部分放一個「NULL」(表示「空地址」),鏈表到此結束。

單向鏈表
單向鏈表的每個結點中除信息域以外還有一個指針域,用來指出其後續結點,單向鏈表的最後一個結點的指針域為空(NULL)。單向鏈表由頭指針唯一確定,因此單向鏈表可以用頭指針的名字來命名,例如頭指針名為head的單向鏈表稱為表head,頭指針指向單向鏈表的第一個結點。

簡單實現為:

#define NULL 0

typedef int DATATYPE

typedef struct node

{DATATYPE info;

node *next;

}LINKLIST;

雙向鏈表
每個結點中只包括一個指向下個結點的指針域,這種鏈表稱為單向鏈表。如果要在單向鏈表一個指針所指的當前位置插入一個新結點,就必須從鏈表頭指針開始逐個遍歷直到當前指針所指結點的前一結點,修改這個結點的指針。雙向鏈表的每個結點中包括兩個指針域,分別指向該結點的前一個結點和後一個結點。在雙向鏈表中由任何一個結點都很容易找到其前面的結點和後面的結點,而不需要在上述的插入(及刪除)操作中由頭結點開始尋找。

簡單實現為:

typedef struct node

{ DATATYPE info;

node *priv, *next;

}DINKLIST;

循環鏈表
單向鏈表的最後一個結點的指針域為空(NULL)。如果將這個指針里利用起來,以指向單向鏈表的第一個結點,就組成一個單向循環鏈表。

這里有一篇好文章:http://myweb.yzu.e.cn/toby88/c/cstudy/shenru/jiegou/lianbiao.htm

⑹ C語言鏈表的使用方法

D
答案D設置完,p就從鏈表中丟掉了。
p就是一個指向結構體node的指針。
p->next就是p包含的執行下一個node的指針,在本題,就是q。

⑺ 用C語言編程實現單鏈表的基本操作

運行結果如下:


完整代碼如下:


#include<stdio.h>

#include<stdlib.h>


typedef struct LNode

{

char data;

LNode *next;

}* LNodePtr;


LNodePtr CreateList()

{

//初始化頭節點

LNodePtr head = (LNodePtr)malloc(sizeof(LNode));

head->data = 0;

head->next = NULL;


LNodePtr tNode;//臨時節點

char data;

while(true)

{

scanf("%c",&data);

if(data == '' || data == ' ' || data == ' ' || data == ' ')

{

continue;

}

if(data == '!')//輸入感嘆號停止插入節點

{

printf("輸入鏈表元素結束。 ");

break;

}

if(data >= 'A' && data <= 'Z')

{

tNode = (LNodePtr)malloc(sizeof(LNode));

tNode->data = data; /* 數據域賦值 */

tNode->next = head->next;

head->next = tNode;

}

else

{

printf("輸入字元需為大寫字母。 ");

}

}

return head;

}


/**

加密函數,加密的方式為鏈接head的所有節點前移offset位,但是不支持offset比鏈表的節點數多

@param head 鏈表頭節點

@param offset 節點前移的位數

*/

void EncryptList(LNodePtr head,int offset)

{

LNodePtr tNode = head->next;

int i;

for(i = 0; i < offset; i++)

{

if(tNode->next != NULL)

{

tNode = tNode->next;

}

}

if(i == offset)

{

LNodePtr newHead = tNode;

LNodePtr tail = tNode;

while (tail->next != NULL)

{

tail = tail->next;

}

tail->next = head->next;

while(tNode->next != NULL)

{

if(tNode->next != newHead)

{

tNode = tNode->next;

}

else

{

tNode->next = NULL;

break;

}

}

head->next = newHead;

}

else

{

printf("不支持移動");

}

}


void ListPrint(LNodePtr head)

{

if(head->next != NULL)

{

LNodePtr tNode = head->next;

while (tNode->next != NULL)

{

printf("%c ",tNode->data);

tNode = tNode->next;

}

printf("%c",tNode->data);

}

}


int main()

{

LNodePtr list = CreateList();

printf(" 創建的鏈表如下: ");

ListPrint(list);

EncryptList(list,3);

printf(" 所有節點前移了3位之後的鏈表如下: ");

ListPrint(list);

printf(" ");

return 0;

}


如果看不懂代碼可以問我

⑻ 用c語言寫一個簡單的鏈表,具體要怎麼用代碼實現

可以用結構體和指針來實現
定義:
定義一個單個元素的結構

typedef struct Chain_tag { // 這里用typedef來定義,方便使用 int data; // 這里的數據可以是任意類型 //其他數據 struct Chain_tag *prev, *next;// 由於Chain為不完全類型,故只能用指針的方式聲明} Chain;

使用:
用單個結構體的指針作為head

#include <malloc.h> //Chain的定義寫在這里 Chain *alloc_single_chain(int data /*, (其他參數)*/){ Chain *tmp; tmp = malloc(sizeof(Chain)); tmp.data = data; //...其餘數據初始化 tmp.prev = tmp.next = NULL; // 將前後指針置為NULL return tmp;} voiddispose_chain(Chain *target) //其實這里功能簡單,用宏實現也可以{ free(target); return;} int main(){ Chain *head; Chain *pos; head = alloc_single_chain(10);//初始化起始結點 head->next = alloc_single_chain(11);//同理。。下一個結點 for (pos = head; pos; pos = pos->next)//清理垃圾好習慣 { dispose_chain(pos); } return 0;}

⑼ 用c語言寫一個簡單的鏈表,具體要怎麼用代碼實現

struct
stu
{
char
a[10];
char
e[4];
char
c[14];
char
d[30];
struct
stu*b;
};
struct
stu
*creat(struct
stu*head,char
c)
//功能1:創建鏈表
{
while(c-'y'==0||c-'y'==0)
{
printf("請輸入姓名,性別,電話號碼,e-mail地址\n");
head=insert(head);
printf("是否繼續輸入y/n\n");getchar();
scanf("%c",&c);system("cls");
}
if(c-'n'==0||c-'n'==0)
return
head;
else
{
printf("是否繼續輸入y/n\n");
getchar();
scanf("%c",&c);
system("cls");
return
(creat(head,c));
}
}

⑽ 怎樣在C語言中正確運用鏈表鏈表的使用需要注意哪些要點

1.使用鏈表時候,先確認要使用的是單向鏈表,還是雙向鏈表,或者是循環鏈表。一定要初始化。
2.添加節點時候,要注意是否隊列已滿。
3.刪除節點的時候,要注意隊列是否為空。
4.要有可以判斷鏈表是否為空的函數。
5.要有可以判斷鏈表節點個數的函數。