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 }//指针后移麻烦链表形式循环链表 */