c語言單鏈表的創建
A. 用C語言創建一個具有10個元素的單鏈表
#include <stdio.h>
#include<stdlib.h>
typedef struct List
{
int e;
struct List *next;
}List;
void initList(List *head)
{
int n,i;
List *p,*q;
scanf("%d",&n);
p=head;
for(i=0;i<n;i++)
{
q=(List*)malloc(sizeof(List));
if(!q)
return;
scanf("%d",&q->e);
q->next=NULL;
p->next=q;
p=p->next;
}
}
void displayList(List *head)
{
List *p;
for(p=head->next;p;p=p->next)
printf("%d ",p->e );
printf(" ");
}
void destoryList(List *head)
{
List *p;
while(head->next) {
p=head->next;
head->next=p->next;
free(p);
}
free(head);
}
void insertList(List *head,int x)
{
List *p=head;
while(p->next) {
p=p->念陪next;
}
p->next=(List*)malloc(sizeof(List));
p->next->e=x;
p->next->仔冊蠢next=NULL;
}
void deleteList(List *head,int x)
{
List *p,*pre;
int flag=1;
for(p=head->next;p;p=p->next)
{
if(p->e==x)
{
pre->next=p->next;
free(p);
p=pre;
flag=0;
}
pre=p;
}
if(flag)
printf("No element to delete! ");
}
int main()
{
int x;
List *head=(List*)malloc(sizeof(List));
initList(head);
displayList(head);
printf("Input a integer to insert:");
scanf("%d",&x);
insertList(head,x);
printf("Processed list:");
displayList(head);
printf("Input a integer to delete:");
scanf("%d",&x);
deleteList(head,x);
printf("Processed list:");
displayList(head);
destoryList(head);
return 0;
}
運行結果姿昌:
B. 用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. C語言建立帶頭結點的單鏈表
單鏈表的生成有2種方式:頭插法和尾插法。
1、頭插法
/*********************************************************************
*函數名稱:linklist*CreateLinklistHead()
*函數功能:利用頭插法創建鏈表
*參數:無
*返回值:創建完鏈表後的鏈表頭結點
*說明:無
*********************************************************************/
externlinklist*CreateLinklistHead()
{
intx,i,nodeNum;
linklist*head,*temp;//頭結點與臨時結點
head=(linklist*)malloc(sizeof(linklist));//生成表頭結點
head->next=NULL;//給表頭結點的指針域賦值
printf("請輸入鏈表中結點的個數:");
scanf("%d",&nodeNum);
for(i=1;i<=nodeNum;i++)
{
printf("請輸入第%d個結點的數據裂兆:",i);
scanf("%d",&x);
temp=(linklist*)malloc(sizeof(linklist));//生成新的結點
temp->data=x;//對新結點的數據域賦值
//將新結點插到頭結點之後
temp->next=head->next;
head->next=temp;
}
returnhead;//返回新建鏈表的頭結點
}
2、尾插法
/*********************************************************************
*函數名稱:linklist*CreateLinklistRear()
*函數功能:利用尾插法創建鏈表
*參數:無
*返回值:創建完鏈表後的鏈表頭結點
*說明鉛缺:無
*********************************************************************/
externlinklist*CreateLinklistRear()
{
intx,i,nodeNum;
linklist*head,*rear,*temp;//定義頭結點、尾結點和臨時結點
head=(linklist*)malloc(sizeof(linklist));//生成表頭結點,表頭結點不存放數據
head->next=NULL;//將表頭結點的指針域賦值為NULL
rear=head;//將表頭結點賦值給表尾結點
printf("請輸入鏈表中結點的個數:");
scanf("槐源辯%d",&nodeNum);
for(i=1;i<=nodeNum;i++)
{
printf("請輸入第%d個結點的數據:",i);
scanf("%d",&x);
temp=(linklist*)malloc(sizeof(linklist));//生成新的結點
temp->data=x;//新增結點的數據域
temp->next=NULL;//新增結點的指針域(由於是尾插法,所以插入的結點都在尾部,即指針域為NULL)
rear->next=temp;//使前一個結點指向新增結點(head->next=temp)
rear=temp;//將新增結點賦值給尾結點(尾插法,插入的結點在尾部)(rear=head->next)
}
//rear->next=NULL;//將尾結點的指針域賦值為空(為了方便檢驗鏈表是否為空鏈表)
returnhead;//返回頭結點
}
D. 用C語言編程實現單鏈表的基本操作
第二個顯示為什麼?為什麼什麼東西都沒有、
#include<stdio.h>
typedef
struct
sample
{
char
ch;
struct
sample
*next;
}LNode;
LNode
*Createlist(LNode
*head)
//創建單鏈表,頭插入法
{
LNode
*p;
if((head=(LNode
*)malloc(sizeof(LNode)))==NULL)
printf("aply
error\n");
head->next=NULL;
head->ch
=
'\0';
int
i=
0;
printf("請一次輸入A-Z:\n");
for(i
=
0
;
i
<
26;
++i)
{
p=(LNode
*)malloc(sizeof(LNode));
if(!p)
printf("aply
error\n");
scanf("%c",&p->ch);
p->next
=
head->next;
head->next=p;
}
return
head;
}
LNode
*EncryptList(LNode*
head)
{
LNode
*p=
head,*q
=
head->next,*r
=
head->next;
while(p->next)
{
p
=
p->next;
}
int
i
=
0
;
for(i
=
0
;
i
<
3;
i++)
{
p->next
=
r;
p
=
p->next;
q
=
q->next;
r
=
q;
}
p->next
=NULL;
head->next
=
q;
return
head;
}
void
ListPrint(LNode
*head)
{
LNode
*p
=
head->next;
while(p->next!=NULL)
{
printf("%c\t",p->ch);
p=p->next;
}
printf("%c\n",p->ch);
}
int
main(void)
{
LNode
*head;
head
=
Createlist(head);//鏈表初始化
ListPrint(head);
//列印單鏈表數據
head
=
EncryptList(head);
ListPrint(head);
return
0;
}
看看。
E. C語言創建鏈表,函數調用部分
#include<stdio.h>
#include<windows.h>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
//定義數據類型名稱
typedef int DataType;
#define flag -1 //定義數據輸入結束的標志數據
//單鏈表結點存儲結構定義
typedef struct Node
{
DataType data;
struct Node *next;
}LNode ,*LinkList;
//建立單鏈表子函數
LNode *Create_LinkList()
{
LNode *s,*head,*L;int i=0,x; //定義指向當前插入元素的指針
while(1)
{
scanf("%d",&x);
if(-1==x)
{ return head;
break;}
s= (LNode *)malloc(sizeof(LNode)); //為當前插入元素的指針分配地址空間
s->data =x;
s->next =NULL;
i++;
if(i==1)
head=s;
else
L->next =s;
L=s;
}
}
//查找子函數(按序號查找)
LNode *Get_LinkList(LinkList L,int i)
{
LNode *p;
int j; //j是計數器,用來判斷當前的結點是否是第i個結點
p=L;
j=1;
while(p!=NULL&&j<i)
{
p=p->next ; //當前結點p不是第i個且p非空,則p移向下一個結點
j++;
}
return p;
}
//插入運運算元函數
void Insert_LinkList(LinkList L,int i,DataType x) //在單鏈表L中第i個位置插入值為x的新結點
{
LNode *p,*s;
p =Get_LinkList(L,i); //尋找鏈表的第i-1個位置結點
if(p==NULL)
{
printf("插入位置不合法!");
exit(-1);
}
else
{
s= (LinkList)malloc(sizeof(LNode)); //為當前插入元素的指針分配地址空間
s->data =x;
s->next =p->next ;
p->next =s;
}
}
//單鏈表的刪除運運算元函數
void Delete_LinkList(LinkList L,int i) //刪除單鏈表上的第i個結點
{
LNode *p,*q;
p=Get_LinkList(L,i-1); //尋找鏈表的第i-1個位置結點
if(p==NULL)
{
printf("刪除的位置不合法!"); //第i個結點的前驅結點不存在,不能執行刪除操作
exit(-1);
}
else
{
if(p->next ==NULL)
{
printf("刪除的位置不合法!"); //第i個結點不存在,不能執行刪除操作
exit(-1);
}
else
{
q=p->next ;
p->next =p->next->next;
free(q);
}
}
}
//求表長運運算元函數
int Length_LinkList(LinkList L)
{
int l; //l記錄L的表長
LNode *p;
p=L;
l=1;
while(p->next)
{
p=p->next;
l++;
}
return l;
}
int main ()
{
LNode *head,*p;
head=(LinkList)malloc(sizeof(LNode));
int x,y;
a:
printf("*******menu*******
");
printf("**創建**********1*
");
printf("**插入**********2*
");
printf("**刪除**********3*
");
printf("**表長**********4*
");
printf("**清屏**********5*
");
printf("**列印**********6*
");
printf("**退出******other*
");
printf("******************
");
int i=1;
while(i)
{
printf("請輸入選項:");
scanf("%d",&i);
switch(i)
{
case 1:head=Create_LinkList(); getchar();break;
case 2:printf("請輸入位置和數據;");
scanf("%d%d",&x,&y);
Insert_LinkList(head,x,y);break;
case 3:printf("請輸入位置;");
scanf("%d",&x);
Delete_LinkList(head,x);break;
case 4:printf("%d",Length_LinkList(head));break;
case 5:system("cls");goto a;
case 6:p=head;
while(p!=NULL)
{printf("%d
",p->data);
p=p->next;}
break;
default :i=0;
}
}
}
我把創建給改了一下