2016 7 25 链表
生活随笔
收集整理的這篇文章主要介紹了
2016 7 25 链表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 #include<stdio.h>
2 #include<stdlib.h>
3 /*
4 usingnamespacestd;
5
6 structNode
7 {
8 int data;//數據域
9 struct Node*next;//指針域
10 };
11
12 /*
13 Create
14 *函數功能:創建鏈表.
15 *輸入:各節點的data
16 *返回值:指針head
17 *//*
18 Node*Create()
19 {
20 int n=0;
21 Node*head,*p1,*p2;
22 p1=p2=new Node;
23 cin>>p1->data;
24 head=NULL;
25 while(p1->data!=0)
26 {
27 if(n==0)
28 {
29 head=p1;
30 }
31 else
32 p2->next=p1;
33 p2=p1;
34 p1=new Node;
35 cin>>p1->data;
36 n++;
37 }
38 p2->next=NULL;
39 return head;
40 }
41
42 /*
43 insert
44 *函數功能:在鏈表中插入元素.
45 *輸入:head鏈表頭指針,p新元素插入位置,x新元素中的數據域內容
46 *返回值:無
47 void insert(Node*head,int p,int x)
48 {
49 Node*tmp=head;//for循環是為了防止插入位置超出了鏈表長度
50 for(inti=0;i<p;i++)
51 {
52 if(tmp==NULL)
53 return -1;
54 if(i<p-1)
55 tmp=tmp->next;
56 }
57 Node*tmp2=new Node;
58 tmp2->data=x;
59 tmp2->next=tmp->next;
60 tmp->next=tmp2;
61 }
62 */
63 /*
64 del
65 *函數功能:刪除鏈表中的元素
66 *輸入:head鏈表頭指針,p被刪除元素位置
67 *返回值:被刪除元素中的數據域.如果刪除失敗返回-1
68 intdel(Node*head,int p)
69 {
70 Node*tmp=head;
71 for(inti=0;i<p;i++)
72 {
73 if(tmp==NULL)
74 return -1;
75 if(i<p-1)
76 tmp=tmp->next;
77 }
78 int ret=tmp->next->data;
79 tmp->next=tmp->next->next;
80 return ret;
81 }
82
83 void print(Node*head)
84 {
85 for(Node*tmp=head;tmp!=NULL;tmp=tmp->next)
86 printf("%d",tmp->data);
87 printf("\n");
88 }
89
90 int main()
91 {
92 Node*head;
93 head=newNode;
94 head->data=-1;
95 head->next=NULL;
96 return 0;
97 }
98 */
99 //例子
100 #include<iostream>
101 //#define NULL 0
102 struct student
103 {
104 long num;
105 struct student*next;
106 };
107 int main()
108 {
109 int i,n;
110 student*p=(struct student*)malloc(sizeof(struct student));
111 student*q=p;
112 printf("輸入幾個值");
113 scanf("%d",&n);
114 for(i=1;i<=n;i++)
115 {
116 scanf("%d",&(q->num));
117 q->next=(struct student*)malloc(sizeof(struct student));
118 q=q->next;
119 }
120 printf("值第幾個");
121 int rank;
122 scanf("%d%d",&(q->num),&rank);
123 student*w=p;
124 for(i=1;i<rank-1;i++)
125 {
126 w=w->next;
127 }
128 q->next=w->next;
129 w->next=q;
130 for(i=1;i<=n+1;i++)
131 {
132 printf("%d",p->num);
133 p=p->next;
134 }
135 return 0;
136 }//指針后移麻煩鏈表形式循環鏈表 */
?
轉載于:https://www.cnblogs.com/sysu-eeman-yang/p/5987505.html
總結
以上是生活随笔為你收集整理的2016 7 25 链表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: codevs2171 棋盘覆盖
- 下一篇: Keepalived 添加脚本配置监控h