#include
#include
#include
#include
using namespace std;
typedef struct student
int data;
struct student *next;
}node;
//建立單鏈表
node *creat()
node *head, *p, *s;
int x, cycle = 1;
head = (node *)malloc(sizeof(node));
p = head;
while(cycle)
printf("\nplease input the data: ");
scanf("%d",&x);
if(x != 0)
s = (node *)malloc(sizeof(node));
s->data = x;
printf("\n%d",s->data);
p->next = s;
p = s;
else
cycle = 0;
head = head->next;
p->next = null;
printf("\n
yyy
%d",head->data);
return (head);
//單鏈表測長
int length(node *head)
int n = 0;
node *p;
p = head;
while(p != null)
p = p->next;
n++;
return (n);
//單鏈表列印
void print(node *head)
node *p;
int n;
n =length(head);
printf("\nnow,these %d records are :\n",n);
p = head;
if(head != null)
//空鍊錶
while(p != null)
printf("
uuu
%d \n",p->data);
p = p->next;
//單鏈表刪除節點
node *del(node *head, int x)
node *p1, *p2;
p1 = head;
while((x != p1->data) && (p1->next != null))
//元素x不是首節點&&沒到鍊錶尾
p2 = p1;
p1 = p1->next;
if(x = p1->data)
if(p1 == head)
//被刪除節點為首節點
head = p1->next;
free(p1);
else
p2->next = p1->next;
else
printf("\n%d cound not been found",x);
return (head);
//單鏈表插入節點(在第k個節點後插入)
node *insert(node *head, int k, int x)
node *p0, *p1;
int i, l;
l = length(head);
p1 = head;
p0 = (node *)malloc(sizeof(node));
//建立新節點
p0->data = x;
if(k
printf("\nout of bounds");
for(i=1; (ii++)
p1 = p1->next;
if(k)
p0->next = p1->next;
p1->next = p0;
else
p0->next = head;
head = p0;
return (head);
//單鏈表從小到大排序
node *sort(node *head)
node *p, *p2;
int l, temp;
int i, j;
l = length(head);
if((head == null)||(head->next == null))
//非空鍊錶或單資料鏈表
return (head);
p = head;
for(i=1; ii++)
p = head;
//每個迴圈後把指標指向煉表頭
for(j=i+1; j<=l; j++)
p2 = p->next;
if((p->data) > (p2->data))
temp = p2->data;
p2->data = p->data;
p->data = temp;
p = p->next;
return (head);
//單鏈表逆置(僅僅逆置了資料,前驅後繼關係沒有逆置)
node *reverse1(node *head)
node *p, *p2;
int l, temp;
int i, j;
l = length(head);
if((head == null)||(head->next == null))
//非空鍊錶或單資料鏈表
return (head);
p = head;
for(i=1; ii++)
p = head;
//每個迴圈後把指標指向煉表頭
for(j=i+1; j<=l; j++)
p2 = p->next;
if(1)
temp = p2->data;
p2->data = p->data;
p->data = temp;
p = p->next;
return (head);
//單鏈表逆置(連線順序逆置)
node *reverse2(node *head)
node *p1, *p2, *p3;
if((head == null)||(head->next == null))
//非空鍊錶或單資料鏈表
return (head);
p1 = head;
p2 = p1->next;
while(p2)
p3 = p2->next;
p2->next = p1;
p1 = p2;
p2 = p3;
head->next = null;
head = p1;
return (head);
//main函式呼叫測試
int main(int, char**, char**)
node *a, *head_del, *head_ins, *head_sort, *head_rev;
a = creat();
length(a);
print(a);
printf("\nplease enter the data which you want to delete: ");
int x;
scanf("%d",&x);
head_del = del(a, x);
print(head_del);
printf("\nplease enter the number which you want to insert: ");
int k, y;
scanf("%d",&k);
scanf("%d",&y);
printf("\nk = %d\ny = %d\n", k, y);
head_ins = insert(head_del, k, y);
print(head_ins);
printf("\nsorting from small to large:");
head_sort = sort(head_ins);
print(head_sort);
printf("\nreversing the data:");
//reverse1(head_sort);
//print(head_sort);
head_rev = reverse2(head_sort);
print(head_rev);
return 0;}
**至微博
**至微博
如何提高VS2010的效能,VS2010不再卡
教大家幾個技巧可以稍微讓你覺得它沒那麼卡!一 vs2010選項視覺體驗設定 工具 選項 環境 視覺體驗的勾選都去掉。不解釋,你懂得。不過還是建議開啟硬體加速 二 禁用intellisense 工具 選項 文字編輯器 c c 高階 禁用intellisense設為true 這個功能很好用。相較於vis...
單鏈表各種操作
終於自己寫出了關於單鏈表的操作,而不是看別人的,現在程式設計越來越有感覺了,自己編更好,別人的還看不懂,不知道他們的思路是什麼 單鏈表的建立,插入,刪除,排序,求長度。插入是按大小順序插入的。include include struct node void creat node void print...
單鏈表的各種操作
單鏈表的各種操作 define null 0 typedef char elemtype 字元型資料 typedef struct lnode setnull struct lnode p int length struct lnode p elemtype get struct lnode p,i...