本文目錄一覽:
- 1、求C語言單鏈表 源代碼
- 2、c語言 單鏈表源代碼
- 3、求一份實現單鏈表的基本操作的c語言程序,包括輸入元素,謝謝。
- 4、用C語言實現建立一個單鏈表的過程,並實現列印鏈表中每一個元素,寫出完整程序
- 5、編寫一個C語言程序 實現單鏈表的基本操作
求C語言單鏈表 源代碼
#includestdio.h
#includestdlib.h
#includestring.h
struct people
{
char name[10];
int age;
struct people * next;
};
int main()
{
struct people * head=NULL;
struct people * prev , * current;
int flag=1;
while(flag!=0)
{
printf(“請輸入學生姓名,年齡:(年齡輸入0結束所有輸入工作)\n”);
current=(struct people *)malloc(sizeof(struct people));
if(head==NULL)
head=current;
else
prev-next=current;
current-next=NULL;
scanf(“%s”,current-name);
scanf(“%d”,current-age);
prev=current;
flag=current-age;
}
printf(“Output:\n”);
if(head==NULL)
printf(“無資料。\n”);
else
{
current=head;
while(current-next!=NULL)
{
printf(“姓名:%s\n年齡:%d\n\n”,current-name,current-age);
current=current-next;
}
}
}
至於排序,斷開舊鏈表,將前後指針鏈接到新的節點就好
如果還有問題歡迎再問哈
c語言 單鏈表源代碼
#include “stdafx.h”
#include stdio.h
#include malloc.h
typedef char ElemType;
struct LNode
{
ElemType data;
struct LNode *next;
};
//***********************************************************置空表setnull()
void setnull(struct LNode **p)
{
*p=NULL;
}
//************************************************************求長度length()
int length(struct LNode **p)
{
int n=0;
struct LNode *q=*p;
while (q!=NULL)
{
n++;
q=q-next;
}
return(n);
}
//*************************************************************取結點get()
ElemType get(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p;
while (ji q!=NULL) /**//*查找第i個結點*/
{
q=q-next;j++;
}
if (q!=NULL) /**//*找到了第i個結點*/
return(q-data);
else
{
printf(“位置參數不正確!\n”);
return NULL;
}
}
//************************************************************按值查找locate()
int locate(struct LNode **p,ElemType x)
{
int n=0;
struct LNode *q=*p;
while (q!=NULL q-data!=x) /**//*查找data域為x的第一個結點*/
{
q=q-next;
n++;
}
if (q==NULL) /**//*未找到data域等於x的結點*/
return(-1);
else /**//*找到data域等於x的結點*/
return(n+1);
}
//**********************************************************插入結點insert()
void insert(struct LNode **p,ElemType x,int i)
{
int j=1;
struct LNode *s,*q;
s=(struct LNode *)malloc(sizeof(struct LNode)); /**//*建立要插入的結點s*/
s-data=x;
q=*p;
if (i==1) /**//*插入的結點作為頭結點*/
{
s-next=q;
*p=s;
}
else
{
while (ji-1 q-next!=NULL) /**//*查找第i-1個結點*/
{
q=q-next;j++;
}
if (j==i-1) /**//*找到了第i-1個結點,由q指向它*/
{
s-next=q-next; /**//*將結點s插入到q結點之後*/
q-next=s;
}
else
printf(“位置參數不正確!\n”);
}
}
//*********************************************************刪除結點del()
void del(struct LNode **p,int i)
{
int j=1;
struct LNode *q=*p,*t;
if (i==1) /**//*刪除鏈表的頭結點*/
{
t=q;
*p=q-next;
}
else
{
while (ji-1 q-next!=NULL) /**//*查找第i-1個結點*/
{
q=q-next;j++;
}
if (q-next!=NULL j==i-1) /**//*找到第i-1個結點,由q指向它*/
{
t=q-next; /**//*t指向要刪除的結點*/
q-next=t-next; /**//*將q之後的結點刪除*/
}
else printf(“位置參數不正確!\n”);
}
if (t!=NULL) /**//*在t不為空時釋放該結點*/
free(t);
}
//********************************************************顯示鏈表display()
void display(struct LNode **p)
{
struct LNode *q;
q=*p;
printf(“單鏈表顯示:”);
if (q==NULL) /**//*鏈表為空時*/
printf(“鏈表為空!”);
else if (q-next==NULL) /**//*鏈表只有一個結點時*/
printf(“%c\n”,q-data);
else { /**//*鏈表存在一個以上的結點時*/
while (q-next!=NULL) /**//*顯示前面的結點*/
{
printf(“%c→”,q-data);q=q-next;
}
printf(“%c”,q-data); /**//*顯示最後一個結點*/
}
printf(“\n”);
}
void main()
{
struct LNode *head;
setnull(head);
insert(head,’a’,1);
insert(head,’b’,2);
insert(head,’a’,2);
insert(head,’c’,4);
insert(head,’d’,3);
insert(head,’e’,1);
display(head);
printf(“單鏈表長度=%d\n”,length(head));
printf(“位置:%d 值:%c\n”,3,get(head,3));
printf(“值:%c 位置:%d\n”,’a’,locate(head,’a’));
printf(“刪除第1個結點:”);
del(head,1);
display(head);
printf(“刪除第5個結點:”);
del(head,5);
display(head);
printf(“刪除開頭3個結點:”);
del(head,3);
del(head,2);
del(head,1);
display(head);
}
/**//*
運行結果:
單鏈表顯示:e→a→a→d→b→c
單鏈表長度=6
位置:3 值:a
值:a 位置:2
刪除第1個結點:單鏈表顯示:a→a→d→b→c
刪除第5個結點:單鏈表顯示:a→a→d→b
刪除開頭3個結點:單鏈表顯示:b
*/
求一份實現單鏈表的基本操作的c語言程序,包括輸入元素,謝謝。
#includestdlib.h
#include “nodedef.h”
#define CreateLinklistWay 1 // 0表示頭插法創建鏈表,1表示尾插法創建鏈表
#if !CreateLinklistWay
/*********************************************************************
* 函數名稱:linklist *CreateLinklistHead()
* 函數功能:利用頭插法創建鏈表
* 參 數:無
* 返 回 值:創建完鏈表後的鏈表頭結點
* 說 明:無
*********************************************************************/
extern linklist *CreateLinklistHead()
{
int x, 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;
}
return head; // 返回新建鏈表的頭結點
}
#else
/*********************************************************************
* 函數名稱:linklist *CreateLinklistRear()
* 函數功能:利用尾插法創建鏈表
* 參 數:無
* 返 回 值:創建完鏈表後的鏈表頭結點
* 說 明:無
*********************************************************************/
extern linklist *CreateLinklistRear()
{
int x, 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; // 將尾結點的指針域賦值為空(為了方便檢驗鏈表是否為空鏈表)
return head; // 返回頭結點
}
#endif
用C語言實現建立一個單鏈表的過程,並實現列印鏈表中每一個元素,寫出完整程序
這是個很簡單的鏈表創建和輸出
#includestdio.h
#includestdlib.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())!=’\n’)//輸入回車鍵時結束輸入
{
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(“\n”);
}
void main()
{
link_list L=NULL;
char x;
printf(“請輸入鏈表節點:\n”);
L=creat();
print(L);
}
編寫一個C語言程序 實現單鏈表的基本操作
# include stdio.h
# include malloc.h
# include stdlib.h
typedef struct Node
{
int data;
struct Node * pNext;
} * PNODE, NODE;
PNODE establish_list (void);
void traverse_list (PNODE pHead);
bool is_empty(PNODE pHead);
int length_list(PNODE pHead);
void sort_list(PNODE pHead);
void insert_list(PNODE pHead, int pos, int val);
int delete_list(PNODE pHead, int pos, int val);
void freeer(PNODE pHead);
int main(void)
{
PNODE pHead;
int len, i, j, val;
pHead = establish_list();
traverse_list(pHead);
if(is_empty(pHead))
printf(“鏈表為空\n”);
else
printf(“鏈表不空\n”);
len = length_list(pHead);
printf(“鏈表的長度為: %d\n”, len);
sort_list(pHead);
traverse_list(pHead);
printf(“請輸入您要在第幾個節點插入\n”);
scanf(“%d”, i);
printf(“請輸入您要在第%d個節點插入的值\n”, i);
scanf(“%d”, j);
insert_list(pHead, i, j);
traverse_list(pHead);
printf(“請輸入您要第幾個刪除的節點\n”);
scanf(“%d”, i);
val = delete_list(pHead, i, val);
printf(“您刪除的節點值為: %d\n”, val);
traverse_list(pHead);
freeer(pHead);
return 0;
}
PNODE establish_list(void)//初始化鏈表,返回頭結點地址
{
int val, len;
PNODE Tem;
PNODE pNew;
PNODE pHead;
pHead = (PNODE)malloc(sizeof(NODE));
Tem = pHead;
if(NULL == pHead)
{
printf(“分配失敗”);
exit(-1);
}
Tem-pNext = NULL;
printf(“請輸入您要定義節點的長度: “);
scanf(“%d”, len);
for (int i=0;ilen;++i)
{
printf(“請輸入第%d個節點的值: “, i+1);
scanf(“%d”, val);
pNew = (PNODE)malloc(sizeof(NODE));
if(NULL == pNew)
{
printf(“分配失敗”);
exit(-1);
}
pNew-data = val;//首先把本次創建的新節點的值付給新節點的數據域
Tem-pNext = pNew;//然後使用臨時的節點變數的指針域保存了新節點的地址,也就是指向了新節點
pNew-pNext = NULL;//如何再不循環,新節點成為最後一個節點
Tem = pNew;//把本次分配的新節點完全的賦給Tem,Tem就成為了這次新節點的影子,那麼下次分配新節點時可以使用上個新節點的數據
}
return pHead;
}
void traverse_list(PNODE pHead)
{
PNODE p = pHead;//使用P是為了不改寫頭結點裡保存的地址
p = pHead-pNext;//使P指向首節點
while(p != NULL)//P本來就是頭結點的指針域,也就是首節點的地址,既然是地址就可以直接判斷p是否等於NULL
{
printf(“%d “, p-data);
p = p-pNext;//使P每循環一次就變成P的下一個節點
}
}
bool is_empty(PNODE pHead)
{
if(NULL == pHead-pNext)
return true;
else
return false;
}
int length_list(PNODE pHead)
{
PNODE p = pHead-pNext;
int len = 0;
while(p != NULL)
{
len++;
p = p-pNext;
}
return len;
}
void sort_list(PNODE pHead)
{
int i, j, t, len;
PNODE p, q;
len = length_list(pHead);
for(i=0,p=pHead-pNext;ilen;i++,p=p-pNext)//逗號後只是為了找到下一個節點,因為不是數組,所以不能使用下標來++
{
for(j=0,q=pHead-pNext;jlen;j++,q=q-pNext)
if(q-data p-data)//這裡的大小與號可以決定是升序還是降序,如果是大於號就是升序,反之小於號就是降序
{
t = q-data;
q-data = p-data;
p-data = t;
}
}
return;
}
void insert_list(PNODE pHead, int pos, int val)
{
int i;
PNODE q = pHead;
PNODE p = pHead;
if(pos 0 pos = length_list(pHead))
{
for(i=0;ipos;i++)
{
q = q-pNext;//q就是要插入的連接點
}
for(i=1;ipos;i++)
{
p = p-pNext;//p就是要插入連接點的前一個節點
}
PNODE pNew = (PNODE)malloc(sizeof(NODE));
p-pNext = pNew;
pNew-data = val;
pNew-pNext = q;
}
else if(pos length_list(pHead))//追加
{
PNODE t;
t = pHead;
PNODE PN;
PN = (PNODE)malloc(sizeof(NODE));
if(PN == NULL)
printf(“分配失敗”);
else
while(t-pNext != NULL)
{
t = t-pNext;//使T-pNext成為尾結點
}
PN-data = val;//給新節點賦予有效數據
t-pNext = PN;//使尾結點的指針域指向了新的結點
PN-pNext = NULL;//新節點成為尾結點
}
else
printf(“error\n”);
return;
}
int delete_list(PNODE pHead, int pos, int val)
{
int i, j;
PNODE q, p;
q = pHead;
p = pHead;
if(pos 0 pos = length_list(pHead))//保證刪除的是節點的有效數
{
for(i=0;ipos;i++)
{
p = p-pNext;
}
for(j=1;jpos;j++)
{
if(pos == 0)
q = pHead;
else
q = q-pNext;
}
q-pNext = p-pNext;
val = p-data;
free(p);
return val;
}
else
printf(“error”);
}
void freeer(PNODE pHead)
{
PNODE pT = pHead;
while(NULL != pHead-pNext)
{
free(pT);
pT = pT-pNext;
}
return;
}
/*
好久以前寫的一個鏈表了,有排序,插入,刪除,輸出,判斷是否為空,甚至還有釋放堆中內存的功能
*/
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/227192.html