1樓:匿名使用者
#include
#include
struct node
;/*建立單連結串列的函式,返回的是頭結點*/struct node *create_slist()r->next='\0';
return h;
}/*輸出連結串列的函式,形參為頭結點*/
void print_slist(struct node *h)printf("->end\n");}}
/*插入結點的函式*/
void insert_node(struct node *h,int x)
else
}if(s->data<=q->data)//如果插入的值比最小值(也是最後一個值)小,則插入在最後面
else//如果插入的值比最大值(也就是第一個值)大,則插入在最前面}/*從大到小排序函式*/
void sort_slist(struct node *h)else
p=p->next;
}q=q->next;}}
/*主函式*/
void main()
執行結果:
建立並初始化,以-1為結束標誌,遍歷訪問連結串列1 2 3 5 4 6 -1
head->6->5->4->3->2->1->end輸入插入點結點的資料值x=7
在連結串列中插入結點
head->7->6->5->4->3->2->1->endpress any key to continue建立並初始化,以-1為結束標誌,遍歷訪問連結串列1 2 3 4 5 -1
head->5->4->3->2->1->end輸入插入點結點的資料值x=0
在連結串列中插入結點
head->5->4->3->2->1->0->endpress any key to continue建立並初始化,以-1為結束標誌,遍歷訪問連結串列1 2 3 4 -1
head->4->3->2->1->end輸入插入點結點的資料值x=2
在連結串列中插入結點
head->4->3->2->2->1->endpress any key to continue希望對你有所幫助!!
2樓:
#include
#include
#define count 5
typedef int elemtype;
typedef struct _lnodelnode, *linklist;
linklist createlist(elemtype *elemlist, int n)//elemlist遞增
return head;
}void printlist(linklist head)printf("\n");
}void insertelem(linklist head, elemtype x)//遞增表中插入
else
break;
}//head與p之間插入
q = (lnode*)malloc(sizeof(lnode));
q->data = x;
q->next = p;
head->next = q;
}void deletelist(linklist head)}void main()
;//保證序列遞增
linklist head = null;
elemtype x;
head = createlist(elemlist, count);
printf("列印連結串列:\n");
printlist(head);
printf("輸入插入元素:");
scanf("%d", &x);
insertelem(head, x);
printf("列印連結串列:\n");
printlist(head);
deletelist(head);}
建立一個單連結串列,並實現插入,刪除操作.
編寫一個完整的程式,實現單連結串列的建立、插入、刪除、輸出等基本操作。
3樓:匿名使用者
typedef int elemtype;
typedef int status;
#define overflow -2
#define ok 1
#define error -1
#include "stdio.h"
#include "stdlib.h"
typedef struct lnode *linklist;
//節點插入
}//遍歷輸出並輸出長度
printf("%d\n",p->data );
printf("長度為:%d\n",i);
return ok;
}//查詢值為x的直接前驅結點q並輸出
p=p->next ;
}if(p->next &&p->data ==x)
if(k==0)
printf("未找到值為%d的結點\n",&x);
printf("\n");
}//刪除節點
else
if(k==0)
printf("表中沒有值為%d的結點!\n",&x);
return ok;
}//連結串列逆置
k=p;
while (l->next !=p)
}//連結串列奇偶分解
p=p->next ;
}if(p->data %2==0)
}//主選單
void main()}}
資料結構中單連結串列的建立,插入和刪除。
4樓:扈懷煒
#include "stdio.h"
#include "stdlib.h"
#define ok 1
#define error 0
typedef int elemtype;
typedef int status;
typedef struct lnode lnode,*linklist;
//以下是建立單連結串列
printf("a list has been created successfully!\n");
}//以下是輸出單連結串列
void outputlist_l(linklist l)
printf("the list is:\n");
while (p )
printf("\n");
}//在第i個元素之前插入一個元素
status listinsert_l(linklist l, int i, elemtype e)
if(p==null)
if(i==j)
//請將該演算法補充完整
}// 刪除第 i 個元素
if(p==null)
if(i==j)
//請將該演算法補充完整
}int main()
else printf("the inserting position is error!please do again!\n");
}else if (choice==2)
else printf("the deleting position is error!please do again!\n");
}else if (choice==3)
else if(choice!=0)
printf("choice error\n");
}return 0;}
建立單連結串列的尾插法的程式設計思路是什麼
建立連結串列時不僅使用頭指標,還要另外使用一個尾結點指標,每次插入的結點成為當前尾結點的後繼結點 也就是成為新表尾結點 並且尾結點指標也要後移,指向新插入的尾結點 其實鏈佇列就是這樣插入 入隊 的 尾插法建表 思路 從一個空表開始,重複讀入資料,生成新結點,讀入資料存放在新結點的數 據域中,然後將新...
在具有n個結點的有序單連結串列中插入新結點並仍然保持有序的時間複雜度是為什麼是O(n
因為單連結串列儲存bai的資訊只du有表頭 如果zhi要在特定位置插入dao一個節點 需要先從表頭內一路找到那個節容點。數量級遞增排列,常見的時間複雜度有 常數階o 1 對數階o 線性階o n 線性對數階o nlog2n 平方階o n 2 立方階o n 3 k次方階o n k 指數階o 2 n 隨著...
以下建立連結串列的語句為什麼不對啊
連結串列沒有前後連線起來。與p同樣定義一個q和head,然後如下修改 p new people head p 儲存頭結點指標,以後遍歷需要使用for i 1 i n i p new people 在c 中,定義一個類people,才可以用new運算子來生成一個物件 struct結構體不行的,改為 s...