如何启动fragment backstackk5

常用类(64)
& & & & 在使用FragmentTransaction对fragment进行操作时,如果在commit()之前调用了它的addToBackStack()方法,那么本次操作中移除的fragment将会进入stop状态,并不会直接被销毁,而且会进入到回退栈中。如果没有调用addToBackStack(),那么本次操作中移除的fragment将直接被销毁(destoryed).
& & & & addToBackStack(String):它为本次fragment变换(transaction)指定了一个名字(unique name),一般情况下是没有用处的,除非打算使用FragmentManager.BackStackEntry中的api对fragment进行更高级的操作。如:
private void pop() {
FragmentManager fm = getSupportFragmentManager();
int count = fm.getBackStackEntryCount();
if (count & 0) {
fm.popBackStack(fm.getBackStackEntryAt(count - 1).getId(),
FragmentManager.POP_BACK_STACK_INCLUSIVE);
void addFragmentToStack() {
mStackLevel++;
// Instantiate a new fragment.
Fragment newFragment = CountingFragment.newInstance(mStackLevel);
// Add the fragment to the activity, pushing this transaction
// on to the back stack.
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.fragment_slide_right_exit,
R.anim.fragment_slide_right_enter,
R.anim.fragment_slide_left_enter,
R.anim.fragment_slide_left_exit);
ft.replace(R.id.connent, newFragment);
ft.addToBackStack(null);
ft.commit();
}& & & & 上述首先将fragment添加到backstack中,再通过Fragment#popBackStack()从backstack中回退一个fragment。
& & & & &有一点需要注意:弹出fragment时,会将该fragment上的所有fragment都一起弹出。
& & & & 可以使用FragmentTransacion#setCustomAnimations()为fragment的进入和退出添加动画。
& & & & 其参数分别为:正常进入时的动画,正常退出时动画。从backstack中弹出时进入动画,从backstack中弹出时退出动画。前两个参数好理解,后面两个不好理解。
& & & & 比如现在stackback中有f1,f2,f3三个fragment。当使用popBackStack()弹出f3时,f3就是退出,退出时执行的动画为第四个参数指定的;而f2会重新显露出来,f2就是进入,执行的动画就是第三个参数指定的。
& & & & 使用v4包中的fragment时,动画为tweent动画;使用系统自带的fragment时,动画为属性动画。
& & & &&Fragment与Fragment之间的通信必须通过宿主activity进行,两个fragment之间禁止直接通信。
activity到fragment
分两种情况:
创建fragment时
&&&&&& 通过fragment的Fragment.setArguments()即可。在Fragment中,可以通过getArguments()获得到传递的Bundle对象。
&&&&&& 但对于setArguments()来说,它要求在Fragment被附加到Activity之前调用(也就是commit()之前),所以最好的就是在Fragment被创建后立即调用该方法。为了满足这个条件,在Fragment中添加一个名为newInstance()的静态方法,该方法主要完成:fragment的实例,并且把bundle对象附加给相应的fragment。示例如下:
* bundle为传入Fragment中的参数。当没有参数时可以为null。
public static Fragment newInstance(Bundle bundle) {
MainFragment fragment = new MainFragment();
if (bundle != null) {
fragment.setArguments(bundle);
fragment已存在
& & & & 宿主activity中含有fragment的引用,因此可直接调用fragment中的public方法。
fragment到activity
fragment启动activity
& & & & 与activity启动activity一样,通过intent将参数传递给activity就行。
fragment返还到activity
&&&&&&& 如果该activity是fragment的托管activity,那么可以直接调用Fragment.getActivity()得到Activity对象,然后调用Activity中的方法将数据传输回去。但这种方法有一个缺点:Fragment与Activity捆绑的太紧。因此,可以通过接口进行通信:在Fragment中定义一个接口,Activity实现该接口并重写其中的方法。在Fragment中,合适时候调用接口中的方法。这样就将数据通过接口中的方法传递到了Activity中。
&&&&&& 当activity不是fragment的托管activity时,记该fragment的托管Activity为activityA,那么在启动activityA时使用startActivityForResult()。在fragment中调用getActivity().setResult()。
fragmentA到fragmentB
托管activity不相同
&&&&&&& 如果两个fragment的托管activity不相同。那么就转换为fragment到activity,再从activity到fragment。
托管activity相同
&&&&&& 当在fragmentA中实例fragmentB时,调用fragmentB的setArguments()即可。
如果不是,可以有两种方法。
第一种:通过setTargetFragment():
&&&&&& 调用fragmentA的setTargetFragment(),并将fragmentB当作参数传递进去。也就是说:把fragmentB当作fragmentA的目标fragment。
&&&&& 有两个fragment,一个是显示列表,一个显示详情。点击列表中的每一条时,更改详情中的内容。并且这两个fragment托管在同一个activity中,具体代码如下:
Activity中的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager manager = getFragmentManager();
ListFragment listFragment = new ListFragment();
DetailFragment detailFragment = new DetailFragment();
manager.beginTransaction().replace(R.id.lv1, listFragment).commit();
manager.beginTransaction().replace(R.id.lv2, detailFragment).commit();
// 将详情Fragment设置为列表Fragment的目标Fragment
listFragment.setTargetFragment(detailFragment,
DetailFragment.REQUEST_DETAIL);
}ListFragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ListView lv = new ListView(getActivity());
lv.setAdapter(new ArrayAdapter&String&(getActivity(),
android.R.layout.simple_list_item_1, date));
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView&?& parent, View view,
int position, long id) {
String text = date[position];
Intent data = new Intent();
data.putExtra(&value&, text);
//activity中设置了目标fragment,这里通过该方法直接获取到目标fragment
getTargetFragment().onActivityResult(getTargetRequestCode(),
RESULT_LIST_DETAIL, data);
}DetailFragment
// 因为在ListFragment中调用了该方法,所以DetailFragment重写该方法即可获得前者传递的数据
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == ListFragment.RESULT_LIST_DETAIL && data != null
&& requestCode == REQUEST_DETAIL) {
String extra = data.getStringExtra(&value&);
mTv.setText(extra);
}第二种:通过托管activity进行中转
&&&&&&& 由于两个fragment是托管于同一个activity,因此,可以先通过接口把fragmentA中的数据传递到托管activity中。再在activity中调用fragmentB的方法,从而可以将参数传递到fragment中。
FirstFragment
private OnTitleChangedL
* 在onAttach中判断相应的activity是否已经实现了定义的接口。
* 如果没实现,则fragment之间是无法进行通信的,所以抛异常。
public void onAttach(Activity activity) {
listener = (OnTitleChangedListener)
} catch (Exception e) {
throw new ClassCastException(
&activity must be implements OnTitleChangedListener&);
super.onAttach(activity);
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ListView v = new ListView(getActivity());
v.setAdapter(new BaseAdapter() {
public View getView(final int position, View convertView, ViewGroup parent) {
Button button = new Button(getActivity());
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
* 调用相应的接口中的方法,把参数传递到相应的activity中。
listener.onTitleChanged(position);
public long getItemId(int position) {
public Object getItem(int position) {
public int getCount() {
* 定义接口,相应的activity要实现该接口。
public interface OnTitleChangedListener {
* 接口中的方法,可以通过该方法把另一个fragment需要的参数传递到相应的activity中。
public void onTitleChanged(int titleId);
对应的Activity:/*
* 实现FirstFragment中定义的接口,方便FirstFragment传递数据
public class MainActivity extends Activity implements OnTitleChangedListener {
private FragmentM
private FirstFragment firstF
private SecondFragment secondF
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
manager = getFragmentManager();
firstFragment = new FirstFragment();
secondFragment = new SecondFragment();
change(R.id.fragment_container_top, firstFragment);
change(R.id.fragment_container_bottom, secondFragment);
* 当FirstFragment调用相应的方法时,参数会传递到Activity中。
* 再调用SecondFragment中的方法。这样参数就由FirstFragment传递到
* SecondFragment中了。至于SecondFragment怎么操作,不是Activity,FistFragment操心的。
public void onTitleChanged(int titleId) {
secondFragment.setText(titleId);//SecondFragment定义的方法,用于接收参数,并进行操作。
public void change(int containerId, Fragment clazz) {
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(containerId, clazz, clazz.getClass()
.getSimpleName());
} catch (Exception e) {
e.printStackTrace();
Fragment的保留
需求&&&&&&&
&&&&&&& 如果在Fragment中播放音频或视频时,切换了屏幕方向。虽然可以通过onSaveInstanceState()将进度记录下来,但是播放过程仍旧会中断。这个时候就需要将fragment保留下来。
&&&&&& 想要进行Fragment的保留,操作非常简单,重写Fragment.onCreate(),在该方法中调用setRetainInstance(true);即可。
&&&&&& 当设备配置发生变化时,总会销毁Fragment与Activity的视图,但并没有立刻销毁该fragment实例。这是因为,新的配置可能会有新的资源来匹配,当有更合适的资源时,就会重新构建新的视图,所以需要销毁旧的视图。
&&&&&& 视图销毁之后,FragmentManager会检查每一个fragment的mRetainInstance的值(setRetainInstance()改变的就是该变量的值)。如果该值为false(初始默认值),就会销毁fragment实例。随后新Activity中的新FragmentManager会建立新的fagment实例及视图。但是当该值为true时,该fragment的视图会被销毁,本身的实例不会被销毁。当新activity被创建后,新的FragmentManager会找到被保留的fragment实例,并重新创建它的视图。
&&&&&& 在这个过程中,虽然fragment仍然存在,但是已经没有任何activity托管它。把这个状态叫做保留状态。保留状态的存在时间非常短暂,从旧的activity被销毁后到新的activity建立前。
&&&&&& 如果一个fragment被保留,在onDestroyView()之后并不会调用onDestroy(),而是直接调用onDetach()。在重新被添加到activity时,会调用onAttach(),然后直接调用onCreateView(),而不会调用onCreate()。这是因为fragment被保留时,并不会销毁实例,只销毁视图,所以不需要调用onDestory()与onCreate()。但是它的托管activity会被重新实例化,因为需要调用onDetach()与onAttach()。
&&&&&& 只有当activity因设备配置发生改变而被销毁时,fragment才会短暂地处于保留状态。如果activity是因为操作系统需要回收内存而被销毁,则所有被保留的fragment也会随之销毁。
与onSaveInstanceState()的比较
&&&&&& 两者的主要区别在于数据可以保存多久。如果只需要短暂的保留数据以应对设备配置的变化,用保留fragment更方便。如果有需要长久地保存的东西,则应覆盖onSaveInstanceState()。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:32712次
积分:1750
积分:1750
排名:第15700名
原创:142篇
(3)(11)(14)(8)(1)(5)(6)(2)(2)(11)(2)(1)(3)(2)(4)(10)(2)(4)(8)(5)(2)(2)(1)(4)(2)(4)(4)(14)(5)(2)Narrow Your Results
Sort Order:
Price Low-High
Price High-Low
Date Added (Newest)
Items per page
5 items per page
10 items per page
20 items per page
25 items per page
50 items per page
100 items per page
For 16-14 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
For 12-10 wire.
&&&$0.077 each
&&&$0.066 each
Quantity: &
Minimum order of 50
For 16-14 wire.
&&&$0.06 each
&&&$0.05 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.06 each
&&&$0.05 each
Quantity: &
Minimum order of 50
For 12-10 wire.
&&&$0.08 each
&&&$0.076 each
Quantity: &
Minimum order of 50
For 16-14 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.044 each
&&&$0.038 each
Quantity: &
Minimum order of 50
For 16-14 wire.
&&&$0.06 each
&&&$0.05 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
For 16-14 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
For 12-10 wire.
&&&$0.077 each
&&&$0.066 each
Quantity: &
Minimum order of 50
For 16-14 wire.
&&&$0.06 each
&&&$0.05 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
For 12-10 wire.
&&&$0.08 each
&&&$0.076 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
Wire size 16-14.
&&&$0.09 each
&&&$0.085 each
Quantity: &
Minimum order of 50
For 16-14 wire.
&&&$0.09 each
&&&$0.085 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.09 each
&&&$0.085 each
Quantity: &
Minimum order of 50
For 16-14 wire.
&&&$0.09 each
&&&$0.085 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.09 each
&&&$0.085 each
Quantity: &
Minimum order of 50
Red. For 22-18 wire. MDFN 1.25-250. Will mate with CAT# 6125-MX.
CAT# 4125-MX
&&&$0.18 each
&&&$0.17 each
&&&$0.15 each
Quantity: &
Minimum order of 10
For 16-14 wire.
&&&$0.077 each
&&&$0.066 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.077 each
&&&$0.066 each
Quantity: &
Minimum order of 50
For 16-14 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
For 12-10 wire.
&&&$0.061 each
&&&$0.058 each
Quantity: &
Minimum order of 50
For 16-14 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
For 12-10 wire.
&&&$0.057 each
&&&$0.055 each
Quantity: &
Minimum order of 50
Wire size 22-18.
&&&$0.09 each
&&&$0.085 each
Quantity: &
Minimum order of 50
Wire size 12-10.
&&&$0.13 each
&&&$0.12 each
Quantity: &
Minimum order of 50
Wire size 12-10.
&&&$0.13 each
&&&$0.12 each
Quantity: &
Minimum order of 50
22-12 AWG wire, solid or stranded.
Connect 2 wires.
Simply push stripped wires into each port for a safe, reliable connection.
Flame-retardant, transparent, polycarbonate housing.
Test slot al...
CAT# GPC-352
518 Available
&&&$0.19 each
&&&$0.16 each
Quantity: &
Minimum order of 10
For 19- 26 AWG wire.
Simply put wires in place and push button with pliers or crimping tool.
Filled with silicone gel to insulate and improve conductance.
&&&$0.11 each
&&&$0.09 each
Quantity: &
Minimum order of 10
3M gel-filled butt splice for 19 - 26 AWG wire. No stripping required. Simply insert wires and push together with pliers.
Silicone gel filling protects from moisture.
Sold in packages of 30 pieces.
CAT# CON-023
Quantity: &
22-12 AWG wire, solid or stranded.
Connect 3 wires.
Simply push stripped wires into each port for a safe, reliable connection.
Flame-retardant, transparent, polycarbonate housing.
Test slot al...
CAT# GPC-353
801 Available
&&&$0.23 each
&&&$0.19 each
Quantity: &
Minimum order of 10
For 19- 26 AWG wire.
Simply put wires in place and push button with pliers or crimping tool.
Filled with silicone gel to insulate and improve conductance.
&&&$0.14 each
&&&$0.125 each
Quantity: &
Minimum order of 10
For 16-14 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
16-14 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
22-18 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
Wire size 16-14.
&&&$0.075 each
&&&$0.062 each
Quantity: &
Minimum order of 50
Wire size 16-14.
&&&$0.075 each
&&&$0.062 each
Quantity: &
Minimum order of 50
Wire size 16-14.
&&&$0.075 each
&&&$0.062 each
Quantity: &
Minimum order of 50
For 16-14 wire.
CAT# 82516
&&&$0.062 each
&&&$0.059 each
Quantity: &
Minimum order of 50
For 16-14 wire.
&&&$0.05 each
&&&$0.045 each
Quantity: &
Minimum order of 50
For 22-18 wire.
&&&$0.044 each
&&&$0.038 each
Quantity: &
Minimum order of 50
Sort Order:
Price Low-High
Price High-Low
Date Added (Newest)
Items per page
5 items per page
10 items per page
20 items per page
25 items per page
50 items per page
100 items per page
Customer Service: 818-904-0524 | Toll Free Order Line: 1-888-826-5432Function to check if a singly linked list is palindrome - GeeksforGeeks
Given a singly linked list of characters, write a function that returns true if the given list is palindrome, else false.
METHOD 1 (Use a Stack)
A simple solution is to use a stack of list nodes.
This mainly involves three steps.
1) Traverse the given list from head to tail and push every visited node to stack.
2) Traverse the list again. For every visited node, pop a node from stack and compare data of popped node with currently visited node.
3) If all nodes matched, then return true, else false.
Time complexity of above method is O(n), but it requires O(n) extra space.
Following methods solve this with constant extra space.
METHOD 2 (By reversing the list)
This method takes O(n) time and O(1) extra space.
Get the middle of the linked list.
Reverse the second half of the linked list.
Check if the first half and second half are identical.
Construct the original linked list by reversing the second half again and attaching it back to the first half
To divide the list in two halves, method 2 of post is used.
When number of nodes are even, the first and second half contain exactly half nodes. The challenging thing in this method is to handle the case when number of nodes are odd.
We don’t want the middle node as part of any of the lists as we are going to compare them for equality.
For odd case, we use a separate variable ‘midnode’.
/* Program to check if a linked list is palindrome */
#include&stdio.h&
#include&stdlib.h&
#include&stdbool.h&
/* Link list node */
struct node
struct node*
void reverse(struct node**);
bool compareLists(struct node*, struct node *);
/* Function to check if given linked list is
palindrome or not */
bool isPalindrome(struct node *head)
struct node *slow_ptr = head, *fast_ptr =
struct node *second_half, *prev_of_slow_ptr =
struct node *midnode = NULL;
// To handle odd size list
bool res = // initialize result
if (head!=NULL && head-&next!=NULL)
/* Get the middle of the list. Move slow_ptr by 1
and fast_ptrr by 2, slow_ptr will have the middle
while (fast_ptr != NULL && fast_ptr-&next != NULL)
fast_ptr = fast_ptr-&next-&
/*We need previous of the slow_ptr for
linked lists
with odd elements */
prev_of_slow_ptr = slow_
slow_ptr = slow_ptr-&
/* fast_ptr would become NULL when there are even elements in list.
And not NULL for odd elements. We need to skip the middle node
for odd case and store it somewhere so that we can restore the
original list*/
if (fast_ptr != NULL)
midnode = slow_
slow_ptr = slow_ptr-&
// Now reverse the second half and compare it with first half
second_half = slow_
prev_of_slow_ptr-&next = NULL; // NULL terminate first half
reverse(&second_half);
// Reverse the second half
res = compareLists(head, second_half); // compare
/* Construct the original list back */
reverse(&second_half); // Reverse the second half again
if (midnode != NULL)
// If there was a mid node (odd size case) which
// was not part of either first half or second half.
prev_of_slow_ptr-&next =
midnode-&next = second_
prev_of_slow_ptr-&next = second_
/* Function to reverse the linked list
Note that this
function may change the head */
void reverse(struct node** head_ref)
struct node* prev
struct node* current = *head_
struct node*
while (current != NULL)
= current-&
current-&next =
*head_ref =
/* Function to check if two input lists have same data*/
bool compareLists(struct node* head1, struct node *head2)
struct node* temp1 = head1;
struct node* temp2 = head2;
while (temp1 && temp2)
if (temp1-&data == temp2-&data)
temp1 = temp1-&
temp2 = temp2-&
else return 0;
/* Both are empty reurn 1*/
if (temp1 == NULL && temp2 == NULL)
/* Will reach here when one is NULL
and other is not */
/* Push a node to linked list. Note that this function
changes the head */
void push(struct node** head_ref, char new_data)
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data
new_node-&data
/* link the old list off the new node */
new_node-&next = (*head_ref);
/* move the head to pochar to the new node */
(*head_ref)
// A utility function to print a given linked list
void printList(struct node *ptr)
while (ptr != NULL)
printf(&%c-&&, ptr-&data);
ptr = ptr-&
printf(&NULL\n&);
/* Drier program to test above function*/
int main()
/* Start with the empty list */
struct node* head = NULL;
char str[] = &abacaba&;
for (i = 0; str[i] != '\0'; i++)
push(&head, str[i]);
printList(head);
isPalindrome(head)? printf(&Is Palindrome\n\n&):
printf(&Not Palindrome\n\n&);
Palindrome
b->a->NULL
Not Palindrome
a->b->a->NULL
Is Palindrome
c->a->b->a->NULL
Not Palindrome
a->c->a->b->a->NULL
Not Palindrome
b->a->c->a->b->a->NULL
Not Palindrome
a->b->a->c->a->b->a->NULL
Is Palindrome
Time Complexity O(n)
Auxiliary Space: O(1)
METHOD 3 (Using Recursion)
Use two pointers left and right. Move right and left using recursion and check for following in each recursive call.
1) Sub-list is palindrome.
2) Value at current left and right are matching.
If both above conditions are true then return true.
The idea is to use function call stack as container. Recursively traverse till the end of list. When we return from last NULL, we will be at last node. The last node to be compared with first node of list.
In order to access first node of list, we need list head to be available in the last call of recursion. Hence we pass head also to the recursive function. If they both match we need to compare (2, n-2) nodes. Again when recursion falls back to (n-2)nd node, we need reference to 2nd node from head. We advance the head pointer in previous call, to refer to next node in the list.
However, the trick in identifying double pointer. Passing single pointer is as good as pass-by-value, and we will pass the same pointer again and again. We need to pass the address of head pointer for reflecting the changes in parent recursive calls.
for suggesting this approach.
// Recursive program to check if a given linked list is palindrome
#include &stdio.h&
#include &stdlib.h&
#include &stdbool.h&
/* Link list node */
struct node
struct node*
// Initial parameters to this function are &head and head
bool isPalindromeUtil(struct node **left, struct
node *right)
/* stop recursion when right becomes NULL */
if (right == NULL)
/* If sub-list is not palindrome then no need to
check for current left and right, return false */
bool isp = isPalindromeUtil(left, right-&next);
if (isp == false)
/* Check values at current left and right */
bool isp1 = (right-&data == (*left)-&data);
/* Move left to next node */
*left = (*left)-&
return isp1;
// A wrapper over isPalindromeUtil()
bool isPalindrome(struct node *head)
isPalindromeUtil(&head, head);
/* Push a node to linked list. Note that this function
changes the head */
void push(struct node** head_ref, char new_data)
/* allocate node */
struct node* new_node =
(struct node*) malloc(sizeof(struct node));
/* put in the data
new_node-&data
/* link the old list off the new node */
new_node-&next = (*head_ref);
/* move the head to pochar to the new node */
(*head_ref)
// A utility function to print a given linked list
void printList(struct node *ptr)
while (ptr != NULL)
printf(&%c-&&, ptr-&data);
ptr = ptr-&
printf(&NULL\n&);
/* Drier program to test above function*/
int main()
/* Start with the empty list */
struct node* head = NULL;
char str[] = &abacaba&;
for (i = 0; str[i] != '\0'; i++)
push(&head, str[i]);
printList(head);
isPalindrome(head)? printf(&Is Palindrome\n\n&):
printf(&Not Palindrome\n\n&);
Not Palindrome
b->a->NULL
Not Palindrome
a->b->a->NULL
Is Palindrome
c->a->b->a->NULL
Not Palindrome
a->c->a->b->a->NULL
Not Palindrome
b->a->c->a->b->a->NULL
Not Palindrome
a->b->a->c->a->b->a->NULL
Is Palindrome
Time Complexity: O(n)
Auxiliary Space: O(n) if Function Call Stack size is considered, otherwise O(1).
Please comment if you find any bug in the programs/algorithms or a better way to do the same.
Related Posts:
Writing code in comment? Please use , generate link and share the link here.
Popular Posts
Subscribe and Never Miss an Article&&
Recent Comments
All CategoriesAll Categories
Select Category
Advanced Data Structure&&(83)
Algorithm&&(502)
&&&Analysis&&(16)
&&&Backtracking&&(15)
&&&Combinatorial&&(2)
&&&Divide and Conquer&&(32)
&&&Dynamic Programming&&(103)
&&&Geometric&&(15)
&&&Greedy&&(26)
&&&Mathematical&&(150)
&&&Pattern Searching&&(32)
&&&Randomized&&(20)
&&&Searching&&(44)
&&&Sorting&&(61)
Arrays&&(100)
Articles&&(25)
Binary Search Tree&&(38)
Bit Magic&&(61)
C/C++ Puzzles&&(300)
Competitive Programming&&(22)
Design&&(15)
GBlog&&(96)
GFacts&&(51)
Graph&&(95)
Hash&&(31)
Heap&&(20)
Interview Experiences&&(1,118)
&&&Experienced&&(41)
&&&Internship&&(55)
Java&&(94)
Linked Lists&&(83)
Matrix&&(54)
Misc&&(50)
Output&&(55)
Project&&(44)
Python&&(25)
Queue&&(17)
Stack&&(27)
Strings&&(84)
Technical Scripter&&(14)
TechTips&&(3)
Trees&&(139)}

我要回帖

更多关于 addtobackstack 无效 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信