⑴ 用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 == '