1.
反向输出一个链表(共5个整型元素)。 程序运行示例如下: please input 5 data==> 1 2 3 4 5 The value is ==>5 The value is ==>4 The value is ==>3 The value is ==>2 The value is ==>1
#include <stdio.h>
#include <stdlib.h>
#define MAXN 5
struct List
{
int data;
struct List *next;
};
typedef struct List node;
typedef node *link;
int main (void)
{
link ptr, head, tail;
int num;
tail = (link)malloc (sizeof (node));
if (tail == NULL)
{
printf ("Error!");
exit (1);
}
tail -> next = NULL;
ptr = tail;
printf("\nplease input 5 data==>\n");
int i;
for (i = 0; i < MAXN; i ++)
{
scanf ("%d", &num);
ptr -> data = num;
head = (link)malloc (sizeof(node));
if (head == NULL)
{
printf ("Error!");
exit (1);
}
head -> next = ptr;
ptr = head;
}
ptr = ptr -> next;
while (ptr != NULL)
{
printf ("The value is ==>%d\n", ptr -> data);
ptr = ptr -> next;
}
free (tail);
free (head);
return 0;
}
2. 将一个链表按逆序排列,即将链头当链尾,链尾当链头。 程序的运行示例如下: 请输入链表(非数表示结束) 结点值:3 结点值:4 结点值:5 结点值:6 结点值:7 结点值:end 原来表: 3 4 5 6 7 反转表: 7 6 5 4 3
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct Node;
typedef struct Node *pNode;
typedef struct Head *pHead;
struct Head
{
int length;
pNode next;
};
struct Node
{
int data;
pNode pre;
pNode next;
};
pHead Create (void);
bool isEmpty (pHead pH);
int Insert (pHead pH, int pos, int val);
void Print (pHead pH);
void ReversePrint (pHead pH);
int main (void)
{
printf ("\n");
printf ("请输入链表(非数表示结束)\n");
pHead pH = NULL;
pH = Create ();
int num;
while (1)
{
printf ("结点值:");
if (scanf ("%d", &num) != 1)
{
break;
}
Insert (pH, 0, num);
}
printf ("\n原来表:\n");
Print (pH);
printf ("\n");
printf ("\n反转表:\n");
ReversePrint (pH);
return 0;
}
pHead
Create (void)
{
pHead pH = (pHead)malloc (sizeof (struct Head));
if (pH == NULL)
{
printf ("Error Malloc!");
exit (1);
}
pH -> next = NULL;
pH -> length = 0;
return pH;
}
bool
isEmpty (pHead pH)
{
if (pH == NULL)
{
printf ("Error LinkList!");
}
if (pH -> length == 0)
{
return true;
}
else
{
return false;
}
}
int
Insert (pHead pH, int pos, int val)
{
pNode p = NULL;
if (pH == NULL || pos < 0 || pos > pH -> length)
{
printf ("Error Insert");
}
p = (pNode)malloc (sizeof (struct Node));
p -> data = val;
if (isEmpty (pH))
{
pH -> next = p;
p -> next = NULL;
p -> pre = NULL;
}
else
{
pNode pCur = pH -> next;
if (pos == 0)
{
pH -> next = p;
p -> pre = NULL;
p -> next = pCur;
pCur -> pre = p;
}
else
{
int i;
for (i = 1; i < pos; i ++)
{
pCur = pCur -> next;
}
p -> next = pCur -> next;
pCur -> next -> pre = p;
p -> pre = pCur;
pCur -> next = p;
}
}
pH -> length ++;
return 1;
}
void
ReversePrint (pHead pH)
{
if (pH == NULL)
{
printf ("Error LinkList!");
}
pNode p = pH -> next;
while (p != NULL)
{
printf ("%4d", p -> data);
p = p -> next;
}
printf ("\n");
}
void
Print (pHead pH)
{
if (pH == NULL)
{
printf ("Error LinkList!");
}
pNode p = pH -> next;
while (p -> next != NULL)
{
p = p -> next;
}
int i;
for (i = pH -> length - 1; i >= 0; i --)
{
printf ("%4d", p -> data);
p = p -> pre;
}
}
3. 超长正整数的加法 请设计一个算法完成两个超长正整数的加法。 **输出格式要求:" s1=" " s2=" "s1+s2=" 程序运行示例如下: 3488934387589 374849389 s1=3488934387589 s2=374849389 s1+s2=3489309236978
4.
将一个链表中元素值为x的结点删除。(链表数据域为整数,初始长为6个元素) 程序运行示例如下: 输入数组6个元素的值。 11 22 33 44 55 66 此链表各个结点的数据域为:11 22 33 44 55 66 输入要删除的数据x: 33 删除后链表各个结点的数据域为:11 22 44 55 66
#include <stdio.h>
#include <stdlib.h>
#define MAXN 6
struct Node
{
int data;
struct Node *next;
};
struct Header
{
int length;
struct Node *next;
};
typedef struct Node *pNode;
typedef struct Header *pHead;
pHead Create (void);
int Insert (pHead pH, int pos, int val);
pNode Search (pHead pH, int val);
pNode Delete (pHead pH, int val);
void Print (pHead pH, pNode p);
int main (void)
{
printf ("输入数组6个元素的值。\n");
pHead pH = Create ();
int i, val;
for (i = 0; i < MAXN; i ++)
{
scanf ("%d", &val);
Insert (pH, 0, val);
}
printf ("此链表各个结点的数据域为:");
Print (pH, pH -> next);
printf ("\n");
int num;
printf ("输入要删除的数据x: ");
scanf ("%d", &num);
Delete (pH, num);
printf ("删除后链表各个结点的数据域为:");
Print (pH, pH -> next);
}
pHead
Create (void)
{
pHead pH = (pHead)malloc (sizeof (struct Header));
pH -> length = 0;
pH -> next = NULL;
return pH;
}
int
Insert (pHead pH, int pos, int val)
{
if (pH == NULL || pos < 0 || pos > pH -> length)
{
return 0;
}
pNode p = (pNode)malloc (sizeof (struct Node));
p -> data = val;
pNode pCur = pH -> next;
if (pos == 0)
{
pH -> next = p;
p -> next = pCur;
}
else
{
int i;
for (i = 1; i < pos; i ++)
{
pCur = pCur -> next;
}
p -> next = pCur -> next;
pCur -> next = p;
}
pH -> length ++;
return 1;
}
pNode
Search (pHead pH, int val)
{
pNode p = pH -> next;
do
{
if (p -> data == val)
{
return p;
}
p = p -> next;
}
while (p -> next != NULL);
printf ("Not Found!");
return NULL;
}
pNode
Delete (pHead pH, int val)
{
pNode p = Search (pH, val);
if (p == NULL)
{
printf ("NaN");
return NULL;
}
pNode q = pH -> next;
pNode pCur = NULL;
if (q -> data == val)
{
pH -> next = q -> next;
pH -> length ++;
return q;
}
else
{
int i;
for (i = 0; i < pH -> length; i ++)
{
pCur = q -> next;
if (pCur -> data == val)
{
q -> next = pCur -> next;
pH -> length --;
return pCur;
}
q = q -> next;
}
}
}
void
Print (pHead pH, pNode p)
{
if (p -> next != NULL && p -> next != pH)
{
Print (pH, p -> next);
}
printf ("%d ", p -> data);
}