2016 7 25 链表

#include<stdio.h>
#include<stdlib.h>

/*
usingnamespacestd;
 
structNode
{
  int  data;//数据域
  struct Node*next;//指针域
};
 
/*
Create
*函数功能:创建链表.
*输入:各节点的data
*返回值:指针head
*//*
Node*Create()
{
  int  n=0;
  Node*head,*p1,*p2;
  p1=p2=new Node;
  cin>>p1->data;
  head=NULL;
while(p1->data!=0)
{
    if(n==0)
   {
     head=p1;
   }
   else
    p2->next=p1;
    p2=p1;
    p1=new Node;
    cin>>p1->data;
    n++;
}
    p2->next=NULL;
    return head;
}
 
/*
insert
*函数功能:在链表中插入元素.
*输入:head链表头指针,p新元素插入位置,x新元素中的数据域内容
*返回值:无

void insert(Node*head,int p,int x)
{
   Node*tmp=head;//for循环是为了防止插入位置超出了链表长度
   for(inti=0;i<p;i++)
   {
     if(tmp==NULL)
     return -1;
     if(i<p-1)
     tmp=tmp->next;
    }
    Node*tmp2=new Node;
      tmp2->data=x;
      tmp2->next=tmp->next;
      tmp->next=tmp2;
}
 */
/*
del
*函数功能:删除链表中的元素
*输入:head链表头指针,p被删除元素位置
*返回值:被删除元素中的数据域.如果删除失败返回-1

intdel(Node*head,int p)
{
   Node*tmp=head;
   for(inti=0;i<p;i++)
   {
      if(tmp==NULL)
      return -1;
      if(i<p-1)
        tmp=tmp->next;
}
   int ret=tmp->next->data;
   tmp->next=tmp->next->next;
   return ret;
}
 
void print(Node*head)
{
    for(Node*tmp=head;tmp!=NULL;tmp=tmp->next)
      printf("%d",tmp->data);
      printf("\n");
}
 
int main()
{
  Node*head;
  head=newNode;
  head->data=-1;
  head->next=NULL;
  return 0;
}
*/
//例子
#include<iostream>
//#define NULL 0
struct student
{
   long num;
   struct student*next;
};

int main()
{
   int i,n;
   student*p=(struct student*)malloc(sizeof(struct  student));
   student*q=p;
   printf("输入几个值");
   scanf("%d",&n); 
   for(i=1;i<=n;i++)
   {
     scanf("%d",&(q->num));
     q->next=(struct student*)malloc(sizeof(struct student));
     q=q->next;
    }
     printf("值第几个");
     int rank;
     scanf("%d%d",&(q->num),&rank);
     student*w=p;
     for(i=1;i<rank-1;i++)
     {
        w=w->next;
    }
    q->next=w->next;
    w->next=q;
    for(i=1;i<=n+1;i++)
    {
      printf("%d",p->num);
      p=p->next;
    }
    return 0;
}//指针后移麻烦链表形式循环链表  */

时间: 2024-07-31 23:20:23

2016 7 25 链表的相关文章

2016.8.25 NOIP2012 day1 解题报告

考试总结: 1.  显然第一道送分题,我差一点直接打表来对拍了,后来发现我们的pdf有问题不能复制,并且我没有YJQ大神那样足够的时间每道题都对拍,就只能试过样例就遁,(其实这种密码我玩过,密码学入门密码,当时好像叫凯撒密码233):对了,ASCII的掌握也很重要,我之前一直以为大写在小写后面啧.(之间漏掉过大于以后减的步骤,这种简单题还是做少了居然耗了30min,以后好好检查争取一次过): 2.  这道题我拿到的第一反应就是贪心,马上想了一个贪心规则但自己总觉得是错的,花了很长的时间举反例举不

2016 - 1 - 25 第三方网络框架 AFN的简单使用

AFNetworking 底层是对NSURlSession 和对 NSURLConnect 的包装 1.具体使用方法可以参照github上的主页面,在这里只是举一个文件上传的简单列子 - (void)update{ AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; [manager POST:@"http://120.25.226.186:32812/upload" parameters:nil constr

PAT甲级题解-1097. Deduplication on a Linked List (25)-链表的删除操作

给定一个链表,你需要删除那些绝对值相同的节点,对于每个绝对值K,仅保留第一个出现的节点.删除的节点会保留在另一条链表上.简单来说就是去重,去掉绝对值相同的那些.先输出删除后的链表,再输出删除了的链表. 建立结构体节点,包括起始地址addr,下一个地址to,值value.链表数组索引为地址,接下来就是模拟链表的操作了,并且建立一个flag数组标记对应值K是否出现,若出现则flag[k]=addr,未出现则为-1,注意这里不能为0因为地址值存在为0的情况.最后的输出地址前面要补0,如果是链尾的话,-

习题3.25 链表和数组对队列例程实现

//链表做队列 /* assume a header */ struct Node; struct Queue; typedef struct Node * PtrToNode; typedef struct Queue * PtrToQ; struct Node{ ElementType ele; PtrToNode Next; }; struct Queue{ PtrToNode rear; PtrToNode front; }; PtrToQ CreateQ( void ) { PtrTo

编程两道——悼念2016.3.25模拟糟糕的模拟考

很久不做算法题,模拟考一团糟,今天自己实现记录下: 1.生成N位格雷码,e.g.[00,01,10,11] 1 #include <iostream> 2 #include <string> 3 #include <cmath> 4 using namespace std; 5 6 7 const int n = 4; 8 int size = 0; 9 int num = int(pow(2, n)); 10 string* codes = new string[nu

PAT Advanced 1032 Sharing(25) [链表]

题目 To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same sufix. For example, "loading" and "being" are stored

PAT Advanced 1052 Linked List Sorting (25) [链表]

题目 A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integer key and a Next pointer to the next structure. Now given a linked list, you are supposed to sort the s

PAT Advanced 1097 Deduplication on a Linked List (25) [链表]

题目 Given a singly linked list L with integer keys, you are supposed to remove the nodes with duplicated absolute values of the keys. That is, for each value K, only the first node of which the value or absolute value of its key equals K will be kept.

2016.3.25(mySQL简单的数据查询)

要从数据库中查询,要使用SQL的select语句,标准select查询有select子句,where子句,order by子句组成. 查询数据的标准结构为:select 列名 from 表名 where 条件 order by 列名 asc(升序)/desc(降序) 查询操作的分类:1.投影操作,指定查询结果中能显示哪些列 2.选择操作,指定哪行出现在结果中 3.排序操作,指定查询的结果以什么样的顺序显示 投影操作:select 列1,列2 from 表名 表前缀:select 表名.列名 fr