链表的输入输出以及就地逆转

链表是个好东西,分享出来记录在这里,方便查找。

另外如struct member * list新建节点要赋值的时候,必须先分配内存空间list = (struct member *)malloc(sizeof(struct member))

然后才能赋值list->data = xxxx;

/*20131215 Denyz 链表是输入输出及就地逆转*/ 
#include <stdio.h>
#include <malloc.h>

typedef struct{
        int num;
        int name;
        }Elementype;
        
typedef struct LinkList{
        Elementype elem;
        struct LinkList *next;
        }LList;

/*链表的创建i个元素,返回Head指针*/
LList *CreatList(int length)
{     
      int i=0,data;
      LList *Head,*PL,*PLn;
   printf("Input the Link List:\n");
      Head = (LList *)malloc(sizeof(LList));
      
   PL=Head->next=(LList *)malloc(sizeof(LList));//第一个结点
   scanf("%d",&PL->elem.num);
   PL->next=NULL;

      for(i=1;i<length;i++)
      {
          PLn=(LList *)malloc(sizeof(LList));//
          scanf("%d",&data);
          PLn->elem.num=data; 
    PLn->next=PL->next;
    PL->next=PLn;//将新结点接入链表
    PL=PLn;                                            
      }
      
      return Head;
}
 
//线性链表就地逆转函数,用“砍头”法
//砍断头结点后,在头结点和Head指针的中间不断插入链表的结点,从而达到链表逆转的目的
void Reverse(LList *Head)
{
 LList *PL1,*PLn,*Tmp;
 PL1=Head->next;
 PLn=PL1->next;//先保存头结点后面一个结点的指针,不然头结点砍断后将丢失后面的结点
 PL1->next=NULL;//砍断第一个结点,也就是头结点
 
 while(PLn)
 {
  Tmp=PLn->next;//用Tmp保存当前结点的next结点的指针,确保砍断当前结点后丢失后面的结点。
  PLn->next=PL1;
  Head->next=PLn;
  PL1=PLn;//PLn成为下一个结点的next指针的指向目标
  PLn=Tmp;//PLn变成需要插入的结点的指针了
 }
 
}


void ShowList(LList *Head)
{
     LList *PL;
     PL=Head->next;
  printf("List is: ");

  while(PL)
  {
         printf("%d  ",PL->elem.num);
         PL=PL->next;
  }
   printf("\n");
}

void main()
{
     int num;
     LList *Head;
     Head=CreatList(4);/*构建4个数据的链表*/
  ShowList(Head);

  printf("Reverse the LinkList:\n");
  Reverse(Head);
     ShowList(Head);

     getch();
 
}

 

单向链表的就地逆转是指在不创建新的链表节点的情况下,将原链表的节点顺序反转。下面是一个C语言实现的单向链表就地逆转的代码: ```c #include <stdio.h> #include <stdlib.h> // 定义链表节点结构体 struct Node { int data; struct Node* next; }; // 创建新节点 struct Node* createNode(int data) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); if (newNode == NULL) { printf("内存分配失败\n"); exit(1); } newNode->data = data; newNode->next = NULL; return newNode; } // 将链表节点逆转 struct Node* reverseList(struct Node* head) { struct Node* prev = NULL; struct Node* current = head; struct Node* next = NULL; while (current != NULL) { next = current->next; current->next = prev; prev = current; current = next; } return prev; } // 打印链表 void printList(struct Node* head) { struct Node* temp = head; while (temp != NULL) { printf("%d ", temp->data); temp = temp->next; } printf("\n"); } int main() { // 创建链表 struct Node* head = createNode(1); head->next = createNode(2); head->next->next = createNode(3); head->next->next->next = createNode(4); head->next->next->next->next = createNode(5); printf("原链表:"); printList(head); // 将链表逆转 head = reverseList(head); printf("逆转后的链表:"); printList(head); return 0; } ``` 这段代码首先定义了一个链表节点的结构体,包含数据和指向下一个节点的指针。然后定义了创建新节点的函数`createNode`,用于创建新的链表节点。接下来是`reverseList`函数,用于将链表节点逆转。最后是`printList`函数,用于打印链表。 在`main`函数中,我们创建了一个包含5个节点的链表,并打印原链表。然后调用`reverseList`函数将链表逆转,并再次打印逆转后的链表。 运行以上代码,输出结果为: ``` 原链表:1 2 3 4 5 逆转后的链表:5 4 3 2 1 ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值